Blame stdio-common/test-popen.c

Packit 6c4009
/* Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
write_data (FILE *stream)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
  for (i=0; i<100; i++)
Packit 6c4009
    fprintf (stream, "%d\n", i);
Packit 6c4009
  if (ferror (stream))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "Output to stream failed.\n");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
read_data (FILE *stream)
Packit 6c4009
{
Packit 6c4009
  int i, j;
Packit 6c4009
Packit 6c4009
  for (i=0; i<100; i++)
Packit 6c4009
    {
Packit 6c4009
      if (fscanf (stream, "%d\n", &j) != 1 || j != i)
Packit 6c4009
	{
Packit 6c4009
	  if (ferror (stream))
Packit 6c4009
	    perror ("fscanf");
Packit 6c4009
	  puts ("Test FAILED!");
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  FILE *output, *input;
Packit 6c4009
  int wstatus, rstatus;
Packit 6c4009
Packit 6c4009
  /* We must remove this entry to assure the `cat' binary does not use
Packit 6c4009
     the perhaps incompatible new shared libraries.  */
Packit 6c4009
  unsetenv ("LD_LIBRARY_PATH");
Packit 6c4009
Packit 6c4009
  output = popen ("/bin/cat >" OBJPFX "tstpopen.tmp", "w");
Packit 6c4009
  if (output == NULL)
Packit 6c4009
    {
Packit 6c4009
      perror ("popen");
Packit 6c4009
      puts ("Test FAILED!");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  write_data (output);
Packit 6c4009
  wstatus = pclose (output);
Packit 6c4009
  printf ("writing pclose returned %d\n", wstatus);
Packit 6c4009
  input = popen ("/bin/cat " OBJPFX "tstpopen.tmp", "r");
Packit 6c4009
  if (input == NULL)
Packit 6c4009
    {
Packit 6c4009
      perror (OBJPFX "tstpopen.tmp");
Packit 6c4009
      puts ("Test FAILED!");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  read_data (input);
Packit 6c4009
  rstatus = pclose (input);
Packit 6c4009
  printf ("reading pclose returned %d\n", rstatus);
Packit 6c4009
Packit 6c4009
  remove (OBJPFX "tstpopen.tmp");
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  output = popen ("/bin/cat", "m");
Packit 6c4009
  if (output != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("popen called with illegal mode does not return NULL");
Packit 6c4009
      puts ("Test FAILED!");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      puts ("popen called with illegal mode does not set errno to EINVAL");
Packit 6c4009
      puts ("Test FAILED!");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  puts (wstatus | rstatus  ? "Test FAILED!" : "Test succeeded.");
Packit 6c4009
  return (wstatus | rstatus);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"