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 = calloc (sizeof (*file), 1);
140 file->gzfile = gzopen (path, m);
148 file->file = fopen (path, m);
158 Qdopen (int fd, const char *mode)
164 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
174 file = calloc (sizeof (*file), 1);
179 file->gzfile = gzdopen (fd, m);
187 file->file = fdopen (fd, m);
195 setmode (_fileno (file->file), O_BINARY);
207 gzclose (file->gzfile);
213 Qread (QFile *file, void *buf, int count)
216 return fread (buf, 1, count, file->file);
219 return gzread (file->gzfile, buf, count);
226 Qwrite (QFile *file, void *buf, int count)
229 return fwrite (buf, 1, count, file->file);
232 return gzwrite (file->gzfile, buf, count);
239 Qprintf (QFile *file, const char *fmt, ...)
244 va_start (args, fmt);
246 ret = vfprintf (file->file, fmt, args);
251 va_start (args, fmt);
252 #ifdef HAVE_VSNPRINTF
253 (void) vsnprintf (buf, sizeof (buf), fmt, args);
255 (void) vsprintf (buf, fmt, args);
258 ret = strlen (buf); /* some *snprintf don't return the nb
261 ret = gzwrite (file->gzfile, buf, (unsigned) ret);
269 Qgets (QFile *file, char *buf, int count)
272 return fgets (buf, count, file->file);
275 return gzgets (file->gzfile, buf, count);
285 return fgetc (file->file);
288 return gzgetc (file->gzfile);
295 Qputc (QFile *file, int c)
298 return fputc (c, file->file);
301 return gzputc (file->gzfile, c);
308 Qseek (QFile *file, long offset, int whence)
311 return fseek (file->file, offset, whence);
314 return gzseek (file->gzfile, offset, whence);
324 return ftell (file->file);
327 return gztell (file->gzfile);
337 return fflush (file->file);
340 return gzflush (file->gzfile, Z_SYNC_FLUSH);
350 return feof (file->file);
353 return gzeof (file->gzfile);
363 Dynamic length version of Qgets. DO NOT free the buffer.
367 Qgetline (QFile *file)
369 static int size = 256;
370 static char *buf = 0;
376 if (!Qgets (file, buf, size))
380 while (buf[len - 1] != '\n') {
381 char *t = realloc (buf, size + 256);
387 if (!Qgets (file, buf + len, size - len))