Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!sri-spam!sri-unix!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: stdio error detection Message-ID: <289@cresswell.quintus.UUCP> Date: Thu, 26-Nov-87 04:29:13 EST Article-I.D.: cresswel.289 Posted: Thu Nov 26 04:29:13 1987 Date-Received: Sun, 29-Nov-87 15:59:42 EST Organization: Quintus Computer Systems, Mountain View, CA Lines: 33 Keywords: errno fclose fopen stdio errors I am trying to make my programs as bullet-proof as possible. One thing I am worried about is fclose(). - Under what conditions (other than not-open file or invalid address &c) can this return an error result? - How can I tell what went wrong? - What, if anything, can I *do* about it? Another thing is fopen(). How can I find out whether a NULL result means - fopen can't obtain another file descriptor, or - fopen can't malloc() another struct _iob, or - fopen is not allowed to open the file PORTABLY? At the moment, if I really want to bullet-proof my code, I am reduced to - checking access() to see if the file exists - malloc()ing a FILE block and freeing it to ensure that there is room - then calling fopen() which is so painful that I usually don't do it, and just say: cannot (read|write|append) Yes, I have read the manual. I have read the V7, 4.2BSD, V.2, and SunOS 3.2 manual section 2 and 3 from divider to divider, also the SVID and volume 1 of the SAS Lattice C manual for IBM MVS & VM/CMS AND the October '86 draft of the ANSI C standard. Someone in this newsgroup earlier suggested checking errno. If I remember correctly, his code had the form if (--stdio function fails--) { perror(--suitable argument--); ... This doesn't seem like a good idea. The effect of the stdio functions on errno is totally undefined in all cases; errno is only defined when a *system call* fails (not when it succeeds). It would be really nice if the stdio functions were defined to set errno (any ANSI C people care to comment?). Of course there is no official errno code which means that "malloc() ran out" -- another nasty gap.