This is the mail archive of the glibc-bugs@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug manual/670] New: system() documentation is incomplete (for standard streams)


The ISO C99 standard requires that the behavior of the system() function shall
be documented. The manual does not say anything about the standard streams, and
thus is incomplete, IMHO. In particular, a documentation concerning the standard
streams would be important since the behavior is not intuitive, as shown on the
following example.

Consider the following program, called "stdstreams":

#include <stdio.h>
#include <stdlib.h>

static void out (char *s)
{
  int ret;
  ret = printf ("%s\n", s);
  if (ret < 0)
    {
      fprintf (stderr, "printf error %s: %d\n", s, ret);
      exit (EXIT_FAILURE);
    }
  ret = fflush (stdout);
  if (ret)
    {
      fprintf (stderr, "fflush error %s: %d\n", s, ret);
      exit (EXIT_FAILURE);
    }
}

int main (int argc, char **argv)
{
  if (argc == 1)
    {
      out ("OK 2");
    }
  else
    {
      int ret;
      fclose (stdin);
      if (freopen ("file.out", "w", stdout) == NULL)
        {
          fprintf (stderr, "freopen error\n");
          exit (EXIT_FAILURE);
        }
      out ("OK 1");
      ret = system ("stdstreams");
      fprintf (stderr, "system returned %d\n", ret);
    }
  return 0;
}

With this program, I get:

dixsept:~/wd/src> ./stdstreams 1
fflush error OK 2: -1
system returned 256
dixsept:~/wd/src> cat file.out
OK 1
dixsept:~/wd/src>

Basically, what is performed is:
1. stdin is closed with fclose().
2. stdout is redirected to a file "file.out" with freopen().
3. A string is sent to stdout.
4. system() is called: the child sends a string to its standard output stream.
It appeared that this output failed.

The observed behavior is not surprising when one guesses what happened with the
file descriptors: the child's stdout is associated with fd 1, which is no longer
open since the smallest free fd (0) was chosen for stdout by freopen(). But this
is just a guess and the observed behavior could have been something else.

Intuitively, one could have expected that the string output in step 4 be sent to
file.out too.

-- 
           Summary: system() documentation is incomplete (for standard
                    streams)
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: normal
          Priority: P2
         Component: manual
        AssignedTo: roland at gnu dot org
        ReportedBy: vincent+libc at vinc17 dot org
                CC: glibc-bugs at sources dot redhat dot com


http://sources.redhat.com/bugzilla/show_bug.cgi?id=670

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]