lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1997-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | static void |
| 23 | write_data (FILE *stream) |
| 24 | { |
| 25 | int i; |
| 26 | for (i=0; i<100; i++) |
| 27 | fprintf (stream, "%d\n", i); |
| 28 | if (ferror (stream)) |
| 29 | { |
| 30 | fprintf (stderr, "Output to stream failed.\n"); |
| 31 | exit (1); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | read_data (FILE *stream) |
| 37 | { |
| 38 | int i, j; |
| 39 | |
| 40 | for (i=0; i<100; i++) |
| 41 | { |
| 42 | if (fscanf (stream, "%d\n", &j) != 1 || j != i) |
| 43 | { |
| 44 | if (ferror (stream)) |
| 45 | perror ("fscanf"); |
| 46 | puts ("Test FAILED!"); |
| 47 | exit (1); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static int |
| 53 | do_test (void) |
| 54 | { |
| 55 | FILE *output, *input; |
| 56 | int wstatus, rstatus; |
| 57 | |
| 58 | /* We must remove this entry to assure the `cat' binary does not use |
| 59 | the perhaps incompatible new shared libraries. */ |
| 60 | unsetenv ("LD_LIBRARY_PATH"); |
| 61 | |
| 62 | output = popen ("/bin/cat >/tmp/tstpopen.tmp", "w"); |
| 63 | if (output == NULL) |
| 64 | { |
| 65 | perror ("popen"); |
| 66 | puts ("Test FAILED!"); |
| 67 | exit (1); |
| 68 | } |
| 69 | write_data (output); |
| 70 | wstatus = pclose (output); |
| 71 | printf ("writing pclose returned %d\n", wstatus); |
| 72 | input = popen ("/bin/cat /tmp/tstpopen.tmp", "r"); |
| 73 | if (input == NULL) |
| 74 | { |
| 75 | perror ("/tmp/tstpopen.tmp"); |
| 76 | puts ("Test FAILED!"); |
| 77 | exit (1); |
| 78 | } |
| 79 | read_data (input); |
| 80 | rstatus = pclose (input); |
| 81 | printf ("reading pclose returned %d\n", rstatus); |
| 82 | |
| 83 | remove ("/tmp/tstpopen.tmp"); |
| 84 | |
| 85 | errno = 0; |
| 86 | output = popen ("/bin/cat", "m"); |
| 87 | if (output != NULL) |
| 88 | { |
| 89 | puts ("popen called with illegal mode does not return NULL"); |
| 90 | puts ("Test FAILED!"); |
| 91 | exit (1); |
| 92 | } |
| 93 | if (errno != EINVAL) |
| 94 | { |
| 95 | puts ("popen called with illegal mode does not set errno to EINVAL"); |
| 96 | puts ("Test FAILED!"); |
| 97 | exit (1); |
| 98 | } |
| 99 | |
| 100 | puts (wstatus | rstatus ? "Test FAILED!" : "Test succeeded."); |
| 101 | return (wstatus | rstatus); |
| 102 | } |
| 103 | |
| 104 | #define TEST_FUNCTION do_test () |
| 105 | #include "../test-skeleton.c" |