Updated to fedora-glibc-20050708T0811
[glibc.git] / stdio-common / test-popen.c
blob5809c7fef6f886ec666b4db26e67cbef47e5d416
1 /* Copyright (C) 1997, 1998, 2000 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
23 static void
24 write_data (FILE *stream)
26 int i;
27 for (i=0; i<100; i++)
28 fprintf (stream, "%d\n", i);
29 if (ferror (stream))
31 fprintf (stderr, "Output to stream failed.\n");
32 exit (1);
36 static void
37 read_data (FILE *stream)
39 int i, j;
41 for (i=0; i<100; i++)
43 if (fscanf (stream, "%d\n", &j) != 1 || j != i)
45 if (ferror (stream))
46 perror ("fscanf");
47 puts ("Test FAILED!");
48 exit (1);
53 int
54 main (void)
56 FILE *output, *input;
57 int wstatus, rstatus;
59 /* We must remove this entry to assure the `cat' binary does not use
60 the perhaps incompatible new shared libraries. */
61 unsetenv ("LD_LIBRARY_PATH");
63 output = popen ("/bin/cat >/tmp/tstpopen.tmp", "w");
64 if (output == NULL)
66 perror ("popen");
67 puts ("Test FAILED!");
68 exit (1);
70 write_data (output);
71 wstatus = pclose (output);
72 printf ("writing pclose returned %d\n", wstatus);
73 input = popen ("/bin/cat /tmp/tstpopen.tmp", "r");
74 if (input == NULL)
76 perror ("/tmp/tstpopen.tmp");
77 puts ("Test FAILED!");
78 exit (1);
80 read_data (input);
81 rstatus = pclose (input);
82 printf ("reading pclose returned %d\n", rstatus);
84 remove ("/tmp/tstpopen.tmp");
86 errno = 0;
87 output = popen ("/bin/cat", "m");
88 if (output != NULL)
90 puts ("popen called with illegal mode does not return NULL");
91 puts ("Test FAILED!");
92 exit (1);
94 if (errno != EINVAL)
96 puts ("popen called with illegal mode does not set errno to EINVAL");
97 puts ("Test FAILED!");
98 exit (1);
101 puts (wstatus | rstatus ? "Test FAILED!" : "Test succeeded.");
102 return (wstatus | rstatus);