]>
Commit | Line | Data |
---|---|---|
568035b7 | 1 | /* Copyright (C) 1997-2013 Free Software Foundation, Inc. |
993b3242 UD |
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 | |
41bdb6e2 AJ |
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. | |
993b3242 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
993b3242 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
993b3242 | 17 | |
6591c335 | 18 | #include <errno.h> |
28f540f4 RM |
19 | #include <stdio.h> |
20 | #include <stdlib.h> | |
21 | ||
cf3141a5 | 22 | static void |
993b3242 | 23 | write_data (FILE *stream) |
28f540f4 RM |
24 | { |
25 | int i; | |
26 | for (i=0; i<100; i++) | |
27 | fprintf (stream, "%d\n", i); | |
6591c335 UD |
28 | if (ferror (stream)) |
29 | { | |
30 | fprintf (stderr, "Output to stream failed.\n"); | |
31 | exit (1); | |
28f540f4 RM |
32 | } |
33 | } | |
34 | ||
cf3141a5 | 35 | static void |
993b3242 | 36 | read_data (FILE *stream) |
28f540f4 RM |
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 | int | |
993b3242 | 53 | main (void) |
28f540f4 RM |
54 | { |
55 | FILE *output, *input; | |
56 | int wstatus, rstatus; | |
57 | ||
993b3242 UD |
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 | ||
e66f63fb | 62 | output = popen ("/bin/cat >/tmp/tstpopen.tmp", "w"); |
28f540f4 RM |
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); | |
e66f63fb | 72 | input = popen ("/bin/cat /tmp/tstpopen.tmp", "r"); |
28f540f4 RM |
73 | if (input == NULL) |
74 | { | |
e66f63fb | 75 | perror ("/tmp/tstpopen.tmp"); |
28f540f4 RM |
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 | ||
ec4b0518 UD |
83 | remove ("/tmp/tstpopen.tmp"); |
84 | ||
f43ce637 UD |
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 | ||
28f540f4 | 100 | puts (wstatus | rstatus ? "Test FAILED!" : "Test succeeded."); |
cf3141a5 | 101 | return (wstatus | rstatus); |
28f540f4 | 102 | } |