Back to index

glibc  2.9
Functions
test-popen.c File Reference
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

static void write_data (FILE *stream)
static void read_data (FILE *stream)
int main (void)

Function Documentation

int main ( void  )

Definition at line 54 of file test-popen.c.

{
  FILE *output, *input;
  int wstatus, rstatus;

  /* We must remove this entry to assure the `cat' binary does not use
     the perhaps incompatible new shared libraries.  */
  unsetenv ("LD_LIBRARY_PATH");

  output = popen ("/bin/cat >/tmp/tstpopen.tmp", "w");
  if (output == NULL)
    {
      perror ("popen");
      puts ("Test FAILED!");
      exit (1);
    }
  write_data (output);
  wstatus = pclose (output);
  printf ("writing pclose returned %d\n", wstatus);
  input = popen ("/bin/cat /tmp/tstpopen.tmp", "r");
  if (input == NULL)
    {
      perror ("/tmp/tstpopen.tmp");
      puts ("Test FAILED!");
      exit (1);
    }
  read_data (input);
  rstatus = pclose (input);
  printf ("reading pclose returned %d\n", rstatus);

  remove ("/tmp/tstpopen.tmp");

  errno = 0;
  output = popen ("/bin/cat", "m");
  if (output != NULL)
    {
      puts ("popen called with illegal mode does not return NULL");
      puts ("Test FAILED!");
      exit (1);
    }
  if (errno != EINVAL)
    {
      puts ("popen called with illegal mode does not set errno to EINVAL");
      puts ("Test FAILED!");
      exit (1);
    }

  puts (wstatus | rstatus  ? "Test FAILED!" : "Test succeeded.");
  return (wstatus | rstatus);
}

Here is the call graph for this function:

static void read_data ( FILE stream) [static]

Definition at line 37 of file test-popen.c.

{
  int i, j;

  for (i=0; i<100; i++)
    {
      if (fscanf (stream, "%d\n", &j) != 1 || j != i)
       {
         if (ferror (stream))
           perror ("fscanf");
         puts ("Test FAILED!");
         exit (1);
       }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void write_data ( FILE stream) [static]

Definition at line 24 of file test-popen.c.

{
  int i;
  for (i=0; i<100; i++)
    fprintf (stream, "%d\n", i);
  if (ferror (stream))
    {
      fprintf (stderr, "Output to stream failed.\n");
      exit (1);
    }
}

Here is the call graph for this function: