[comp.unix.questions] preconnected file descriptors

ok@quintus.UUCP (Richard A. O'Keefe) (12/23/87)

I was under the impression that if you had a plain Bourne shell
which had been invoked as part of logging in, and you ran a program
without specifying any I/O redirection, you would have precisely
three file descriptors already connected:	       ^^^^^^^^^
	0	- stdin
	1	- stdout
	2	- stderr
A particular program we have which is part of a larger suite expects
to be called with fds 3 and 4 connected (to pipes, as it happens).
Part of the testing procedure when we put this thing on a new system
is to run this program on its own, e.g.
	$ component 3<test-script 4>test-output
	$ diff test-output expected-output
At the moment we are putting this thing up on an Apollo running SR9.5,
and the tester didn't realise this thing had arguments, and ran
	$ component
with no I/O redirection.  We were surprised to see output coming out on
the terminal that was intended for fd 4.

This can be reproduced by running the following script:
	#!/bin/sh
	cat >four.c <<'EOF'
	main()
	    {
		static char message[] = "You should NOT see this!\n";
		int rc;
		extern int errno;
		rc = write(4, message, -1+sizeof message);
		printf("sizeof message = %d, rc = %d, errno = %d\n",
			sizeof message, rc, errno);
		exit(0);
	    }
	EOF
	cc -o four four.c
	echo This one IS supposed to write the message
	./four 3</dev/null 4>/dev/tty
	echo This one is NOT supposed to write the message
	./four
The C shell acts as expected.  On the other hand, it doesn't permit
the redirection we really want.

The question:	are file descriptors other than 0,1,2 MEANT to be
		preconnected in the Apollo system, and if so, which,
		and to what?
		if we close 3 and 4, will something break?
		are there other "UNIX" systems which do this?

gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/06/88)

In article <491@cresswell.quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) writes:
>		if we close 3 and 4, will something break?
>		are there other "UNIX" systems which do this?

I seem to recall that on Ninth Edition UNIX, fd #3 is open to the
controlling terminal by convention.  But, hey, if you want to close
an fd and re-use it for other purposes, feel free.