6 Copyright (C) 1996-1997 Id Software, Inc.
7 Copyright (C) 1999,2000 contributors of the QuakeForge project
8 Please see the file "AUTHORS" for a list of contributors
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to:
24 Free Software Foundation, Inc.
25 59 Temple Place - Suite 330
26 Boston, MA 02111-1307, USA
51 #include "QF/quakefs.h"
52 #include "QF/quakeio.h"
56 # define setmode _setmode
57 # define O_BINARY _O_BINARY
62 Qexpand_squiggle (const char *path, char *dest)
67 struct passwd *pwd_ent;
70 if (strncmp (path, "~/", 2) != 0) {
76 // LordHavoc: first check HOME to duplicate previous version behavior
77 // (also handy if someone wants it somewhere other than their
79 home = getenv ("HOME");
80 if (!home || !home[0])
81 home = getenv ("WINDIR");
83 if ((pwd_ent = getpwuid (getuid ()))) {
84 home = pwd_ent->pw_dir;
86 home = getenv ("HOME");
91 strncat (dest, path + 1, MAX_OSPATH - strlen (dest)); // skip
98 Qrename (const char *old, const char *new)
100 char e_old[PATH_MAX];
101 char e_new[PATH_MAX];
103 Qexpand_squiggle (old, e_old);
104 Qexpand_squiggle (new, e_new);
105 return rename (e_old, e_new);
109 Qopen (const char *path, const char *mode)
114 char e_path[PATH_MAX];
116 Qexpand_squiggle (path, e_path);
119 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
125 if (strchr ("0123456789fh", *mode)) {
133 file = calloc (sizeof (*file), 1);
138 file->gzfile = gzopen (path, m);
146 file->file = fopen (path, m);
156 Qdopen (int fd, const char *mode)
162 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
172 file = calloc (sizeof (*file), 1);
177 file->gzfile = gzdopen (fd, m);
185 file->file = fdopen (fd, m);
193 setmode (_fileno (file->file), O_BINARY);
205 gzclose (file->gzfile);
211 Qread (QFile *file, void *buf, int count)
214 return fread (buf, 1, count, file->file);
217 return gzread (file->gzfile, buf, count);
224 Qwrite (QFile *file, void *buf, int count)
227 return fwrite (buf, 1, count, file->file);
230 return gzwrite (file->gzfile, buf, count);
237 Qprintf (QFile *file, const char *fmt, ...)
242 va_start (args, fmt);
244 ret = vfprintf (file->file, fmt, args);
249 va_start (args, fmt);
250 #ifdef HAVE_VSNPRINTF
251 (void) vsnprintf (buf, sizeof (buf), fmt, args);
253 (void) vsprintf (buf, fmt, args);
256 ret = strlen (buf); /* some *snprintf don't return the nb
259 ret = gzwrite (file->gzfile, buf, (unsigned) ret);
267 Qgets (QFile *file, char *buf, int count)
270 return fgets (buf, count, file->file);
273 return gzgets (file->gzfile, buf, count);
283 return fgetc (file->file);
286 return gzgetc (file->gzfile);
293 Qputc (QFile *file, int c)
296 return fputc (c, file->file);
299 return gzputc (file->gzfile, c);
306 Qseek (QFile *file, long offset, int whence)
309 return fseek (file->file, offset, whence);
312 return gzseek (file->gzfile, offset, whence);
322 return ftell (file->file);
325 return gztell (file->gzfile);
335 return fflush (file->file);
338 return gzflush (file->gzfile, Z_SYNC_FLUSH);
348 return feof (file->file);
351 return gzeof (file->gzfile);
361 Dynamic length version of Qgets. DO NOT free the buffer.
365 Qgetline (QFile *file)
367 static int size = 256;
368 static char *buf = 0;
374 if (!Qgets (file, buf, size))
378 while (buf[len - 1] != '\n') {
379 char *t = realloc (buf, size + 256);
385 if (!Qgets (file, buf + len, size - len))