initial import
[glibc.git] / stdio / test-popen.c
blobdf6138b76f5011acd0d8c88fa6c92da0f72aaeaa
1 #include <ansidecl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 void
6 DEFUN(write_data, (stream), FILE *stream)
8 int i;
9 for (i=0; i<100; i++)
10 fprintf (stream, "%d\n", i);
11 if (ferror (stream)) {
12 fprintf (stderr, "Output to stream failed.\n");
13 exit (1);
17 void
18 DEFUN(read_data, (stream), FILE *stream)
20 int i, j;
22 for (i=0; i<100; i++)
24 if (fscanf (stream, "%d\n", &j) != 1 || j != i)
26 if (ferror (stream))
27 perror ("fscanf");
28 puts ("Test FAILED!");
29 exit (1);
34 int
35 DEFUN_VOID(main)
37 FILE *output, *input;
38 int wstatus, rstatus;
40 output = popen ("/bin/cat >tstpopen.tmp", "w");
41 if (output == NULL)
43 perror ("popen");
44 puts ("Test FAILED!");
45 exit (1);
47 write_data (output);
48 wstatus = pclose (output);
49 printf ("writing pclose returned %d\n", wstatus);
50 input = popen ("/bin/cat tstpopen.tmp", "r");
51 if (input == NULL)
53 perror ("tstpopen.tmp");
54 puts ("Test FAILED!");
55 exit (1);
57 read_data (input);
58 rstatus = pclose (input);
59 printf ("reading pclose returned %d\n", rstatus);
61 puts (wstatus | rstatus ? "Test FAILED!" : "Test succeeded.");
62 exit (wstatus | rstatus);