chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / stdio-common / test-popen.c
1 /* Copyright (C) 1997, 1998, 2000 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, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 static void
24 write_data (FILE *stream)
25 {
26   int i;
27   for (i=0; i<100; i++)
28     fprintf (stream, "%d\n", i);
29   if (ferror (stream))
30     {
31       fprintf (stderr, "Output to stream failed.\n");
32       exit (1);
33     }
34 }
35
36 static void
37 read_data (FILE *stream)
38 {
39   int i, j;
40
41   for (i=0; i<100; i++)
42     {
43       if (fscanf (stream, "%d\n", &j) != 1 || j != i)
44         {
45           if (ferror (stream))
46             perror ("fscanf");
47           puts ("Test FAILED!");
48           exit (1);
49         }
50     }
51 }
52
53 int
54 main (void)
55 {
56   FILE *output, *input;
57   int wstatus, rstatus;
58
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");
62
63   output = popen ("/bin/cat >/tmp/tstpopen.tmp", "w");
64   if (output == NULL)
65     {
66       perror ("popen");
67       puts ("Test FAILED!");
68       exit (1);
69     }
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)
75     {
76       perror ("/tmp/tstpopen.tmp");
77       puts ("Test FAILED!");
78       exit (1);
79     }
80   read_data (input);
81   rstatus = pclose (input);
82   printf ("reading pclose returned %d\n", rstatus);
83
84   remove ("/tmp/tstpopen.tmp");
85
86   errno = 0;
87   output = popen ("/bin/cat", "m");
88   if (output != NULL)
89     {
90       puts ("popen called with illegal mode does not return NULL");
91       puts ("Test FAILED!");
92       exit (1);
93     }
94   if (errno != EINVAL)
95     {
96       puts ("popen called with illegal mode does not set errno to EINVAL");
97       puts ("Test FAILED!");
98       exit (1);
99     }
100
101   puts (wstatus | rstatus  ? "Test FAILED!" : "Test succeeded.");
102   return (wstatus | rstatus);
103 }