Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 02 May 1996 11:14:25 -0700
From:      Josh MacDonald <jmacd@CS.Berkeley.EDU>
To:        Poul-Henning Kamp <phk@critter.tfs.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: stdio problem 
Message-ID:  <199605021814.LAA16901@paris.CS.Berkeley.EDU>
In-Reply-To: Your message of "Thu, 02 May 1996 14:11:36 -0000." <3259.831046296@critter.tfs.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
> >   close(0);
> >   if(!freopen(argv[1], "r", stdin)) {
> 
> doing a "close(0)" is bound to confuse the stdio library.
> don't do that.
> freopen will close the filedesriptor and thud DTRT.
> 
> --
> Poul-Henning Kamp           | phk@FreeBSD.ORG       FreeBSD Core-team.
> http://www.freebsd.org/~phk | phk@login.dknet.dk    Private mailbox.
> whois: [PHK]                | phk@ref.tfs.com       TRW Financial Systems, In
>c.
> Future will arrive by its own means, progress not so.

The comment below disagrees.  I was kind of hoping someone familiar
with this code and ANSI and all that could explain or fix this.  This
is from the top of lib/libc/stdio/freopen.c

One such program which depends on being able to freopen a descriptor
that wasn't originally open is GNU diff3.  Should I report that as a
bug to its maintainer?  Or, am I Wrong to exec diff3 without an open
standard input.

Who is wrong?  Someone is.

-josh

/*
 * Re-direct an existing, open (probably) file to some other file.
 * ANSI is written such that the original file gets closed if at
 * all possible, no matter what.
 */
FILE *
freopen(file, mode, fp)
	const char *file, *mode;
	register FILE *fp;
{
	register int f;
	int flags, isopen, oflags, sverrno, wantfd;

	if ((flags = __sflags(mode, &oflags)) == 0) {
		(void) fclose(fp);
		return (NULL);
	}

	if (!__sdidinit)
		__sinit();

	/*
	 * There are actually programs that depend on being able to "freopen"
	 * descriptors that weren't originally open.  Keep this from breaking.
	 * Remember whether the stream was open to begin with, and which file
	 * descriptor (if any) was associated with it.  If it was attached to
	 * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
	 * should work.  This is unnecessary if it was not a Unix file.
	 */




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