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
58 # define setmode _setmode
59 # define O_BINARY _O_BINARY
64 Qexpand_squiggle (const char *path, char *dest)
69 struct passwd *pwd_ent;
72 if (strncmp (path, "~/", 2) != 0) {
78 // LordHavoc: first check HOME to duplicate previous version behavior
79 // (also handy if someone wants it somewhere other than their
81 home = getenv ("HOME");
82 if (!home || !home[0])
83 home = getenv ("WINDIR");
85 if ((pwd_ent = getpwuid (getuid ()))) {
86 home = pwd_ent->pw_dir;
88 home = getenv ("HOME");
93 strncat (dest, path + 1, MAX_OSPATH - strlen (dest)); // skip
100 Qrename (const char *old, const char *new)
102 char e_old[PATH_MAX];
103 char e_new[PATH_MAX];
105 Qexpand_squiggle (old, e_old);
106 Qexpand_squiggle (new, e_new);
107 return rename (e_old, e_new);
111 Qopen (const char *path, const char *mode)
116 char e_path[PATH_MAX];
118 Qexpand_squiggle (path, e_path);
121 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
127 if (strchr ("0123456789fh", *mode)) {
135 file = qmalloc (sizeof (*file));
136 memset(file, 0, sizeof(*file));
141 file->gzfile = gzopen (path, m);
149 file->file = fopen (path, m);
159 Qdopen (int fd, const char *mode)
165 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
175 file = qmalloc (sizeof (*file));
176 memset(file, 0, sizeof(*file));
181 file->gzfile = gzdopen (fd, m);
189 file->file = fdopen (fd, m);
197 setmode (_fileno (file->file), O_BINARY);
209 gzclose (file->gzfile);
215 Qread (QFile *file, void *buf, int count)
218 return fread (buf, 1, count, file->file);
221 return gzread (file->gzfile, buf, count);
228 Qwrite (QFile *file, void *buf, int count)
231 return fwrite (buf, 1, count, file->file);
234 return gzwrite (file->gzfile, buf, count);
241 Qprintf (QFile *file, const char *fmt, ...)
246 va_start (args, fmt);
248 ret = vfprintf (file->file, fmt, args);
253 va_start (args, fmt);
254 #ifdef HAVE_VSNPRINTF
255 (void) vsnprintf (buf, sizeof (buf), fmt, args);
257 (void) vsprintf (buf, fmt, args);
260 ret = strlen (buf); /* some *snprintf don't return the nb
263 ret = gzwrite (file->gzfile, buf, (unsigned) ret);
271 Qgets (QFile *file, char *buf, int count)
274 return fgets (buf, count, file->file);
277 return gzgets (file->gzfile, buf, count);
287 return fgetc (file->file);
290 return gzgetc (file->gzfile);
297 Qputc (QFile *file, int c)
300 return fputc (c, file->file);
303 return gzputc (file->gzfile, c);
310 Qseek (QFile *file, long offset, int whence)
313 return fseek (file->file, offset, whence);
316 return gzseek (file->gzfile, offset, whence);
326 return ftell (file->file);
329 return gztell (file->gzfile);
339 return fflush (file->file);
342 return gzflush (file->gzfile, Z_SYNC_FLUSH);
352 return feof (file->file);
355 return gzeof (file->gzfile);
365 Dynamic length version of Qgets. DO NOT free the buffer.
369 Qgetline (QFile *file)
371 static int size = 256;
372 static char *buf = 0;
378 if (!Qgets (file, buf, size))
382 while (buf[len - 1] != '\n' && buf[len - 1] != '\r') {
383 char *t = realloc (buf, size + 256);
386 Host_Error("Qgetline: realloc failed, out of memory?\n");
389 if (!Qgets (file, buf + len, size - len))
393 while ((len = strlen(buf)) && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))