Back to index

courier  0.68.2
readfile.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "courier.h"
00007 #include      <sys/stat.h>
00008 #include      <stdio.h>
00009 #include      <stdlib.h>
00010 #include      <string.h>
00011 
00012 /*
00013        Read an entire file into memory.
00014 
00015 */
00016 
00017 char   *readfile(const char *filename, struct stat *stat_buf)
00018 {
00019 struct stat   my_buf;
00020 FILE   *fp;
00021 char   *p=0;
00022 
00023        if (!stat_buf)       stat_buf= &my_buf;
00024        if ((fp=fopen(filename, "r")) == 0)       return (0);
00025 
00026        if (fstat(fileno(fp), stat_buf) ||
00027               fread((p=(char *)courier_malloc(stat_buf->st_size+1)),
00028                      1, stat_buf->st_size, fp) != stat_buf->st_size)
00029        {
00030               fclose(fp);
00031               clog_msg_errno();
00032        }
00033        fclose(fp);
00034        p[stat_buf->st_size]=0;
00035        return (p);
00036 }