.
[glibc/history.git] / stdio-common / tst-popen2.c
blob0ab151c598e7c17edcd1e1d8e445fd32e5f00d0d
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
5 static int
6 do_test (void)
8 int fd = dup (fileno (stdout));
9 if (fd <= 1)
11 puts ("dup failed");
12 return 1;
15 FILE *f1 = fdopen (fd, "w");
16 if (f1 == NULL)
18 printf ("fdopen failed: %m\n");
19 return 1;
22 fclose (stdout);
24 FILE *f2 = popen ("echo test1", "r");
25 if (f2 == NULL)
27 fprintf (f1, "1st popen failed: %m\n");
28 return 1;
30 FILE *f3 = popen ("echo test2", "r");
31 if (f2 == NULL || f3 == NULL)
33 fprintf (f1, "2nd popen failed: %m\n");
34 return 1;
37 char *line = NULL;
38 size_t len = 0;
39 int result = 0;
40 if (getline (&line, &len, f2) != 6)
42 fputs ("could not read line from 1st popen\n", f1);
43 result = 1;
45 else if (strcmp (line, "test1\n") != 0)
47 fprintf (f1, "read \"%s\"\n", line);
48 result = 1;
51 if (getline (&line, &len, f2) != -1)
53 fputs ("second getline did not return -1\n", f1);
54 result = 1;
57 if (getline (&line, &len, f3) != 6)
59 fputs ("could not read line from 2nd popen\n", f1);
60 result = 1;
62 else if (strcmp (line, "test2\n") != 0)
64 fprintf (f1, "read \"%s\"\n", line);
65 result = 1;
68 if (getline (&line, &len, f3) != -1)
70 fputs ("second getline did not return -1\n", f1);
71 result = 1;
74 int ret = pclose (f2);
75 if (ret != 0)
77 fprintf (f1, "1st pclose returned %d\n", ret);
78 result = 1;
81 ret = pclose (f3);
82 if (ret != 0)
84 fprintf (f1, "2nd pclose returned %d\n", ret);
85 result = 1;
88 return result;
91 #define TEST_FUNCTION do_test ()
92 #include "../test-skeleton.c"