grep replacement

Chris Torek chris at mimsy.UUCP
Tue Jun 14 13:54:41 AEST 1988


In article <44370 at beno.seismo.CSS.GOV> keith at seismo.CSS.GOV
[at seismo?!?] (Keith Bostic) writes:
>    -- The next full release of BSD will contain "/dev/stdin" and friends.
>	It is not part of the 4.3-tahoe release because it requires changes
>	to stdio.

Well, only because

	freopen("/dev/stdin", "r", stdin)

unexpectedly fails: it closes fd 0 before attempting to open /dev/stdin,
which means that stdin is gone before it can grab it again.  When I
`fixed' this here it broke /usr/ucb/head and I had to fix the fix!

The sequence needed is messy:

	old = fileno(fp);
	new = open(...);
	if (new < 0) {
		close(old);	/* maybe it was EMFILE */
		new = open(...);/* (could test errno too) */
		if (new < 0)
			return error;
	}
	if (new != old) {
		if (dup2(new, old) >= 0)	/* move it back */
			close(new);
		else {
			close(old);
			fileno(fp) = new;
		}
	}

Not using dup2 means that freopen(stderr) might make fileno(stderr)
something other than 2, which breaks at least perror().
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list