Back to index

glibc  2.9
test-popen.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 
00023 static void
00024 write_data (FILE *stream)
00025 {
00026   int i;
00027   for (i=0; i<100; i++)
00028     fprintf (stream, "%d\n", i);
00029   if (ferror (stream))
00030     {
00031       fprintf (stderr, "Output to stream failed.\n");
00032       exit (1);
00033     }
00034 }
00035 
00036 static void
00037 read_data (FILE *stream)
00038 {
00039   int i, j;
00040 
00041   for (i=0; i<100; i++)
00042     {
00043       if (fscanf (stream, "%d\n", &j) != 1 || j != i)
00044        {
00045          if (ferror (stream))
00046            perror ("fscanf");
00047          puts ("Test FAILED!");
00048          exit (1);
00049        }
00050     }
00051 }
00052 
00053 int
00054 main (void)
00055 {
00056   FILE *output, *input;
00057   int wstatus, rstatus;
00058 
00059   /* We must remove this entry to assure the `cat' binary does not use
00060      the perhaps incompatible new shared libraries.  */
00061   unsetenv ("LD_LIBRARY_PATH");
00062 
00063   output = popen ("/bin/cat >/tmp/tstpopen.tmp", "w");
00064   if (output == NULL)
00065     {
00066       perror ("popen");
00067       puts ("Test FAILED!");
00068       exit (1);
00069     }
00070   write_data (output);
00071   wstatus = pclose (output);
00072   printf ("writing pclose returned %d\n", wstatus);
00073   input = popen ("/bin/cat /tmp/tstpopen.tmp", "r");
00074   if (input == NULL)
00075     {
00076       perror ("/tmp/tstpopen.tmp");
00077       puts ("Test FAILED!");
00078       exit (1);
00079     }
00080   read_data (input);
00081   rstatus = pclose (input);
00082   printf ("reading pclose returned %d\n", rstatus);
00083 
00084   remove ("/tmp/tstpopen.tmp");
00085 
00086   errno = 0;
00087   output = popen ("/bin/cat", "m");
00088   if (output != NULL)
00089     {
00090       puts ("popen called with illegal mode does not return NULL");
00091       puts ("Test FAILED!");
00092       exit (1);
00093     }
00094   if (errno != EINVAL)
00095     {
00096       puts ("popen called with illegal mode does not set errno to EINVAL");
00097       puts ("Test FAILED!");
00098       exit (1);
00099     }
00100 
00101   puts (wstatus | rstatus  ? "Test FAILED!" : "Test succeeded.");
00102   return (wstatus | rstatus);
00103 }