update from main archive 970203
[glibc/history.git] / stdio-common / test-popen.c
bloba17606e5031389c05f118fd68c0c6473d9d47639
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 >/tmp/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 /tmp/tstpopen.tmp", "r");
51 if (input == NULL)
53 perror ("/tmp/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 remove ("/tmp/tstpopen.tmp");
63 puts (wstatus | rstatus ? "Test FAILED!" : "Test succeeded.");
64 exit (wstatus | rstatus);