Reading from stderr

Lloyd Kremer kremer at cs.odu.edu
Fri Apr 21 02:47:53 AEST 1989



In article <330 at hcr.UUCP> john at hcr.UUCP (John R. MacMillan) writes:

>In article <1094 at vsi.COM> friedl at vsi.UUCP writes:
>|In article <508 at visdc.UUCP>, jiii at visdc.UUCP (John E Van Deusen III) writes:
>|> Since file
>|> descriptors 0, 1, and 2 are all opened with oflag set to O_RDWR, ...
>|
>|An expansion here: stdin, stdout, and stderr are not opened independently.
>|the terminal device is opened and the resulting fildes is dup-ed twice
>|to yield stdout and stderr.
>
>But this isn't required of stdio, is it?  I used systems where stderr
>is write-only, and programs which read from it had to be fixed.


I think there is a confusion here between high level I/O (stdin, stdout,
stderr) and low level I/O (file descriptors 0, 1, 2).

In any UNIX variant I have used, I have found that the previous posters
were correct in saying that descriptors 0, 1, and 2 are originally created
by the sequence

	open(..., O_RDWR);  /* returns first available descriptor, 0 */
	dup(0);             /* returns first available descriptor, 1 */
	dup(0);             /* returns first available descriptor, 2 */

hence,

	read(2, ..., ...);

should work.  However, when the stdio FILEs are initialized, stdin is set for
read-only (_IOREAD), stdout is set for write-only (_IOWRT), and stderr is
set for write-only with no buffering (_IOWRT|_IONBF); hence

	getc(stderr);

would not work.  So it depends on the level of I/O you are using.

You can check your own system's stdio setup with this short program:

	#include <stdio.h>

	main()
	{
		int flag[3];

		/* gotta get these before doing any stdio */
		flag[0] = stdio->_flag;
		flag[1] = stdout->_flag;
		flag[2] = stderr->_flag;
		printf("stdin  is \\%03o\n", flag[0]);
		printf("stdout is \\%03o\n", flag[1]);
		printf("stderr is \\%03o\n", flag[2]);
		return(0);
	}

and comparing the results to the flag values #define'd in <stdio.h>.

As for absolute guarantees, "It works for me!".  :-)

-- 
					Lloyd Kremer
					Brooks Financial Systems
					...!uunet!xanth!brooks!lloyd
					Have terminal...will hack!



More information about the Comp.unix.wizards mailing list