root/maint/gnulib/tests/test-popen.h

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

INCLUDED FROM


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> and a form of <stdio.h> first.  */
  20 
  21 /* Helpers.  */
  22 #include <stdlib.h>
  23 #include <string.h>
  24 #include <sys/wait.h>
  25 #include <unistd.h>
  26 
  27 #include "macros.h"
  28 
  29 int
  30 main (int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  31 {
  32   size_t len;
  33   char *cmd;
  34   int i;
  35 
  36   /* Children - use the pipe.  */
  37   if (argc > 1)
  38     {
  39       if (*argv[1] == 'r') /* Parent is reading, so we write.  */
  40         ASSERT (putchar ('c') == 'c');
  41       else /* Parent is writing, so we read.  */
  42         ASSERT (getchar () == 'p');
  43       /* Test that parent can read non-zero status.  */
  44       return 42;
  45     }
  46 
  47   /* Parent - create read and write child, once under normal
  48      circumstances and once with stdin and stdout closed.  */
  49   len = strlen (argv[0]);
  50   cmd = malloc (len + 3); /* Adding " r" and NUL.  */
  51   ASSERT (cmd);
  52   /* We count on argv[0] not containing any shell metacharacters.  */
  53   strcpy (cmd, argv[0]);
  54   cmd[len] = ' ';
  55   cmd[len + 2] = '\0';
  56   for (i = 0; i < 2; i++)
  57     {
  58       FILE *child;
  59       int status;
  60 
  61       if (i)
  62         {
  63           ASSERT (fclose (stdin) == 0);
  64           ASSERT (fclose (stdout) == 0);
  65         }
  66 
  67       cmd[len + 1] = 'r';
  68       ASSERT (child = popen (cmd, "r"));
  69       ASSERT (fgetc (child) == 'c');
  70       status = pclose (child);
  71       ASSERT (WIFEXITED (status));
  72       ASSERT (WEXITSTATUS (status) == 42);
  73       if (i)
  74         {
  75           ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
  76           ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
  77         }
  78 
  79       cmd[len + 1] = 'w';
  80       ASSERT (child = popen (cmd, "w"));
  81       ASSERT (fputc ('p', child) == 'p');
  82       status = pclose (child);
  83       ASSERT (WIFEXITED (status));
  84       ASSERT (WEXITSTATUS (status) == 42);
  85       if (i)
  86         {
  87           ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
  88           ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
  89         }
  90     }
  91   free (cmd);
  92   return 0;
  93 }

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