Back to index

glibc  2.9
tst-popen.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <wchar.h>
00023 
00024 static int
00025 do_test (void)
00026 {
00027   FILE *f = popen ("echo test", "r");
00028   int result = 0, ret;
00029   char *line = NULL;
00030   size_t len = 0;
00031 
00032   if (f == NULL)
00033     {
00034       printf ("popen failed %m");
00035       return 1;
00036     }
00037 
00038   /* POSIX says that pipe streams are byte-oriented.  */
00039   if (fwide (f, 0) >= 0)
00040     {
00041       puts ("popen did not return byte-oriented stream");
00042       result = 1;
00043     }
00044 
00045   if (getline (&line, &len, f) != 5)
00046     {
00047       puts ("could not read line from popen");
00048       result = 1;
00049     }
00050   else if (strcmp (line, "test\n") != 0)
00051     {
00052       printf ("read \"%s\"\n", line);
00053       result = 1;
00054     }
00055 
00056   if (getline (&line, &len, f) != -1)
00057     {
00058       puts ("second getline did not return -1");
00059       result = 1;
00060     }
00061 
00062   ret = pclose (f);
00063   if (ret != 0)
00064     {
00065       printf ("pclose returned %d\n", ret);
00066       result = 1;
00067     }
00068 
00069   return result;
00070 }
00071 
00072 #define TEST_FUNCTION do_test ()
00073 #include "../test-skeleton.c"