Update copyright dates with scripts/update-copyrights.
[glibc.git] / stdio-common / test-popen.c
blob66613888aed3591365a7900641c353ab93c0e597
1 /* Copyright (C) 1997-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
22 static void
23 write_data (FILE *stream)
25 int i;
26 for (i=0; i<100; i++)
27 fprintf (stream, "%d\n", i);
28 if (ferror (stream))
30 fprintf (stderr, "Output to stream failed.\n");
31 exit (1);
35 static void
36 read_data (FILE *stream)
38 int i, j;
40 for (i=0; i<100; i++)
42 if (fscanf (stream, "%d\n", &j) != 1 || j != i)
44 if (ferror (stream))
45 perror ("fscanf");
46 puts ("Test FAILED!");
47 exit (1);
52 static int
53 do_test (void)
55 FILE *output, *input;
56 int wstatus, rstatus;
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");
62 output = popen ("/bin/cat >/tmp/tstpopen.tmp", "w");
63 if (output == NULL)
65 perror ("popen");
66 puts ("Test FAILED!");
67 exit (1);
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)
75 perror ("/tmp/tstpopen.tmp");
76 puts ("Test FAILED!");
77 exit (1);
79 read_data (input);
80 rstatus = pclose (input);
81 printf ("reading pclose returned %d\n", rstatus);
83 remove ("/tmp/tstpopen.tmp");
85 errno = 0;
86 output = popen ("/bin/cat", "m");
87 if (output != NULL)
89 puts ("popen called with illegal mode does not return NULL");
90 puts ("Test FAILED!");
91 exit (1);
93 if (errno != EINVAL)
95 puts ("popen called with illegal mode does not set errno to EINVAL");
96 puts ("Test FAILED!");
97 exit (1);
100 puts (wstatus | rstatus ? "Test FAILED!" : "Test succeeded.");
101 return (wstatus | rstatus);
104 #define TEST_FUNCTION do_test ()
105 #include "../test-skeleton.c"