RdFToBuf.c revision ac92798b
1/*
2 * Copyright (C) 1989-95 GROUPE BULL
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
24 */
25
26/*****************************************************************************\
27* RdFToBuf.c:                                                                 *
28*                                                                             *
29*  XPM library                                                                *
30*  Copy a file to a malloc'ed buffer, provided as a convenience.              *
31*                                                                             *
32*  Developed by Arnaud Le Hors                                                *
33\*****************************************************************************/
34
35/*
36 * The code related to FOR_MSW has been added by
37 * HeDu (hedu@cul-ipn.uni-kiel.de) 4/94
38 */
39
40/* October 2004, source code review by Thomas Biege <thomas@suse.de> */
41
42#ifdef HAVE_CONFIG_H
43#include <config.h>
44#endif
45#include "XpmI.h"
46#include <sys/stat.h>
47#if !defined(FOR_MSW) && !defined(WIN32)
48#include <unistd.h>
49#endif
50#ifndef VAX11C
51#include <fcntl.h>
52#endif
53#if defined(FOR_MSW) || defined(WIN32)
54#include <io.h>
55#define stat _stat
56#define fstat _fstat
57#define fdopen _fdopen
58#define O_RDONLY _O_RDONLY
59#endif
60
61int
62XpmReadFileToBuffer(
63    const char	 *filename,
64    char	**buffer_return)
65{
66    int fd, fcheck;
67    off_t len;
68    char *ptr;
69    struct stat stats;
70    FILE *fp;
71
72    *buffer_return = NULL;
73
74#ifndef VAX11C
75    fd = open(filename, O_RDONLY);
76#else
77    fd = open(filename, O_RDONLY, NULL);
78#endif
79    if (fd < 0)
80	return XpmOpenFailed;
81
82    if (fstat(fd, &stats)) {
83	close(fd);
84	return XpmOpenFailed;
85    }
86    fp = fdopen(fd, "r");
87    if (!fp) {
88	close(fd);
89	return XpmOpenFailed;
90    }
91    len = stats.st_size;
92    ptr = (char *) XpmMalloc(len + 1);
93    if (!ptr) {
94	fclose(fp);
95	return XpmNoMemory;
96    }
97    fcheck = fread(ptr, 1, len, fp);
98    fclose(fp);
99#ifdef VMS
100    /* VMS often stores text files in a variable-length record format,
101       where there are two bytes of size followed by the record.  fread
102       converts this so it looks like a record followed by a newline.
103       Unfortunately, the size reported by fstat() (and fseek/ftell)
104       counts the two bytes for the record terminator, while fread()
105       counts only one.  So, fread() sees fewer bytes in the file (size
106       minus # of records) and thus when asked to read the amount
107       returned by stat(), it fails.
108       The best solution, suggested by DEC, seems to consider the length
109       returned from fstat() as an upper bound and call fread() with
110       a record length of 1. Then don't check the return value.
111       We'll check for 0 for gross error that's all.
112    */
113    len = fcheck;
114    if (fcheck == 0) {
115#else
116    if (fcheck != len) {
117#endif
118	XpmFree(ptr);
119	return XpmOpenFailed;
120    }
121    ptr[len] = '\0';
122    *buffer_return = ptr;
123    return XpmSuccess;
124}
125