r+ on fopen.

jeff%aids-unix at sri-unix.UUCP jeff%aids-unix at sri-unix.UUCP
Tue Sep 20 05:02:22 AEST 1983


From:  Jeff Dean <jeff at aids-unix>

No, I am not the anonymous hacker, but there really is a problem with
read/write files under 4.1 bsd.

To see the problem, fopen a file in "r+", fgets a line, fseek to an odd
location, and then fwrite something there (and then fclose the file).
Nothing has been written!  Repeat this, fseeking to an even location, and
now the fwrite works correctly.

According to the manual, an fseek is necessary to switch between read and
write.  However, there is a problem with fseek which causes it to
incorrectly switch modes.  Under certain conditions, an fseek to an odd
location causes a seek to ( location - 1 ), followed by a getc (!).
Unfortunately, getc puts the buffer back into read mode, causing fseek to
exit with the buffer still in read mode.

Placing an fflush in the program will not solve the problem.  fseek is
leaving the buffer in read mode; fflush has no effect on a buffer in read
mode.

An obvious solution to this problem is to simply remove the offending code
(all code dealing with the variable "resync").  Is there some reason that
this "resync" code is there? (a PDP11-ism ?)

	jd



More information about the Comp.unix.wizards mailing list