2000-05-20 09:42:02 +00:00
|
|
|
/*
|
|
|
|
quakeio.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 1999,2000 contributors of the QuakeForge project
|
|
|
|
Please see the file "AUTHORS" for a list of contributors
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2000-12-08 06:51:37 +00:00
|
|
|
# include "config.h"
|
2000-05-20 09:42:02 +00:00
|
|
|
#endif
|
2000-12-05 16:41:28 +00:00
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
#ifdef HAVE_MALLOC_H
|
2000-05-22 21:16:22 +00:00
|
|
|
# include <malloc.h>
|
2000-05-20 09:42:02 +00:00
|
|
|
#endif
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef WIN32
|
2000-05-22 21:16:22 +00:00
|
|
|
# include <io.h>
|
|
|
|
# include <fcntl.h>
|
2000-05-20 09:42:02 +00:00
|
|
|
#else
|
2000-05-22 21:16:22 +00:00
|
|
|
# include <pwd.h>
|
2000-05-20 09:42:02 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2000-05-22 21:16:22 +00:00
|
|
|
# include <unistd.h>
|
2000-05-20 09:42:02 +00:00
|
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
2000-05-22 21:16:22 +00:00
|
|
|
# define _POSIX_
|
2000-05-20 09:42:02 +00:00
|
|
|
#endif
|
|
|
|
#include <limits.h>
|
|
|
|
|
2000-12-05 16:41:28 +00:00
|
|
|
#include "quakeio.h"
|
|
|
|
#include "quakefs.h"
|
|
|
|
|
2000-09-28 19:48:06 +00:00
|
|
|
#ifdef WIN32
|
2000-12-08 06:51:37 +00:00
|
|
|
# ifndef __BORLANDC__
|
|
|
|
# define setmode _setmode
|
|
|
|
# define O_BINARY _O_BINARY
|
|
|
|
# endif
|
2000-09-28 19:48:06 +00:00
|
|
|
#endif
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
void
|
2000-12-08 06:51:37 +00:00
|
|
|
Qexpand_squiggle (const char *path, char *dest)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
char *home;
|
|
|
|
|
2000-05-20 09:42:02 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
struct passwd *pwd_ent;
|
|
|
|
#endif
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
if (strncmp (path, "~/", 2) != 0) {
|
|
|
|
strcpy (dest, path);
|
2000-05-20 09:42:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-12-11 19:14:51 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// LordHavoc: first check HOME to duplicate previous version behavior
|
|
|
|
// (also handy if someone wants it somewhere other than their
|
|
|
|
// windows directory)
|
|
|
|
home = getenv ("HOME");
|
|
|
|
if (!home || !home[0])
|
|
|
|
home = getenv ("WINDIR");
|
|
|
|
#else
|
2000-12-08 06:51:37 +00:00
|
|
|
if ((pwd_ent = getpwuid (getuid ()))) {
|
2000-05-20 09:42:02 +00:00
|
|
|
home = pwd_ent->pw_dir;
|
|
|
|
} else
|
2000-12-08 06:51:37 +00:00
|
|
|
home = getenv ("HOME");
|
2000-12-11 19:14:51 +00:00
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
|
|
|
|
if (home) {
|
|
|
|
strcpy (dest, home);
|
2000-12-08 06:51:37 +00:00
|
|
|
strncat (dest, path + 1, MAX_OSPATH - strlen (dest)); // skip
|
|
|
|
// leading ~
|
2000-05-20 09:42:02 +00:00
|
|
|
} else
|
2000-12-08 06:51:37 +00:00
|
|
|
strcpy (dest, path);
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-08 06:51:37 +00:00
|
|
|
Qrename (const char *old, const char *new)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
char e_old[PATH_MAX];
|
|
|
|
char e_new[PATH_MAX];
|
2000-05-20 09:42:02 +00:00
|
|
|
|
|
|
|
Qexpand_squiggle (old, e_old);
|
|
|
|
Qexpand_squiggle (new, e_new);
|
|
|
|
return rename (e_old, e_new);
|
|
|
|
}
|
|
|
|
|
2000-09-27 19:44:26 +00:00
|
|
|
QFile *
|
2000-12-08 06:51:37 +00:00
|
|
|
Qopen (const char *path, const char *mode)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
QFile *file;
|
|
|
|
char m[80], *p;
|
|
|
|
int zip = 0;
|
|
|
|
char e_path[PATH_MAX];
|
2000-05-20 09:42:02 +00:00
|
|
|
|
|
|
|
Qexpand_squiggle (path, e_path);
|
|
|
|
path = e_path;
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
|
|
|
|
if (*mode == 'z') {
|
|
|
|
zip = 1;
|
2000-09-27 19:44:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2000-12-15 05:57:44 +00:00
|
|
|
#ifndef HAVE_ZLIB
|
|
|
|
if (strchr ("0123456789fh", *mode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2000-12-08 06:51:37 +00:00
|
|
|
*p++ = *mode;
|
2000-09-27 19:44:26 +00:00
|
|
|
}
|
2000-12-08 06:51:37 +00:00
|
|
|
*p = 0;
|
2000-09-27 19:44:26 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
file = calloc (sizeof (*file), 1);
|
2000-09-27 19:44:26 +00:00
|
|
|
if (!file)
|
|
|
|
return 0;
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
if (zip) {
|
2000-12-08 06:51:37 +00:00
|
|
|
file->gzfile = gzopen (path, m);
|
2000-09-27 19:44:26 +00:00
|
|
|
if (!file->gzfile) {
|
2000-12-08 06:51:37 +00:00
|
|
|
free (file);
|
2000-09-27 19:44:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
file->file = fopen (path, m);
|
2000-09-27 19:44:26 +00:00
|
|
|
if (!file->file) {
|
2000-12-08 06:51:37 +00:00
|
|
|
free (file);
|
2000-09-27 19:44:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2000-05-20 09:42:02 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2000-09-27 19:44:26 +00:00
|
|
|
QFile *
|
2000-12-08 06:51:37 +00:00
|
|
|
Qdopen (int fd, const char *mode)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
QFile *file;
|
|
|
|
char m[80], *p;
|
|
|
|
int zip = 0;
|
2000-09-27 19:44:26 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
|
|
|
|
if (*mode == 'z') {
|
|
|
|
zip = 1;
|
2000-09-27 19:44:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2000-12-08 06:51:37 +00:00
|
|
|
*p++ = *mode;
|
2000-09-27 19:44:26 +00:00
|
|
|
}
|
2000-05-20 09:42:02 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
*p = 0;
|
2000-09-27 19:44:26 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
file = calloc (sizeof (*file), 1);
|
2000-09-27 19:44:26 +00:00
|
|
|
if (!file)
|
|
|
|
return 0;
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
if (zip) {
|
2000-12-08 06:51:37 +00:00
|
|
|
file->gzfile = gzdopen (fd, m);
|
2000-09-27 19:44:26 +00:00
|
|
|
if (!file->gzfile) {
|
2000-12-08 06:51:37 +00:00
|
|
|
free (file);
|
2000-09-27 19:44:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
file->file = fdopen (fd, m);
|
2000-09-27 19:44:26 +00:00
|
|
|
if (!file->file) {
|
2000-12-08 06:51:37 +00:00
|
|
|
free (file);
|
2000-09-27 19:44:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2000-05-20 09:42:02 +00:00
|
|
|
#ifdef WIN32
|
2000-09-28 19:48:06 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
setmode (_fileno (file->file), O_BINARY);
|
2000-05-20 09:42:02 +00:00
|
|
|
#endif
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-12-08 06:51:37 +00:00
|
|
|
Qclose (QFile *file)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
fclose (file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
gzclose (file->gzfile);
|
2000-09-27 19:44:26 +00:00
|
|
|
#endif
|
2000-12-08 06:51:37 +00:00
|
|
|
free (file);
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-08 06:51:37 +00:00
|
|
|
Qread (QFile *file, void *buf, int count)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return fread (buf, 1, count, file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gzread (file->gzfile, buf, count);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-09-27 19:44:26 +00:00
|
|
|
Qwrite (QFile *file, void *buf, int count)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return fwrite (buf, 1, count, file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gzwrite (file->gzfile, buf, count);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-08 06:51:37 +00:00
|
|
|
Qprintf (QFile *file, const char *fmt, ...)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
va_list args;
|
|
|
|
int ret = -1;
|
2000-05-20 09:42:02 +00:00
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
va_start (args, fmt);
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
ret = vfprintf (file->file, fmt, args);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else {
|
2000-12-08 06:51:37 +00:00
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2000-09-27 19:44:26 +00:00
|
|
|
#ifdef HAVE_VSNPRINTF
|
2000-12-08 06:51:37 +00:00
|
|
|
(void) vsnprintf (buf, sizeof (buf), fmt, args);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
2000-12-08 06:51:37 +00:00
|
|
|
(void) vsprintf (buf, fmt, args);
|
2000-09-27 19:44:26 +00:00
|
|
|
#endif
|
2000-12-08 06:51:37 +00:00
|
|
|
va_end (args);
|
|
|
|
ret = strlen (buf); /* some *snprintf don't return the nb
|
|
|
|
of bytes written */
|
|
|
|
if (ret > 0)
|
|
|
|
ret = gzwrite (file, buf, (unsigned) ret);
|
2000-09-27 19:44:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
2000-12-08 06:51:37 +00:00
|
|
|
va_end (args);
|
2000-05-20 09:42:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2000-12-08 06:51:37 +00:00
|
|
|
Qgets (QFile *file, char *buf, int count)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return fgets (buf, count, file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gzgets (file->gzfile, buf, count);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-08 06:51:37 +00:00
|
|
|
Qgetc (QFile *file)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return fgetc (file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gzgetc (file->gzfile);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-08 06:51:37 +00:00
|
|
|
Qputc (QFile *file, int c)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return fputc (c, file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gzputc (file->gzfile, c);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-08 06:51:37 +00:00
|
|
|
Qseek (QFile *file, long offset, int whence)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return fseek (file->file, offset, whence);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gzseek (file->gzfile, offset, whence);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2000-12-08 06:51:37 +00:00
|
|
|
Qtell (QFile *file)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return ftell (file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gztell (file->gzfile);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-08 06:51:37 +00:00
|
|
|
Qflush (QFile *file)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return fflush (file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gzflush (file->gzfile, Z_SYNC_FLUSH);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-12-08 06:51:37 +00:00
|
|
|
Qeof (QFile *file)
|
2000-05-20 09:42:02 +00:00
|
|
|
{
|
2000-09-27 19:44:26 +00:00
|
|
|
if (file->file)
|
2000-12-08 06:51:37 +00:00
|
|
|
return feof (file->file);
|
2000-09-30 05:56:00 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2000-09-27 19:44:26 +00:00
|
|
|
else
|
2000-12-08 06:51:37 +00:00
|
|
|
return gzeof (file->gzfile);
|
2000-09-27 19:44:26 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2000-05-20 09:42:02 +00:00
|
|
|
}
|
2000-10-06 07:05:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Qgetline
|
|
|
|
|
2000-12-11 19:14:51 +00:00
|
|
|
Dynamic length version of Qgets. DO NOT free the buffer.
|
2000-10-06 07:05:22 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
char *
|
2000-12-08 06:51:37 +00:00
|
|
|
Qgetline (QFile *file)
|
2000-10-06 07:05:22 +00:00
|
|
|
{
|
2000-12-08 06:51:37 +00:00
|
|
|
static int size = 256;
|
2000-10-06 07:05:22 +00:00
|
|
|
static char *buf = 0;
|
2000-12-08 06:51:37 +00:00
|
|
|
int len;
|
2000-10-06 07:05:22 +00:00
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
buf = malloc (size);
|
|
|
|
|
2000-12-08 06:51:37 +00:00
|
|
|
if (!Qgets (file, buf, size))
|
2000-10-06 07:05:22 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
len = strlen (buf);
|
|
|
|
while (buf[len - 1] != '\n') {
|
2000-12-08 06:51:37 +00:00
|
|
|
char *t = realloc (buf, size + 256);
|
|
|
|
|
2000-10-06 07:05:22 +00:00
|
|
|
if (!t)
|
|
|
|
return 0;
|
|
|
|
buf = t;
|
|
|
|
size += 256;
|
2000-12-08 06:51:37 +00:00
|
|
|
if (!Qgets (file, buf + len, size - len))
|
2000-10-06 07:05:22 +00:00
|
|
|
break;
|
|
|
|
len = strlen (buf);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|