Blame lib/Xm/XpmRdFToBuf.c

Packit b099d7
/* $XConsortium: XpmRdFToBuf.c /main/2 1996/09/20 08:13:12 pascale $ */
Packit b099d7
/*
Packit b099d7
 * Copyright (C) 1989-95 GROUPE BULL
Packit b099d7
 *
Packit b099d7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b099d7
 * of this software and associated documentation files (the "Software"), to
Packit b099d7
 * deal in the Software without restriction, including without limitation the
Packit b099d7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b099d7
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b099d7
 * furnished to do so, subject to the following conditions:
Packit b099d7
 *
Packit b099d7
 * The above copyright notice and this permission notice shall be included in
Packit b099d7
 * all copies or substantial portions of the Software.
Packit b099d7
 *
Packit b099d7
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b099d7
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b099d7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Packit b099d7
 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
Packit b099d7
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit b099d7
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit b099d7
 *
Packit b099d7
 * Except as contained in this notice, the name of GROUPE BULL shall not be
Packit b099d7
 * used in advertising or otherwise to promote the sale, use or other dealings
Packit b099d7
 * in this Software without prior written authorization from GROUPE BULL.
Packit b099d7
 */
Packit b099d7
Packit b099d7
/*****************************************************************************\
Packit b099d7
* RdFToBuf.c:                                                                 *
Packit b099d7
*                                                                             *
Packit b099d7
*  XPM library                                                                *
Packit b099d7
*  Copy a file to a malloc'ed buffer, provided as a convenience.              *
Packit b099d7
*                                                                             *
Packit b099d7
*  Developed by Arnaud Le Hors                                                *
Packit b099d7
\*****************************************************************************/
Packit b099d7
Packit b099d7
/*
Packit b099d7
 * The code related to FOR_MSW has been added by
Packit b099d7
 * HeDu (hedu@cul-ipn.uni-kiel.de) 4/94
Packit b099d7
 */
Packit b099d7
Packit b099d7
#ifdef HAVE_CONFIG_H
Packit b099d7
#include <config.h>
Packit b099d7
#endif
Packit b099d7
Packit b099d7
Packit b099d7
/* October 2004, source code review by Thomas Biege <thomas@suse.de> */
Packit b099d7
Packit b099d7
#include "XpmI.h"
Packit b099d7
#include <sys/stat.h>
Packit b099d7
#if !defined(FOR_MSW) && !defined(WIN32)
Packit b099d7
#include <unistd.h>
Packit b099d7
#endif
Packit b099d7
#ifndef VAX11C
Packit b099d7
#include <fcntl.h>
Packit b099d7
#endif
Packit b099d7
#if defined(FOR_MSW) || defined(WIN32)
Packit b099d7
#include <io.h>
Packit b099d7
#define stat _stat
Packit b099d7
#define fstat _fstat
Packit b099d7
#define fdopen _fdopen
Packit b099d7
#define O_RDONLY _O_RDONLY
Packit b099d7
#endif
Packit b099d7
Packit b099d7
int
Packit b099d7
XpmReadFileToBuffer(filename, buffer_return)
Packit b099d7
    char *filename;
Packit b099d7
    char **buffer_return;
Packit b099d7
{
Packit b099d7
    int fd, fcheck;
Packit b099d7
    off_t len;
Packit b099d7
    char *ptr;
Packit b099d7
    struct stat stats;
Packit b099d7
    FILE *fp;
Packit b099d7
Packit b099d7
    *buffer_return = NULL;
Packit b099d7
Packit b099d7
#ifndef VAX11C
Packit b099d7
    fd = open(filename, O_RDONLY);
Packit b099d7
#else
Packit b099d7
    fd = open(filename, O_RDONLY, NULL);
Packit b099d7
#endif
Packit b099d7
    if (fd < 0)
Packit b099d7
	return XpmOpenFailed;
Packit b099d7
Packit b099d7
    if (fstat(fd, &stats)) {
Packit b099d7
	close(fd);
Packit b099d7
	return XpmOpenFailed;
Packit b099d7
    }
Packit b099d7
    fp = fdopen(fd, "r");
Packit b099d7
    if (!fp) {
Packit b099d7
	close(fd);
Packit b099d7
	return XpmOpenFailed;
Packit b099d7
    }
Packit b099d7
    len = stats.st_size;
Packit b099d7
    ptr = (char *) XpmMalloc(len + 1);
Packit b099d7
    if (!ptr) {
Packit b099d7
	fclose(fp);
Packit b099d7
	return XpmNoMemory;
Packit b099d7
    }
Packit b099d7
    fcheck = fread(ptr, 1, len, fp);
Packit b099d7
    fclose(fp);
Packit b099d7
#ifdef VMS
Packit b099d7
    /* VMS often stores text files in a variable-length record format,
Packit b099d7
       where there are two bytes of size followed by the record.  fread	
Packit b099d7
       converts this so it looks like a record followed by a newline.	
Packit b099d7
       Unfortunately, the size reported by fstat() (and fseek/ftell)	
Packit b099d7
       counts the two bytes for the record terminator, while fread()	
Packit b099d7
       counts only one.  So, fread() sees fewer bytes in the file (size	
Packit b099d7
       minus # of records) and thus when asked to read the amount	
Packit b099d7
       returned by stat(), it fails.
Packit b099d7
       The best solution, suggested by DEC, seems to consider the length
Packit b099d7
       returned from fstat() as an upper bound and call fread() with
Packit b099d7
       a record length of 1. Then don't check the return value.
Packit b099d7
       We'll check for 0 for gross error that's all.
Packit b099d7
    */
Packit b099d7
    len = fcheck;
Packit b099d7
    if (fcheck == 0) {
Packit b099d7
#else
Packit b099d7
    if (fcheck != len) {
Packit b099d7
#endif
Packit b099d7
	XpmFree(ptr);
Packit b099d7
	return XpmOpenFailed;
Packit b099d7
    }
Packit b099d7
    ptr[len] = '\0';
Packit b099d7
    *buffer_return = ptr;
Packit b099d7
    return XpmSuccess;
Packit b099d7
}