2002-01-21 07:24:51 +00:00
|
|
|
/*
|
|
|
|
bi_file.c
|
|
|
|
|
|
|
|
CSQC file builtins
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-11-08 03:35:22 +00:00
|
|
|
#include <stdlib.h>
|
2002-01-21 07:24:51 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
2002-01-30 08:34:44 +00:00
|
|
|
# include <string.h>
|
2002-01-21 07:24:51 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
2002-01-30 08:34:44 +00:00
|
|
|
# include <strings.h>
|
2002-01-21 07:24:51 +00:00
|
|
|
#endif
|
|
|
|
|
2002-03-12 17:36:17 +00:00
|
|
|
#ifdef HAVE_FNMATCH_H
|
|
|
|
# define model_t sunmodel_t
|
|
|
|
# include <fnmatch.h>
|
|
|
|
# undef model_t
|
|
|
|
#else
|
|
|
|
# ifdef WIN32
|
|
|
|
# include "fnmatch.h"
|
|
|
|
# endif
|
|
|
|
#endif
|
2002-03-11 19:01:13 +00:00
|
|
|
|
2002-03-12 17:44:45 +00:00
|
|
|
#ifndef HAVE_FNMATCH_PROTO
|
|
|
|
int fnmatch (const char *__pattern, const char *__string, int __flags);
|
|
|
|
#endif
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
#include "QF/csqc.h"
|
2003-02-14 22:36:10 +00:00
|
|
|
#include "QF/cvar.h"
|
2002-01-21 07:24:51 +00:00
|
|
|
#include "QF/progs.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2002-01-21 07:24:51 +00:00
|
|
|
#include "QF/va.h"
|
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2002-03-11 19:01:13 +00:00
|
|
|
static const char *file_ban_list[] = {
|
|
|
|
"default.cfg{,.gz}",
|
|
|
|
"demo1.dem{,.gz}",
|
|
|
|
"demo2.dem{,.gz}",
|
|
|
|
"demo3.dem{,.gz}",
|
|
|
|
"end1.bin{,.gz}",
|
|
|
|
"end2.bin{,.gz}",
|
|
|
|
"gfx.wad{,.gz}",
|
|
|
|
"progs.dat{,.gz}",
|
|
|
|
"quake.rc{,.gz}",
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *dir_ban_list[] = {
|
|
|
|
"gfx",
|
|
|
|
"maps",
|
|
|
|
"progs",
|
|
|
|
"skins",
|
|
|
|
"sound",
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_readable (char *path)
|
|
|
|
{
|
|
|
|
char t;
|
|
|
|
char *p = strchr (path, '/');
|
|
|
|
const char **match;
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
t = *p;
|
|
|
|
*p = 0;
|
|
|
|
for (match = dir_ban_list; *match; match++) {
|
|
|
|
if (fnmatch (*match, path, FNM_PATHNAME) == 0) {
|
|
|
|
*p = t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (match = file_ban_list; *match; match++) {
|
|
|
|
if (fnmatch (*match, path, FNM_PATHNAME) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_writeable (char *path)
|
|
|
|
{
|
|
|
|
return file_readable (path);
|
|
|
|
}
|
|
|
|
|
2002-01-21 07:24:51 +00:00
|
|
|
static void
|
|
|
|
bi_File_Open (progs_t *pr)
|
|
|
|
{
|
2003-02-28 05:35:27 +00:00
|
|
|
qfile_resources_t *res = PR_Resources_Find (pr, "QFile");
|
|
|
|
QFile **file = QFile_AllocHandle (pr, res);
|
2003-04-22 18:20:15 +00:00
|
|
|
const char *pth = P_GSTRING (pr, 0);
|
|
|
|
const char *mode = P_GSTRING (pr, 1);
|
2002-11-08 03:35:22 +00:00
|
|
|
char *path;
|
|
|
|
char *p;
|
2002-03-11 19:01:13 +00:00
|
|
|
int do_write = 0;
|
|
|
|
int do_read = 0;
|
|
|
|
|
|
|
|
p = strchr (mode, 'r');
|
|
|
|
if (p) {
|
|
|
|
do_read |= 1;
|
|
|
|
if (p[1] == '+')
|
|
|
|
do_write |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strchr (mode, 'w');
|
|
|
|
if (p) {
|
|
|
|
do_write |= 1;
|
|
|
|
if (p[1] == '+')
|
|
|
|
do_read |= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strchr (mode, 'a');
|
|
|
|
if (p) {
|
|
|
|
do_write |= 1;
|
|
|
|
if (p[1] == '+')
|
|
|
|
do_read |= 1;
|
|
|
|
}
|
2002-01-21 07:24:51 +00:00
|
|
|
|
2003-02-14 19:46:07 +00:00
|
|
|
path = QFS_CompressPath (pth);
|
2003-04-22 18:20:15 +00:00
|
|
|
//printf ("'%s' '%s'\n", P_GSTRING (pr, 0), path);
|
2002-01-21 07:24:51 +00:00
|
|
|
if (!path[0])
|
|
|
|
goto error;
|
|
|
|
if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || path [2] == 0))
|
|
|
|
goto error;
|
|
|
|
if (path[strlen (path) - 1] =='/')
|
|
|
|
goto error;
|
2002-03-11 19:01:13 +00:00
|
|
|
if (!do_read && !do_write)
|
|
|
|
goto error;
|
|
|
|
if (do_read && !file_readable (path))
|
|
|
|
goto error;
|
|
|
|
if (do_write && !file_writeable (path))
|
|
|
|
goto error;
|
2003-02-28 05:35:27 +00:00
|
|
|
|
2003-05-23 17:17:01 +00:00
|
|
|
*file = QFS_Open (va ("%s/%s", qfs_gamedir->dir.def, path), mode);
|
2003-02-28 05:35:27 +00:00
|
|
|
if (!*file)
|
|
|
|
goto error;
|
|
|
|
R_INT (pr) = (file - res->handles) + 1;
|
2002-11-08 03:35:22 +00:00
|
|
|
free (path);
|
2002-01-21 07:24:51 +00:00
|
|
|
return;
|
|
|
|
error:
|
2002-11-08 03:35:22 +00:00
|
|
|
free (path);
|
2002-07-24 21:42:33 +00:00
|
|
|
R_INT (pr) = 0;
|
2002-01-21 07:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
File_Progs_Init (progs_t *pr)
|
|
|
|
{
|
|
|
|
PR_AddBuiltin (pr, "File_Open", bi_File_Open, -1);
|
|
|
|
}
|