Back to index

plt-scheme  4.2.1
Functions
RdFToBuf.c File Reference
#include "xpmP.h"
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

Go to the source code of this file.

Functions

int XpmReadFileToBuffer (char *filename, char **buffer_return)

Function Documentation

int XpmReadFileToBuffer ( char *  filename,
char **  buffer_return 
)

Definition at line 51 of file RdFToBuf.c.

{
    int fd, fcheck, len;
    char *ptr;
    struct stat stats;
    FILE *fp;

    *buffer_return = NULL;

#ifndef VAX11C
    fd = open(filename, O_RDONLY);
#else
    fd = open(filename, O_RDONLY, NULL);
#endif
    if (fd < 0)
       return XpmOpenFailed;

    if (fstat(fd, &stats)) {
       close(fd);
       return XpmOpenFailed;
    }
    fp = fdopen(fd, "r");
    if (!fp) {
       close(fd);
       return XpmOpenFailed;
    }
    len = (int) stats.st_size;
    ptr = (char *) XpmMalloc(len + 1);
    if (!ptr) {
       fclose(fp);
       return XpmNoMemory;
    }
    fcheck = fread(ptr, len, 1, fp);
    fclose(fp);
    if (fcheck != 1) {
       XpmFree(ptr);
       return XpmOpenFailed;
    }
    ptr[len] = '\0';
    *buffer_return = ptr;
    return XpmSuccess;
}