2001-02-19 21:15:25 +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
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
# include <malloc.h>
|
|
|
|
#endif
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef WIN32
|
|
|
|
# include <io.h>
|
|
|
|
# include <fcntl.h>
|
|
|
|
#else
|
|
|
|
# include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/quakeio.h"
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define _POSIX_
|
|
|
|
#endif
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
void
|
2001-02-26 06:48:02 +00:00
|
|
|
Qexpand_squiggle (const char *path, char *dest)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char *home;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
struct passwd *pwd_ent;
|
|
|
|
#endif
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (strncmp (path, "~/", 2) != 0) {
|
|
|
|
strcpy (dest, path);
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef _WIN32
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((pwd_ent = getpwuid (getuid ()))) {
|
2001-02-19 21:15:25 +00:00
|
|
|
home = pwd_ent->pw_dir;
|
|
|
|
} else
|
|
|
|
#endif
|
2001-02-26 06:48:02 +00:00
|
|
|
home = getenv ("HOME");
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (home) {
|
|
|
|
strcpy (dest, home);
|
2001-02-26 06:48:02 +00:00
|
|
|
strcat (dest, path + 1); // skip leading ~
|
2001-02-19 21:15:25 +00:00
|
|
|
} else
|
2001-02-26 06:48:02 +00:00
|
|
|
strcpy (dest, path);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qrename (const char *old, const char *new)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char e_old[PATH_MAX];
|
|
|
|
char e_new[PATH_MAX];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Qexpand_squiggle (old, e_old);
|
|
|
|
Qexpand_squiggle (new, e_new);
|
|
|
|
return rename (e_old, e_new);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
QFile *
|
|
|
|
Qopen (const char *path, const char *mode)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
QFile *file;
|
|
|
|
char m[80], *p;
|
|
|
|
int zip = 0;
|
|
|
|
char e_path[PATH_MAX];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Qexpand_squiggle (path, e_path);
|
|
|
|
path = e_path;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
|
|
|
|
if (*mode == 'z') {
|
|
|
|
zip = 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
*p++ = *mode;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
*p = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
file = calloc (sizeof (*file), 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!file)
|
|
|
|
return 0;
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
if (zip) {
|
2001-02-26 06:48:02 +00:00
|
|
|
file->gzfile = gzopen (path, m);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!file->gzfile) {
|
2001-02-26 06:48:02 +00:00
|
|
|
free (file);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
file->file = fopen (path, m);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!file->file) {
|
2001-02-26 06:48:02 +00:00
|
|
|
free (file);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
QFile *
|
|
|
|
Qdopen (int fd, const char *mode)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
QFile *file;
|
|
|
|
char m[80], *p;
|
|
|
|
int zip = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
|
|
|
|
if (*mode == 'z') {
|
|
|
|
zip = 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
*p++ = *mode;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
*p = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
file = calloc (sizeof (*file), 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!file)
|
|
|
|
return 0;
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
if (zip) {
|
2001-02-26 06:48:02 +00:00
|
|
|
file->gzfile = gzdopen (fd, m);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!file->gzfile) {
|
2001-02-26 06:48:02 +00:00
|
|
|
free (file);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
file->file = fdopen (fd, m);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!file->file) {
|
2001-02-26 06:48:02 +00:00
|
|
|
free (file);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef WIN32
|
|
|
|
#ifdef __BORLANDC__
|
2001-02-26 06:48:02 +00:00
|
|
|
setmode (_fileno (file->file), O_BINARY);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
2001-02-26 06:48:02 +00:00
|
|
|
_setmode (_fileno (file->file), _O_BINARY);
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-02-26 06:48:02 +00:00
|
|
|
Qclose (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
fclose (file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
gzclose (file->gzfile);
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2001-02-26 06:48:02 +00:00
|
|
|
free (file);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qread (QFile *file, void *buf, int count)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return fread (buf, 1, count, file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gzread (file->gzfile, buf, count);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Qwrite (QFile *file, void *buf, int count)
|
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return fwrite (buf, 1, count, file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gzwrite (file->gzfile, buf, count);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qprintf (QFile *file, const char *fmt, ...)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
va_list args;
|
|
|
|
int ret = -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
va_start (args, fmt);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
ret = vfprintf (file->file, fmt, args);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else {
|
2001-02-26 06:48:02 +00:00
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_VSNPRINTF
|
2001-02-26 06:48:02 +00:00
|
|
|
(void) vsnprintf (buf, sizeof (buf), fmt, args);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
2001-02-26 06:48:02 +00:00
|
|
|
(void) vsprintf (buf, fmt, args);
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2001-02-26 06:48:02 +00:00
|
|
|
va_end (args);
|
|
|
|
ret = strlen (buf); /* some *sprintf don't return the nb
|
|
|
|
of bytes written */
|
|
|
|
if (ret > 0)
|
|
|
|
ret = gzwrite (file, buf, (unsigned) ret);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
2001-02-26 06:48:02 +00:00
|
|
|
va_end (args);
|
2001-02-19 21:15:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
char *
|
|
|
|
Qgets (QFile *file, char *buf, int count)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return fgets (buf, count, file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gzgets (file->gzfile, buf, count);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qgetc (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return fgetc (file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gzgetc (file->gzfile);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qputc (QFile *file, int c)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return fputc (c, file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gzputc (file->gzfile, c);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qseek (QFile *file, long offset, int whence)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return fseek (file->file, offset, whence);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gzseek (file->gzfile, offset, whence);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2001-02-26 06:48:02 +00:00
|
|
|
Qtell (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return ftell (file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gztell (file->gzfile);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qflush (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return fflush (file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gzflush (file->gzfile, Z_SYNC_FLUSH);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qeof (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
2001-02-26 06:48:02 +00:00
|
|
|
return feof (file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-02-26 06:48:02 +00:00
|
|
|
return gzeof (file->gzfile);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qgetpos (QFile *file, fpos_t * pos)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_FPOS_T_STRUCT
|
2001-02-26 06:48:02 +00:00
|
|
|
pos->__pos = Qtell (file);
|
|
|
|
return pos->__pos == -1 ? -1 : 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
2001-02-26 06:48:02 +00:00
|
|
|
*pos = Qtell (file);
|
|
|
|
return *pos == -1 ? -1 : 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-26 06:48:02 +00:00
|
|
|
Qsetpos (QFile *file, fpos_t * pos)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_FPOS_T_STRUCT
|
2001-02-26 06:48:02 +00:00
|
|
|
return Qseek (file, pos->__pos, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
2001-02-26 06:48:02 +00:00
|
|
|
return Qseek (file, *pos, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
}
|