ref: f367cc559968dc10f81eabd39227a4793beaf1b3
parent: b8f176fc34741b72fc6bbb6ab45cad33c2c0d717
author: Konstantinn Bonnet <qu7uux@gmail.com>
date: Tue Jun 30 12:58:33 EDT 2015
fix some file handling bullshit 01234567890123456789012345678901234567890123456789012345678901234567890123456789 - fix memory leak and map list going out of bounds in multiplayer menu whenever the menu is accessed anew without exiting it by pressing esc - remove FS_Dir_f, FS_Path_f, FS_Link_f: useless - Sys_Find*, FS_ListFiles: use DMDIR as single flag, for whether to only keep directories or to exclude them
--- a/dat.h
+++ b/dat.h
@@ -200,10 +200,6 @@
extern int curtime; // current time in ms, from Sys_Milliseconds()
enum{
- SFF_SUBDIR = 1<<3
-};
-
-enum{
CVAR_ARCHIVE = 1<<0, // save to vars.rc
CVAR_USERINFO = 1<<1, // add to userinfo on change
CVAR_SERVERINFO = 1<<2, // add to serverinfo on change
--- a/files.c
+++ b/files.c
@@ -47,16 +47,6 @@
cvar_t *fs_cddir;
cvar_t *fs_gamedirvar;
-typedef struct filelink_s
-{
- struct filelink_s *next;
- char *from;
- int fromlength;
- char *to;
-} filelink_t;
-
-filelink_t *fs_links;
-
typedef struct searchpath_s
{
char filename[MAX_OSPATH];
@@ -187,26 +177,9 @@
char netpath[MAX_OSPATH];
pack_t *pak;
int i;
- filelink_t *link;
file_from_pak = 0;
- // check for links first
- for (link = fs_links ; link ; link=link->next)
- {
- if (!strncmp (filename, link->from, link->fromlength))
- {
- Com_sprintf (netpath, sizeof(netpath), "%s%s",link->to, filename+link->fromlength);
- *file = fopen (netpath, "rb");
- if (*file)
- {
- Com_DPrintf ("link file: %s\n",netpath);
- return FS_filelength (*file);
- }
- return -1;
- }
- }
-
//
// search through the path, one element at a time
//
@@ -535,27 +508,19 @@
return fs_gamedir;
}
-/*
-=============
-FS_ExecAutoexec
-=============
-*/
-void FS_ExecAutoexec (void)
+void
+FS_ExecAutoexec(void)
{
- char *dir;
- char name [MAX_QPATH];
+ char buf[MAX_QPATH], *d;
- dir = Cvar_VariableString("gamedir");
- if (*dir)
- Com_sprintf(name, sizeof(name), "%s/%s/autoexec.cfg", fs_basedir->string, dir);
- else
- Com_sprintf(name, sizeof(name), "%s/%s/autoexec.cfg", fs_basedir->string, BASEDIRNAME);
- if (Sys_FindFirst(name, 0, SFF_SUBDIR))
- Cbuf_AddText ("exec autoexec.cfg\n");
+ d = Cvar_VariableString("gamedir");
+ snprint(buf, sizeof buf, "%s/%s/autoexec.cfg", fs_basedir->string, *d ? d : BASEDIRNAME);
+
+ if(Sys_FindFirst(buf, 0) != nil)
+ Cbuf_AddText("exec autoexec.cfg\n");
Sys_FindClose();
}
-
/*
================
FS_SetGamedir
@@ -612,73 +577,23 @@
}
}
-
-/*
-================
-FS_Link_f
-
-Creates a filelink_t
-================
-*/
-void FS_Link_f (void)
+char **
+FS_ListFiles(char *findname, int *numfiles, int f)
{
- filelink_t *l, **prev;
-
- if (Cmd_Argc() != 3)
- {
- Com_Printf ("USAGE: link <from> <to>\n");
- return;
- }
-
- // see if the link already exists
- prev = &fs_links;
- for (l=fs_links ; l ; l=l->next)
- {
- if (!strcmp (l->from, Cmd_Argv(1)))
- {
- Z_Free (l->to);
- if (!strlen(Cmd_Argv(2)))
- { // delete it
- *prev = l->next;
- Z_Free (l->from);
- Z_Free (l);
- return;
- }
- l->to = CopyString (Cmd_Argv(2));
- return;
- }
- prev = &l->next;
- }
-
- // create a new link
- l = Z_Malloc(sizeof(*l));
- l->next = fs_links;
- fs_links = l;
- l->from = CopyString(Cmd_Argv(1));
- l->fromlength = strlen(l->from);
- l->to = CopyString(Cmd_Argv(2));
-}
-
-/*
-** FS_ListFiles
-*/
-char **FS_ListFiles( char *findname, int *numfiles, unsigned musthave, unsigned canthave )
-{
char *s;
int nfiles = 0;
char **list;
- s = Sys_FindFirst( findname, musthave, canthave );
- while ( s )
- {
- if ( s[strlen(s)-1] != '.' )
+ s = Sys_FindFirst(findname, f);
+ while(s != nil){
+ if(s[strlen(s)-1] != '.')
nfiles++;
- s = Sys_FindNext( musthave, canthave );
+ s = Sys_FindNext(f);
}
- Sys_FindClose ();
+ Sys_FindClose();
- if ( !nfiles )
- return NULL;
+ if(!nfiles)
+ return nil;
nfiles++; // add space for a guard
*numfiles = nfiles;
@@ -686,7 +601,7 @@
list = malloc( sizeof( char * ) * nfiles );
memset( list, 0, sizeof( char * ) * nfiles );
- s = Sys_FindFirst( findname, musthave, canthave );
+ s = Sys_FindFirst(findname, f);
nfiles = 0;
while ( s )
{
@@ -695,7 +610,7 @@
list[nfiles] = strdup( s );
nfiles++;
}
- s = Sys_FindNext( musthave, canthave );
+ s = Sys_FindNext(f);
}
Sys_FindClose ();
@@ -702,126 +617,32 @@
return list;
}
-/*
-** FS_Dir_f
-*/
-void FS_Dir_f( void )
+/* for enumerating all dirs in the search path */
+char *
+FS_NextPath(char *prevpath)
{
- char *path = NULL;
- char findname[1024];
- char wildcard[1024] = "*.*";
- char **dirnames;
- int ndirs;
+ searchpath_t *s;
+ char *prev;
- if ( Cmd_Argc() != 1 )
- {
- strcpy( wildcard, Cmd_Argv( 1 ) );
- }
-
- while ( ( path = FS_NextPath( path ) ) != NULL )
- {
- char *tmp = findname;
-
- Com_sprintf( findname, sizeof(findname), "%s/%s", path, wildcard );
-
- while ( *tmp != 0 )
- {
- if ( *tmp == '\\' )
- *tmp = '/';
- tmp++;
- }
- Com_Printf( "Directory of %s\n", findname );
- Com_Printf( "----\n" );
-
- if ( ( dirnames = FS_ListFiles( findname, &ndirs, 0, 0 ) ) != 0 )
- {
- int i;
-
- for ( i = 0; i < ndirs-1; i++ )
- {
- if ( strrchr( dirnames[i], '/' ) )
- Com_Printf( "%s\n", strrchr( dirnames[i], '/' ) + 1 );
- else
- Com_Printf( "%s\n", dirnames[i] );
-
- free( dirnames[i] );
- }
- free( dirnames );
- }
- Com_Printf( "\n" );
- };
-}
-
-/*
-============
-FS_Path_f
-
-============
-*/
-void FS_Path_f (void)
-{
- searchpath_t *s;
- filelink_t *l;
-
- Com_Printf ("Current search path:\n");
- for (s=fs_searchpaths ; s ; s=s->next)
- {
- if (s == fs_base_searchpaths)
- Com_Printf ("----------\n");
- if (s->pack)
- Com_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
- else
- Com_Printf ("%s\n", s->filename);
- }
-
- Com_Printf ("\nLinks:\n");
- for (l=fs_links ; l ; l=l->next)
- Com_Printf ("%s : %s\n", l->from, l->to);
-}
-
-/*
-================
-FS_NextPath
-
-Allows enumerating all of the directories in the search path
-================
-*/
-char *FS_NextPath (char *prevpath)
-{
- searchpath_t *s;
- char *prev;
-
- if (!prevpath)
+ if(prevpath == nil)
return fs_gamedir;
prev = fs_gamedir;
- for (s=fs_searchpaths ; s ; s=s->next)
- {
- if (s->pack)
+ for(s = fs_searchpaths; s != nil; s = s->next){
+ if(s->pack != nil)
continue;
- if (prevpath == prev)
+ if(prevpath == prev)
return s->filename;
prev = s->filename;
}
-
- return NULL;
+ return nil;
}
-
-/*
-================
-FS_InitFilesystem
-================
-*/
void FS_InitFilesystem (void)
{
static char homedir[1024];
char *home;
- Cmd_AddCommand ("path", FS_Path_f);
- Cmd_AddCommand ("link", FS_Link_f);
- Cmd_AddCommand ("dir", FS_Dir_f );
-
//
// basedir <path>
// allows the game to run from outside the data tree
@@ -855,6 +676,3 @@
if (fs_gamedirvar->string[0])
FS_SetGamedir (fs_gamedirvar->string);
}
-
-
-
--- a/fns.h
+++ b/fns.h
@@ -46,9 +46,10 @@
void FS_Read(void *, int, FILE *);
void FS_FreeFile(void *);
void FS_CreatePath(char *);
+char** FS_ListFiles(char *, int *, int);
-char* Sys_FindFirst(char *, uint, uint);
-char* Sys_FindNext(uint, uint);
+char* Sys_FindFirst(char *, int);
+char* Sys_FindNext(int);
void Sys_FindClose(void);
int Sys_Milliseconds(void);
void Sys_Mkdir(char *);
--- a/menu.c
+++ b/menu.c
@@ -2415,6 +2415,20 @@
//=====
}
+static void
+freemaps(void)
+{
+ int i;
+
+ if(mapnames != nil){
+ for(i = 0; i < nummaps; i++)
+ free(mapnames[i]);
+ free(mapnames);
+ }
+ mapnames = nil;
+ nummaps = 0;
+}
+
void StartServerActionFunc( void * )
{
char startmap[1024];
@@ -2513,6 +2527,8 @@
int i;
FILE *fp;
+ freemaps();
+
/*
** load the list of map names
*/
@@ -2524,13 +2540,9 @@
}
else
{
-#ifdef _WIN32
- length = filelength( fileno( fp ) );
-#else
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, 0, SEEK_SET);
-#endif
buffer = malloc( length );
fread( buffer, length, 1, fp );
}
@@ -2698,19 +2710,8 @@
char *StartServer_MenuKey( int key )
{
if ( key == K_ESCAPE )
- {
- if ( mapnames )
- {
- int i;
+ freemaps();
- for ( i = 0; i < nummaps; i++ )
- free( mapnames[i] );
- free( mapnames );
- }
- mapnames = 0;
- nummaps = 0;
- }
-
return Default_MenuKey( &s_startserver_menu, key );
}
@@ -3425,28 +3426,22 @@
char findname[1024];
char scratch[1024];
int ndirs = 0, npms;
- char **dirnames = NULL;
- char *path = NULL;
+ char **dirnames = nil;
+ char *path = nil;
int i;
- extern char **FS_ListFiles( char *, int *, unsigned, unsigned );
-
s_numplayermodels = 0;
- /*
- ** get a list of directories
- */
- do
- {
- if ( ( path = FS_NextPath( path ) ) == NULL)
+ /* get a list of directories */
+ do{
+ if((path = FS_NextPath(path)) == nil)
break;
- Com_sprintf( findname, sizeof(findname), "%s/players/*.*", path );
-
- if ( ( dirnames = FS_ListFiles( findname, &ndirs, SFF_SUBDIR, 0 ) ) != 0 )
+ snprint(findname, sizeof findname, "%s/players/*.*", path);
+ if((dirnames = FS_ListFiles(findname, &ndirs, DMDIR)) != nil)
break;
- } while ( path );
+ }while(path != nil);
- if ( !dirnames )
+ if(dirnames == nil)
return false;
/*
@@ -3469,11 +3464,10 @@
continue;
// verify the existence of tris.md2
- strcpy( scratch, dirnames[i] );
- strcat( scratch, "/tris.md2" );
- if ( !Sys_FindFirst( scratch, 0, SFF_SUBDIR ) )
- {
- free( dirnames[i] );
+ strcpy(scratch, dirnames[i]);
+ strcat(scratch, "/tris.md2");
+ if(Sys_FindFirst(scratch, 0) == nil){
+ free(dirnames[i]);
dirnames[i] = 0;
Sys_FindClose();
continue;
@@ -3481,13 +3475,10 @@
Sys_FindClose();
// verify the existence of at least one pcx skin
- strcpy( scratch, dirnames[i] );
- strcat( scratch, "/*.pcx" );
- pcxnames = FS_ListFiles( scratch, &npcxfiles, 0, SFF_SUBDIR);
-
- if ( !pcxnames )
- {
- free( dirnames[i] );
+ strcpy(scratch, dirnames[i]);
+ strcat(scratch, "/*.pcx");
+ if((pcxnames = FS_ListFiles(scratch, &npcxfiles, 0)) == nil){
+ free(dirnames[i]);
dirnames[i] = 0;
continue;
}
--- a/sv_ccmds.c
+++ b/sv_ccmds.c
@@ -150,19 +150,17 @@
remove (name);
Com_sprintf (name, sizeof(name), "%s/save/%s/*.sav", FS_Gamedir (), savename);
- s = Sys_FindFirst( name, 0, 0 );
- while (s)
- {
- remove (s);
- s = Sys_FindNext( 0, 0 );
+ s = Sys_FindFirst(name, 0);
+ while(s != nil){
+ remove(s);
+ s = Sys_FindNext(0);
}
Sys_FindClose ();
Com_sprintf (name, sizeof(name), "%s/save/%s/*.sv2", FS_Gamedir (), savename);
- s = Sys_FindFirst(name, 0, 0 );
- while (s)
- {
+ s = Sys_FindFirst(name, 0);
+ while(s != nil){
remove (s);
- s = Sys_FindNext( 0, 0 );
+ s = Sys_FindNext(0);
}
Sys_FindClose ();
}
@@ -232,9 +230,8 @@
Com_sprintf (name, sizeof(name), "%s/save/%s/", FS_Gamedir(), src);
len = strlen(name);
Com_sprintf (name, sizeof(name), "%s/save/%s/*.sav", FS_Gamedir(), src);
- found = Sys_FindFirst(name, 0, 0 );
- while (found)
- {
+ found = Sys_FindFirst(name, 0);
+ while(found != nil){
strcpy (name+len, found+len);
Com_sprintf (name2, sizeof(name2), "%s/save/%s/%s", FS_Gamedir(), dst, found+len);
@@ -247,7 +244,7 @@
strcpy (name2+l-3, "sv2");
CopyFile (name, name2);
- found = Sys_FindNext( 0, 0 );
+ found = Sys_FindNext(0);
}
Sys_FindClose ();
}
--- a/sys.c
+++ b/sys.c
@@ -227,18 +227,8 @@
free(base);
}
-static qboolean
-CompareAttributes(ulong m, uint musthave, uint canthave)
-{
- if(m & DMDIR && canthave & SFF_SUBDIR)
- return false;
- if(musthave & SFF_SUBDIR && ~m & DMDIR)
- return false;
- return true;
-}
-
char *
-Sys_FindFirst(char *path, uint musthave, uint canhave)
+Sys_FindFirst(char *path, int f)
{
char *p;
int fd;
@@ -273,24 +263,22 @@
}
di = 0;
- return Sys_FindNext(musthave, canhave);
+ return Sys_FindNext(f);
}
+/* if f is DMDIR, only retain directories; otherwise always exclude them */
char *
-Sys_FindNext(uint musthave, uint canhave)
+Sys_FindNext(int f)
{
int i;
if(dirs == nil)
Sys_Error("Sys_FindNext without open\n");
-
while(di < dirn){
i = di++;
- if(glob_match(findpattern, dirs[i].name)){
- if(CompareAttributes(dirs[i].mode, musthave, canhave)){
- snprintf(findpath, sizeof findpath, "%s/%s", findbase, dirs[i].name);
- return findpath;
- }
+ if(!(f ^ dirs[i].mode & DMDIR) && glob_match(findpattern, dirs[i].name)){
+ snprint(findpath, sizeof findpath, "%s/%s", findbase, dirs[i].name);
+ return findpath;
}
}
return nil;