home *** CD-ROM | disk | FTP | other *** search
- /* iread.c */
- /*************************************************
- * read file into malloc'd buffer, return file size
- * Istvan Mohos, 1987 --- in the Public Domain
- **************************************************/
-
- #include "i.h"
-
- int
- iread (fname, mallocp)
- char *fname;
- char **mallocp;
- {
- struct stat sbuf;
- int checkval, fd;
- int count;
-
- if (BADCHARP (fname))
- return (ierror ("iread: invalid file name"));
-
- if (mallocp == (char **) NULL) {
- if (access (fname, R_OK) == -1)
- return (-1); /* can't read it */
- return (0);
- }
-
- if ((fd = open (fname, 0)) == -1)
- return (ierror ("iread: no file access"));
-
- if ((checkval = fstat (fd, &sbuf)) == -1)
- return (ierror ("iread: fstat read error"));
-
- if ((count = (int)sbuf.st_size) == 0)
- return (ierror ("iread: zero length file"));
-
- if (NULCHARP (*mallocp = malloc ((unsigned int) count+1)))
- return (ierror ("iread: can't allocate read buffer"));
-
- if ((checkval = read (fd, *mallocp, count)) != count) {
- sprintf (ierbuf+200,
- "iread: expected: %d, read: %d", count, checkval);
- return (ierror (ierbuf+200));
- }
- close (fd);
- *(*mallocp + count) = 0;
- return (checkval);
- }
-