Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site onfcanim.UUCP Path: utzoo!watmath!watnot!watcgl!onfcanim!dave From: dave@onfcanim.UUCP (Dave Martindale) Newsgroups: net.unix,net.unix-wizards Subject: Re: Not a typewriter Message-ID: <14886@onfcanim.UUCP> Date: Fri, 20-Jun-86 00:07:25 EDT Article-I.D.: onfcanim.14886 Posted: Fri Jun 20 00:07:25 1986 Date-Received: Sat, 21-Jun-86 08:48:33 EDT References: <1683@homxb.UUCP> <542@codas.ATT.UUCP> <717@axis.UUCP> Reply-To: dave@onfcanim.UUCP (Dave Martindale) Organization: ONF, Montreal Lines: 50 Xref: watmath net.unix:8269 net.unix-wizards:18507 Summary: In article <717@axis.UUCP> philip@axis.UUCP (Philip Peake) writes: >In article <542@codas.ATT.UUCP> mikel@codas.UUCP writes: >>> What does the sys_errlist message 'Not a typewriter' *really* mean?? >> > >This is true. *However*, if you look at the documentation *carefully*, you >will find that it says that errno is only valid if the system call preceeding >it returned an error value. > >I agree that this is a disgusting state of affairs, and I have recently >had problems with a code segment as follows: > > > fp = fopen(.....); > errno = 0; > .. > .. > loop: fwrite(.....); > if (errno != 0) /* a write problem occured */ > { > ... > ... > } > .. > >This bombed out complaining about "Not a teletype", there was in fact, >no problem at all. The code came from a BSD4.? system, the machine on >which the problems occured runs S5.2. Right. As the manual says, errno is meaningful only after a system call has failed. If you want to check for an error in the fwrite, either check fwrite's return value, or use ferror(). Errno is meaningful at the level of doing system calls - read, write, etc. Fwrite is a higher-level function, and has its own ways of indicating errors. In fact, in this particular case, errno is being set as a side effect of the fwrite. The first time you do I/O on a stdio unit, the package does an ioctl("get terminal characteristics") on the file descriptor. If the ioctl succeeds, stdio knows that it should line-buffer or character-buffer that stream. If the ioctl fails (and when it does, it will set errno to "not a typewriter"), stdio will use block buffering appropriate to a file. Because of this, "not a typewriter" is the error message you will almost always see if you look at errno when there hasn't been a real error, since almost every program does I/O through stdio fairly early. The moral is: you *can't* reliably tell whether there has been an error by checking an external variable every once in a while. You have to check the return value of everyting that could possibly go wrong.