]> git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/RdFToBuf.c
Fixed unresolved references
[wxWidgets.git] / src / mac / xpm / RdFToBuf.c
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 #include "wx/setup.h"
41
42 #ifdef macintosh
43 #include <stat.h>
44 #include <unix.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #endif
48
49 #include "XpmI.h"
50 #ifndef macintosh
51 #include <sys/stat.h>
52
53 #if !defined(FOR_MSW) && !defined(WIN32)
54 #include <unistd.h>
55 #endif
56 #ifndef VAX11C
57 #include <fcntl.h>
58 #endif
59 #if defined(FOR_MSW) || defined(WIN32)
60 #include <io.h>
61 #define stat _stat
62 #define fstat _fstat
63 #define fdopen _fdopen
64 #define O_RDONLY _O_RDONLY
65 #endif
66 #endif
67
68 int
69 XpmReadFileToBuffer(filename, buffer_return)
70 char *filename;
71 char **buffer_return;
72 {
73 int fd, fcheck, len;
74 char *ptr;
75 struct stat stats;
76 FILE *fp;
77
78 *buffer_return = NULL;
79
80 #ifndef VAX11C
81 fd = open(filename, O_RDONLY);
82 #else
83 fd = open(filename, O_RDONLY, NULL);
84 #endif
85 if (fd < 0)
86 return XpmOpenFailed;
87
88 if (fstat(fd, &stats)) {
89 close(fd);
90 return XpmOpenFailed;
91 }
92 fp = fdopen(fd, "r");
93 if (!fp) {
94 close(fd);
95 return XpmOpenFailed;
96 }
97 len = (int) stats.st_size;
98 ptr = (char *) XpmMalloc(len + 1);
99 if (!ptr) {
100 fclose(fp);
101 return XpmNoMemory;
102 }
103 fcheck = fread(ptr, 1, len, fp);
104 fclose(fp);
105 #ifdef VMS
106 /* VMS often stores text files in a variable-length record format,
107 where there are two bytes of size followed by the record. fread
108 converts this so it looks like a record followed by a newline.
109 Unfortunately, the size reported by fstat() (and fseek/ftell)
110 counts the two bytes for the record terminator, while fread()
111 counts only one. So, fread() sees fewer bytes in the file (size
112 minus # of records) and thus when asked to read the amount
113 returned by stat(), it fails.
114 The best solution, suggested by DEC, seems to consider the length
115 returned from fstat() as an upper bound and call fread() with
116 a record length of 1. Then don't check the return value.
117 We'll check for 0 for gross error that's all.
118 */
119 len = fcheck;
120 if (fcheck == 0) {
121 #else
122 if (fcheck != len) {
123 #endif
124 XpmFree(ptr);
125 return XpmOpenFailed;
126 }
127 ptr[len] = '\0';
128 *buffer_return = ptr;
129 return XpmSuccess;
130 }