]> git.xonotic.org Git - xonotic/gmqcc.git/blob - fs.c
Fix some memory leaks.
[xonotic/gmqcc.git] / fs.c
1 /*
2  * Copyright (C) 2012, 2013
3  *     Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24
25 /*
26  * This is essentially a "wrapper" interface around standard C's IO
27  * library.  There is two reason we implement this, 1) visual studio
28  * hearts for "secure" varations, as part of it's "Security Enhancements
29  * in the CRT" (http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx).
30  * 2) But one of the greater reasons is for the possibility of large file
31  * support in the future.  I don't expect to reach the 2GB limit any
32  * time soon (mainly because that would be insane).  But when it comes
33  * to adding support for some other larger IO tasks (in the test-suite,
34  * or even the QCVM we'll need it). There is also a third possibility of
35  * building .dat files directly from zip files (which would be very cool
36  * at least I think so).  
37  */
38 #ifdef _MSC_VER
39 /* {{{ */
40     /*
41      * Visual Studio has security CRT features which I actually want to support
42      * if we ever port to Windows 8, and want GMQCC to be API safe.
43      *
44      * We handle them here, for all file-operations. 
45      */
46
47     static void file_exception (
48         const wchar_t *expression,
49         const wchar_t *function,
50         const wchar_t *file,
51         unsigned int   line,
52         uintptr_t      reserved
53     ) {
54         wprintf(L"Invalid parameter dectected %s:%d %s [%s]\n", file, line, function, expression);
55         wprintf(L"Aborting ...\n");
56         abort();
57     }
58
59     static void file_init() {
60         static bool init = false;
61
62         if (init)
63             return;
64
65         _set_invalid_parameter_handler(&file_exception);
66
67         /*
68          * Turnoff the message box for CRT asserations otherwise
69          * we don't get the error reported to the console as we should
70          * otherwise get.
71          */
72         _CrtSetReportMode(_CRT_ASSERT, 0);
73         init = !init;
74     }
75
76
77     FILE *fs_file_open(const char *filename, const char *mode) {
78         FILE *handle = NULL;
79         file_init();
80
81         return (fopen_s(&handle, filename, mode) != 0) ? NULL : handle;
82     }
83
84     size_t fs_file_read(void *buffer, size_t size, size_t count, FILE *fp) {
85         file_init();
86         return fread_s(buffer, size*count, size, count, fp);
87     }
88
89     int fs_file_printf(FILE *fp, const char *format, ...) {
90         int      rt;
91         va_list  va;
92         va_start(va, format);
93
94         file_init();
95         rt = vfprintf_s(fp, format, va);
96         va_end  (va);
97
98         return rt;
99     }
100
101 /* }}} */
102 #else
103 /* {{{ */
104     /*
105      * All other compilers/platforms that don't restrict insane policies on
106      * IO for no aparent reason.
107      */
108     FILE *fs_file_open(const char *filename, const char *mode) {
109         return fopen(filename, mode);
110     }
111
112     size_t fs_file_read(void *buffer, size_t size, size_t count, FILE *fp) {
113         return fread(buffer, size, count, fp);
114     }
115
116     int fs_file_printf(FILE *fp, const char *format, ...) {
117         int      rt;
118         va_list  va;
119         va_start(va, format);
120         rt = vfprintf(fp, format, va);
121         va_end  (va);
122
123         return rt;
124     }
125
126 /* }}} */
127 #endif
128
129 /*
130  * These are implemented as just generic wrappers to keep consistency in
131  * the API.  Not as macros though  
132  */
133 void fs_file_close(FILE *fp) {
134     /* Invokes file_exception on windows if fp is null */
135     fclose (fp);
136 }
137
138 size_t  fs_file_write (
139     const void    *buffer,
140     size_t         size,
141     size_t         count,
142     FILE          *fp
143 ) {
144     /* Invokes file_exception on windows if fp is null */
145     return fwrite(buffer, size, count, fp);
146 }
147
148 int fs_file_error(FILE *fp) {
149     /* Invokes file_exception on windows if fp is null */
150     return ferror(fp);
151 }
152
153 int fs_file_getc(FILE *fp) {
154     /* Invokes file_exception on windows if fp is null */
155     return fgetc(fp);
156 }
157
158 int fs_file_puts(FILE *fp, const char *str) {
159     /* Invokes file_exception on windows if fp is null */
160     return fputs(str, fp);
161 }
162
163 int fs_file_seek(FILE *fp, long int off, int whence) {
164     /* Invokes file_exception on windows if fp is null */
165     return fseek(fp, off, whence);
166 }
167
168 int fs_file_putc(FILE *fp, int ch) {
169     /* Invokes file_exception on windows if fp is null */
170     return fputc(ch, fp);
171 }
172
173 int fs_file_flush(FILE *fp) {
174     /* Invokes file_exception on windows if fp is null */
175     return fflush(fp);
176 }
177
178 long int fs_file_tell(FILE *fp) {
179     /* Invokes file_exception on windows if fp is null */
180     return ftell(fp);
181 }
182
183 /*
184  * Implements libc getline for systems that don't have it, which is
185  * assmed all.  This works the same as getline().
186  */
187 int fs_file_getline(char **lineptr, size_t *n, FILE *stream) {
188     int   chr;
189     int   ret;
190     char *pos;
191
192     if (!lineptr || !n || !stream)
193         return -1;
194     if (!*lineptr) {
195         if (!(*lineptr = (char*)mem_a((*n=64))))
196             return -1;
197     }
198
199     chr = *n;
200     pos = *lineptr;
201
202     for (;;) {
203         int c = fs_file_getc(stream);
204
205         if (chr < 2) {
206             *n += (*n > 16) ? *n : 64;
207             chr = *n + *lineptr - pos;
208             if (!(*lineptr = (char*)mem_r(*lineptr,*n)))
209                 return -1;
210             pos = *n - chr + *lineptr;
211         }
212
213         if (ferror(stream))
214             return -1;
215         if (c == EOF) {
216             if (pos == *lineptr)
217                 return -1;
218             else
219                 break;
220         }
221
222         *pos++ = c;
223         chr--;
224         if (c == '\n')
225             break;
226     }
227     *pos = '\0';
228     return (ret = pos - *lineptr);
229 }
230
231 /*
232  * Now we implement some directory functionality.  Windows lacks dirent.h
233  * this is such a pisss off, we implement it here.
234  */  
235 #if defined(_WIN32) && !defined(__MINGW32__)
236     DIR *fs_dir_open(const char *name) {
237         DIR *dir = (DIR*)mem_a(sizeof(DIR) + strlen(name));
238         if (!dir)
239             return NULL;
240
241         strcpy(dir->dd_name, name);
242         return dir;
243     }
244         
245     int fs_dir_close(DIR *dir) {
246         FindClose((HANDLE)dir->dd_handle);
247         mem_d ((void*)dir);
248         return 0;
249     }
250
251     struct dirent *fs_dir_read(DIR *dir) {
252         WIN32_FIND_DATA info;
253         struct dirent  *data;
254         int             rets;
255
256         if (!dir->dd_handle) {
257             char *dirname;
258             if (*dir->dd_name) {
259                 size_t n = strlen(dir->dd_name);
260                 if ((dirname  = (char*)mem_a(n + 5) /* 4 + 1 */)) {
261                     strcpy(dirname,     dir->dd_name);
262                     strcpy(dirname + n, "\\*.*");   /* 4 + 1 */
263                 }
264             } else {
265                 if (!(dirname = util_strdup("\\*.*")))
266                     return NULL;
267             }
268
269             dir->dd_handle = (long)FindFirstFile(dirname, &info);
270             mem_d(dirname);
271             rets = !(!dir->dd_handle);
272         } else if (dir->dd_handle != -11) {
273             rets = FindNextFile ((HANDLE)dir->dd_handle, &info);
274         } else {
275             rets = 0;
276         }
277
278         if (!rets)
279             return NULL;
280         
281         if ((data = (struct dirent*)mem_a(sizeof(struct dirent)))) {
282             strncpy(data->d_name, info.cFileName, FILENAME_MAX - 1);
283             data->d_name[FILENAME_MAX - 1] = '\0'; /* terminate */
284             data->d_namlen                 = strlen(data->d_name);
285         }
286         return data;
287     }
288
289     int fs_dir_change(const char *path) {
290         return !SetCurrentDirectory(path);
291     }
292
293     int fs_dir_make(const char *path) {
294         return !CreateDirectory(path, NULL);
295     }
296
297     /*
298      * Visual studio also lacks S_ISDIR for sys/stat.h, so we emulate this as well
299      * which is not hard at all.
300      */
301 #   undef  S_ISDIR
302 #   define S_ISDIR(X) ((X)&_S_IFDIR)
303 #else
304 #   if !defined(__MINGW32__)
305 #       include <sys/stat.h> /* mkdir */
306 #       include <unistd.h>   /* chdir */
307
308         int fs_dir_make(const char *path) {
309             return mkdir(path, 0700);
310         }
311 #   else
312         int fs_dir_make(const char *path) {
313             return mkdir(path);
314         }
315 #   endif /*! !defined(__MINGW32__) */
316
317 DIR *fs_dir_open(const char *name) {
318     return opendir(name);
319 }
320
321 int fs_dir_close(DIR *dir) {
322     return closedir(dir);
323 }
324
325 struct dirent *fs_dir_read(DIR *dir) {
326     return readdir(dir);
327 }
328
329 int fs_dir_change(const char *path) {
330     return chdir(path);
331 }
332
333 #endif /*! defined(_WIN32) && !defined(__MINGW32__) */