is struct stat buf.st_size always correct?
Flame Bait
joshua at athertn.Atherton.COM
Thu Apr 12 09:36:05 AEST 1990
I have a simple routine which uses stat() to find the size of a file,
then mallocs space for it, and then freads the whole file into a string.
Under SunOS 3.5 (mostly 4.2BSD) it seemed to work fine, but I did not
use it much. Under SunOS 4.0.3 (mostly 4.3BSD) I sometimes end up with
extra `\377' characters at the end of the string. It looks like the whole
file is there, but with two extra characters as well.
Is it possible that the st_size is rounded up to the nearest 4 bytes,
or something like that?
The routine is similar to this one:
int eatfile(name, place) char *name ; char **place ;
{
struct stat buf ;
int size ;
FILE *fp ;
int s ;
/* actions */ /* error checking */
s = stat(name,&buf) ; if (s == -1) return -1 ;
size = buf.st_size ;
*place = malloc((unsigned)size) ; if (!*place) return -2;
fp = fopen(name,"r") ; if (!fp) { free(*place) ; return -3 ; }
s = fread(*place,1,size,fp) ; if (!s) { free(*place) ; return -4 ; }
s = fclose(fp) ; if (s==EOF) { free(*place); return -5; }
return size ;
}
Joshua Levy joshua at atherton.com home:(415)968-3718
{decwrl|sun|hpda}!athertn!joshua work:(408)734-9822
More information about the Comp.unix.questions
mailing list