root/maint/gnulib/tests/test-popen-safer.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of opening a subcommand stream.
   2    Copyright (C) 2009-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
  18 
  19 #include <config.h>
  20 
  21 /* Specification.  */
  22 #include "stdio--.h"
  23 
  24 /* Helpers.  */
  25 #include <sys/wait.h>
  26 #include <unistd.h>
  27 
  28 /* This test intentionally closes stderr.  So, we arrange to have fd 10
  29    (outside the range of interesting fd's during the test) set up to
  30    duplicate the original stderr.  */
  31 
  32 #define BACKUP_STDERR_FILENO 10
  33 #define ASSERT_STREAM myerr
  34 #include "macros.h"
  35 
  36 static FILE *myerr;
  37 
  38 int
  39 main (int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41   FILE *fp;
  42   int status;
  43 
  44   /* We close fd 2 later, so save it in fd 10.  */
  45   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
  46       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
  47     return 2;
  48 
  49   ASSERT (fp = popen ("exit 0", "r"));
  50   ASSERT (STDERR_FILENO < fileno (fp));
  51   status = pclose (fp);
  52   ASSERT (WIFEXITED (status));
  53   ASSERT (!WEXITSTATUS (status));
  54 
  55   ASSERT (fclose (stdin) == 0);
  56   ASSERT (fp = popen ("exit 0", "r"));
  57   ASSERT (STDERR_FILENO < fileno (fp));
  58   status = pclose (fp);
  59   ASSERT (WIFEXITED (status));
  60   ASSERT (!WEXITSTATUS (status));
  61 
  62   ASSERT (fclose (stdout) == 0);
  63   ASSERT (fp = popen ("exit 0", "r"));
  64   ASSERT (STDERR_FILENO < fileno (fp));
  65   status = pclose (fp);
  66   ASSERT (WIFEXITED (status));
  67   ASSERT (!WEXITSTATUS (status));
  68 
  69   ASSERT (fclose (stderr) == 0);
  70   ASSERT (fp = popen ("exit 0", "r"));
  71   ASSERT (STDERR_FILENO < fileno (fp));
  72   status = pclose (fp);
  73   ASSERT (WIFEXITED (status));
  74   ASSERT (!WEXITSTATUS (status));
  75   return 0;
  76 }

/* [previous][next][first][last][top][bottom][index][help] */