Back to index

glibc  2.9
tst-popen2.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <unistd.h>
00004 
00005 static int
00006 do_test (void)
00007 {
00008   int fd = dup (fileno (stdout));
00009   if (fd <= 1)
00010     {
00011       puts ("dup failed");
00012       return 1;
00013     }
00014 
00015   FILE *f1 = fdopen (fd, "w");
00016   if (f1 == NULL)
00017     {
00018       printf ("fdopen failed: %m\n");
00019       return 1;
00020     }
00021 
00022   fclose (stdout);
00023 
00024   FILE *f2 = popen ("echo test1", "r");
00025   if (f2 == NULL)
00026     {
00027       fprintf (f1, "1st popen failed: %m\n");
00028       return 1;
00029     }
00030   FILE *f3 = popen ("echo test2", "r");
00031   if (f2 == NULL || f3 == NULL)
00032     {
00033       fprintf (f1, "2nd popen failed: %m\n");
00034       return 1;
00035     }
00036 
00037   char *line = NULL;
00038   size_t len = 0;
00039   int result = 0;
00040   if (getline (&line, &len, f2) != 6)
00041     {
00042       fputs ("could not read line from 1st popen\n", f1);
00043       result = 1;
00044     }
00045   else if (strcmp (line, "test1\n") != 0)
00046     {
00047       fprintf (f1, "read \"%s\"\n", line);
00048       result = 1;
00049     }
00050 
00051   if (getline (&line, &len, f2) != -1)
00052     {
00053       fputs ("second getline did not return -1\n", f1);
00054       result = 1;
00055     }
00056 
00057   if (getline (&line, &len, f3) != 6)
00058     {
00059       fputs ("could not read line from 2nd popen\n", f1);
00060       result = 1;
00061     }
00062   else if (strcmp (line, "test2\n") != 0)
00063     {
00064       fprintf (f1, "read \"%s\"\n", line);
00065       result = 1;
00066     }
00067 
00068   if (getline (&line, &len, f3) != -1)
00069     {
00070       fputs ("second getline did not return -1\n", f1);
00071       result = 1;
00072     }
00073 
00074   int ret = pclose (f2);
00075   if (ret != 0)
00076     {
00077       fprintf (f1, "1st pclose returned %d\n", ret);
00078       result = 1;
00079     }
00080 
00081   ret = pclose (f3);
00082   if (ret != 0)
00083     {
00084       fprintf (f1, "2nd pclose returned %d\n", ret);
00085       result = 1;
00086     }
00087 
00088   return result;
00089 }
00090 
00091 #define TEST_FUNCTION do_test ()
00092 #include "../test-skeleton.c"