[comp.os.minix] Minix/ST getc bug + fix

meulenbr@cstw01.UUCP (Frans Meulenbroeks) (10/31/88)

Hi!

I noticed a bug in the Minix/ST getc.
Consider the following program (typed in from memory):
#include <stdio.h>
main()
{
	int c;
	FILE *f;
	f = fopen("/etc/passwd", "r");
	c = getc(f);
	printf("pos = %ld\n", ftell(f));
}
To my amazemend ftell returned zero.

Fix: in getc.c  _buf is filled, _count is decreased before this is done.
This should be done afterwards.
This bug was apparently noticed during Minix test, but fixed at the wrong place.
Unapply this fix in fseek.c

The following shar file contains patches which (hopefully :-) ) correct
this.

Since I don't have a PC version of Minix, I don't know if this fix is also
appropriate there.

Unshar, patch, compile, ar and enjoy!

Frans.

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
-----cut here-----cut here-----cut here-----cut here-----
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	fseek.cdiff
#	getc.cdiff
# This archive created: Mon Oct 31 10:50:33 1988
sed 's/^X//' << \SHAR_EOF > fseek.cdiff
X*** ../lib/fseek.c	Tue Mar  1 21:37:22 1988
X--- fseek.c	Mon Oct 31 10:40:07 1988
X***************
X*** 18,26 ****
X  			pos = offset;
X  
X  			if ( where == 0 )
X! 				pos += count - lseek(fileno(iop), 0L,1) - 1;
X! 				/*^^^ This caused the problem : - 1 corrected
X! 				      it */
X  			else
X  				offset -= count;
X  
X--- 18,24 ----
X  			pos = offset;
X  
X  			if ( where == 0 )
X! 				pos += count - lseek(fileno(iop), 0L,1);
X  			else
X  				offset -= count;
X  
SHAR_EOF
sed 's/^X//' << \SHAR_EOF > getc.cdiff
X*** ../lib/getc.c	Tue Mar  1 21:37:24 1988
X--- getc.c	Mon Oct 31 10:43:07 1988
X***************
X*** 13,19 ****
X  	if ( !testflag(iop, READMODE) ) 
X  		return (EOF);
X  
X! 	if (--iop->_count <= 0){
X  
X  		if ( testflag(iop, UNBUFF) )
X  			iop->_count = read(iop->_fd,&ch,1);
X--- 13,19 ----
X  	if ( !testflag(iop, READMODE) ) 
X  		return (EOF);
X  
X! 	if (iop->_count <= 0){
X  
X  		if ( testflag(iop, UNBUFF) )
X  			iop->_count = read(iop->_fd,&ch,1);
X***************
X*** 32,37 ****
X--- 32,39 ----
X  			iop->_ptr = iop->_buf;
X  	}
X  
X+ 	/* Don't decrease count before the filling of the buffer */
X+ 	iop->_count--;
X  	if (testflag(iop,UNBUFF))
X  		return (ch & CMASK);
X  	else
SHAR_EOF
#	End of shell archive
exit 0
-- 
Frans Meulenbroeks        (meulenbr@cst.prl.philips.nl)
	Centre for Software Technology
	( or try: ...!mcvax!philmds!prle!cst!meulenbr)