Changed file::find to take the path and glob as one argument. Made some

miscellaneous file access cleanups.
This commit is contained in:
Brian Koropoff 2002-11-16 03:52:07 +00:00
parent f26ec7377c
commit ac8169ba54

View file

@ -527,14 +527,24 @@ int (*GIB_File_Transform_Path) (dstring_t *path) = NULL;
int int
GIB_File_Transform_Path_Null (dstring_t *path) GIB_File_Transform_Path_Null (dstring_t *path)
{ {
char *s;
// Convert backslash to forward slash
while ((s = strchr (path->str, '\\')))
*s = '/';
return 0; return 0;
} }
int int
GIB_File_Transform_Path_Secure (dstring_t *path) GIB_File_Transform_Path_Secure (dstring_t *path)
{ {
char *s;
while ((s = strchr (path->str, '\\')))
*s = '/';
if (Sys_PathType (path->str) != PATHTYPE_RELATIVE_BELOW) if (Sys_PathType (path->str) != PATHTYPE_RELATIVE_BELOW)
return -1; return -1;
dstring_insertstr (path, 0, "/");
dstring_insertstr (path, 0, com_gamedir); dstring_insertstr (path, 0, com_gamedir);
return 0; return 0;
} }
@ -618,29 +628,32 @@ GIB_File_Find_f (void)
{ {
DIR *directory; DIR *directory;
struct dirent *entry; struct dirent *entry;
char *path; char *path, *glob, *s;
const char *ifs; const char *ifs;
dstring_t *list; dstring_t *list;
if (GIB_Argc () < 2 || GIB_Argc () > 3) { if (GIB_Argc () != 2) {
Cbuf_Error ("syntax", Cbuf_Error ("syntax",
"file::find: invalid syntax\n" "file::find: invalid syntax\n"
"usage: file::find glob [path]"); "usage: file::find path_and_glob");
return; return;
} }
if (GIB_Argc () == 3) { if (GIB_File_Transform_Path (GIB_Argd(1))) {
if (!*GIB_Argv(2)) { Cbuf_Error ("access",
Cbuf_Error ("file", "file::find: access to %s denied", GIB_Argv(1));
"file::find: null path provided"); return;
return; }
} path = GIB_Argv(1);
if (GIB_File_Transform_Path (GIB_Argd(2))) { s = strrchr (path, '/');
Cbuf_Error ("access", if (!s) { // No slash in path
"file::find: access to %s denied", GIB_Argv(2)); glob = path; // The glob is the entire argument
return; path = "."; // The path is the current directory
} } else {
*s = 0; // Split the string at the final slash
glob = s+1;
if (!*path) // If we now have a null path...
path = "/"; // we wanted the filesystem root in unix
} }
path = GIB_Argv(2);
directory = opendir (path); directory = opendir (path);
if (!directory) { if (!directory) {
Cbuf_Error ("file", Cbuf_Error ("file",
@ -649,12 +662,12 @@ GIB_File_Find_f (void)
} }
list = dstring_newstr (); list = dstring_newstr ();
if (!(ifs = GIB_Var_Get_Local (cbuf_active, "ifs"))) if (!(ifs = GIB_Var_Get_Local (cbuf_active, "ifs")))
ifs = " "; ifs = "\n"; // Newlines don't appear in filenames and are part of the default ifs
while ((entry = readdir (directory))) { while ((entry = readdir (directory))) {
if (strcmp (entry->d_name, ".") && if (strcmp (entry->d_name, ".") &&
strcmp (entry->d_name, "..") && strcmp (entry->d_name, "..") &&
!fnmatch (GIB_Argv (1), entry->d_name, 0)) { !fnmatch (glob, entry->d_name, 0)) {
dstring_appendstr (list, ifs); dstring_appendsubstr (list, ifs, 1);
dstring_appendstr (list, entry->d_name); dstring_appendstr (list, entry->d_name);
} }
} }
@ -744,7 +757,7 @@ GIB_Range_f (void)
ifs = " "; ifs = " ";
dstr = dstring_newstr (); dstr = dstring_newstr ();
for (i = atof(GIB_Argv(1)); inc < 0 ? i >= limit : i <= limit; i += inc) for (i = atof(GIB_Argv(1)); inc < 0 ? i >= limit : i <= limit; i += inc)
dstring_appendstr (dstr, va("%s%.10g", ifs, i)); dasprintf(dstr, "%.1s%.10g", ifs, i);
GIB_Return (dstr->str[0] ? dstr->str+1 : ""); GIB_Return (dstr->str[0] ? dstr->str+1 : "");
dstring_delete (dstr); dstring_delete (dstr);
} }