Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!sri-unix!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: closing stdout Message-ID: <442@cresswell.quintus.UUCP> Date: Mon, 7-Dec-87 01:16:44 EST Article-I.D.: cresswel.442 Posted: Mon Dec 7 01:16:44 1987 Date-Received: Sat, 12-Dec-87 07:05:27 EST Organization: Quintus Computer Systems, Mountain View, CA Lines: 53 Keywords: Yes it IS a buggy library I thought my message made it clear that I expected fclose(stdout) to work, but apparently not. Rich $alz hastened to reassure the world that any thought that it might not is "NONSENSE!" and that if it does give trouble, you have a severely broken C library. There are some interesting points about this. One is the wording in the October '86 ANSI C Draft: "Due to weak implementations of the standard I/O library ...". There are other points like "On some operating systems it is difficult, or impossible, to create a file unless something is written to the file. A maximally portable program which relies on a file being created must write something to the associated stream before closing it." (So yes, there could be a problem with fclose() when it DIDN'T write anything...) I think the main point is that I can find nothing in the ANSI draft nor in any of the C manuals I have checked which explicitly says that you can close the standard streams. True, there is nothing that says you can't, but this is the kind of thing that is overlooked. There's an old joke with the punch-line "We've already established what you are, madam. Now we're just haggling over the price." I'm afraid stdio is like that. Here's my favourite example: cat <<'EOF' >foo.c #includemain() { extern int errno; int result; result = getchar(); errno = 0; result = putc(result, stdin); printf("result = %d, errno = %d\n", result, errno); } EOF cc -o foo foo.c ./foo <<'EOF' X12356 EOF On every UNIX system where I've tried this (V7, 4.2, V.2, V.3) the output has been result = 88, errno = 0 Now putc() is supposed to return EOF (-1) on error. Writing to an INPUT stream isn't an error? The bug is that depending on where you are in the buffer, putc() MIGHT notice the mistake, but it usually won't. With this sort of family history, a wee bit of suspicion about fclose() is not unwarranted. There's no point in me getting on the phone to AT&T or SUN about this; the bug is a pretty fundamental one in the UNIX stdio implementation, and they'd have to rewrite most of the macros as well as _filbuf and _flsbuf (no I haven't got sources, but I was able to predict the bug from looking at /usr/include/stdio.h, and it has never gone away).