Debian Codegraph @

src/RdFToBuf.c

raw
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 /* October 2004, source code review by Thomas Biege <[email protected]> */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40 #include <sys/stat.h> // before XpmI.h, which may rewrite stat (etc) via Xwindows.h
41 #include "XpmI.h"
42 #ifndef WIN32
43 #include <unistd.h>
44 #endif
45 #include <fcntl.h>
46 #ifdef WIN32
47 #include <io.h>
48 #define fdopen _fdopen
49 #define O_RDONLY _O_RDONLY
50 #endif
51
52 int
53 XpmReadFileToBuffer(
54 const char *filename,
55 char **buffer_return)
56 {
57 int fd, fcheck;
58 off_t len;
59 char *ptr;
60 struct stat stats;
61 FILE *fp;
62
63 *buffer_return = NULL;
64
65 fd = open(filename, O_RDONLY | O_CLOEXEC);
66 if (fd < 0)
67 return XpmOpenFailed;
68
69 if (fstat(fd, &stats)) {
70 close(fd);
71 return XpmOpenFailed;
72 }
73 fp = fdopen(fd, "r");
74 if (!fp) {
75 close(fd);
76 return XpmOpenFailed;
77 }
78 len = stats.st_size;
79 if (len < 0 || len >= SIZE_MAX) {
80 fclose(fp);
81 return XpmOpenFailed;
82 }
83 ptr = (char *) XpmMalloc(len + 1);
84 if (!ptr) {
85 fclose(fp);
86 return XpmNoMemory;
87 }
88 fcheck = fread(ptr, 1, len, fp);
89 fclose(fp);
90 #ifdef VMS
91 /* VMS often stores text files in a variable-length record format,
92 where there are two bytes of size followed by the record. fread
93 converts this so it looks like a record followed by a newline.
94 Unfortunately, the size reported by fstat() (and fseek/ftell)
95 counts the two bytes for the record terminator, while fread()
96 counts only one. So, fread() sees fewer bytes in the file (size
97 minus # of records) and thus when asked to read the amount
98 returned by stat(), it fails.
99 The best solution, suggested by DEC, seems to consider the length
100 returned from fstat() as an upper bound and call fread() with
101 a record length of 1. Then don't check the return value.
102 We'll check for 0 for gross error that's all.
103 */
104 len = fcheck;
105 if (fcheck == 0) {
106 #else
107 if (fcheck != len) {
108 #endif
109 XpmFree(ptr);
110 return XpmOpenFailed;
111 }
112 ptr[len] = '\0';
113 *buffer_return = ptr;
114 return XpmSuccess;
115 }