Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 2 May 1996 01:58:26 -0700 (PDT)
From:      Josh MacDonald <jmacd@deceit.xcf.berkeley.edu>
To:        freebsd-hackers@freebsd.org
Subject:   stdio problem
Message-ID:  <199605020858.BAA05189@deceit.xcf.berkeley.edu>

next in thread | raw e-mail | index | archive | help

In the following code:

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

int main(int argc, char** argv)
{
  if(argc != 2) {
    fprintf(stderr, "no\n");
    exit(1);
  }

  close(0);
  if(!freopen(argv[1], "r", stdin)) {
    perror("freopen");
    exit(1);
  }
  while(fgetc(stdin) != EOF) { }
  if(ferror(stdin)) {
    perror("stdin");
    exit(1);
  }
  printf("its okay\n");
  exit(0);
}

FreeBSD exits "stdin: Bad file descriptor"
Linux, SunOS, ULTRIX, PTX, Solaris, HP-UX, IRIX, etc... all report
"its okay".  

I am wondering if either FreeBSD, the rest of the OS's, or no one
is "wrong".  The pracical application here is that if you close 0
and then exec GNU diff3 you'll encounter this problem.  Is it legal
to close your standard input and then fork/exec other programs?  
I was under the impression that is was.  Am I defying the laws of
physics if I close(0) and then fork/exec a program expecting to 
maybe use its standard input with freopen?  I don't think so.

-josh



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199605020858.BAA05189>