Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!hplabs!nsc!pyramid!athertn!joshua From: joshua@athertn.Atherton.COM (Flame Bait) Newsgroups: comp.unix.questions Subject: is struct stat buf.st_size always correct? Message-ID: <21501@joshua.athertn.Atherton.COM> Date: 11 Apr 90 23:36:05 GMT Reply-To: joshua@Atherton.COM (Flame Bait) Organization: Atherton Technology, Sunnyvale, CA Lines: 31 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@atherton.com home:(415)968-3718 {decwrl|sun|hpda}!athertn!joshua work:(408)734-9822