+
+/*
+===========
+FS_Search
+
+Allocate and fill a search structure with information on matching filenames.
+===========
+*/
+fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
+{
+ fssearch_t *search;
+ searchpath_t *searchpath;
+ pack_t *pak;
+ int i, basepathlength, numfiles, numchars;
+ stringlist_t *dir, *dirfile, *liststart, *listcurrent, *listtemp;
+ const char *slash, *backslash, *colon, *separator;
+ char *basepath;
+ char netpath[MAX_OSPATH];
+ char temp[MAX_OSPATH];
+
+ while(!strncmp(pattern, "./", 2))
+ pattern += 2;
+ while(!strncmp(pattern, ".\\", 2))
+ pattern += 2;
+
+ search = NULL;
+ liststart = NULL;
+ listcurrent = NULL;
+ listtemp = NULL;
+ slash = strrchr(pattern, '/');
+ backslash = strrchr(pattern, '\\');
+ colon = strrchr(pattern, ':');
+ separator = pattern;
+ if (separator < slash)
+ separator = slash;
+ if (separator < backslash)
+ separator = backslash;
+ if (separator < colon)
+ separator = colon;
+ basepathlength = separator - pattern;
+ basepath = Mem_Alloc (tempmempool, basepathlength + 1);
+ if (basepathlength)
+ memcpy(basepath, pattern, basepathlength);
+ basepath[basepathlength] = 0;
+
+ // search through the path, one element at a time
+ for (searchpath = fs_searchpaths;searchpath;searchpath = searchpath->next)
+ {
+ // is the element a pak file?
+ if (searchpath->pack)
+ {
+ // look through all the pak file elements
+ pak = searchpath->pack;
+ for (i = 0;i < pak->numfiles;i++)
+ {
+ strcpy(temp, pak->files[i].name);
+ while (temp[0])
+ {
+ if (matchpattern(temp, (char *)pattern, true))
+ {
+ for (listtemp = liststart;listtemp;listtemp = listtemp->next)
+ if (!strcmp(listtemp->text, temp))
+ break;
+ if (listtemp == NULL)
+ {
+ listcurrent = stringlistappend(listcurrent, temp);
+ if (liststart == NULL)
+ liststart = listcurrent;
+ if (!quiet)
+ Con_DPrintf("SearchPackFile: %s : %s\n", pak->filename, temp);
+ }
+ }
+ // strip off one path element at a time until empty
+ // this way directories are added to the listing if they match the pattern
+ slash = strrchr(temp, '/');
+ backslash = strrchr(temp, '\\');
+ colon = strrchr(temp, ':');
+ separator = temp;
+ if (separator < slash)
+ separator = slash;
+ if (separator < backslash)
+ separator = backslash;
+ if (separator < colon)
+ separator = colon;
+ *((char *)separator) = 0;
+ }
+ }
+ }
+ else
+ {
+ // get a directory listing and look at each name
+ snprintf(netpath, sizeof (netpath), "%s/%s", searchpath->filename, basepath);
+ if ((dir = listdirectory(netpath)))
+ {
+ for (dirfile = dir;dirfile;dirfile = dirfile->next)
+ {
+ snprintf(temp, sizeof(temp), "%s/%s", basepath, dirfile->text);
+ if (matchpattern(temp, (char *)pattern, true))
+ {
+ for (listtemp = liststart;listtemp;listtemp = listtemp->next)
+ if (!strcmp(listtemp->text, temp))
+ break;
+ if (listtemp == NULL)
+ {
+ listcurrent = stringlistappend(listcurrent, temp);
+ if (liststart == NULL)
+ liststart = listcurrent;
+ if (!quiet)
+ Con_DPrintf("SearchDirFile: %s\n", temp);
+ }
+ }
+ }
+ freedirectory(dir);
+ }
+ }
+ }
+
+ if (liststart)
+ {
+ liststart = stringlistsort(liststart);
+ numfiles = 0;
+ numchars = 0;
+ for (listtemp = liststart;listtemp;listtemp = listtemp->next)
+ {
+ numfiles++;
+ numchars += strlen(listtemp->text) + 1;
+ }
+ search = Z_Malloc(sizeof(fssearch_t) + numchars + numfiles * sizeof(char *));
+ search->filenames = (char **)((char *)search + sizeof(fssearch_t));
+ search->filenamesbuffer = (char *)((char *)search + sizeof(fssearch_t) + numfiles * sizeof(char *));
+ search->numfilenames = numfiles;
+ numfiles = 0;
+ numchars = 0;
+ for (listtemp = liststart;listtemp;listtemp = listtemp->next)
+ {
+ search->filenames[numfiles] = search->filenamesbuffer + numchars;
+ strcpy(search->filenames[numfiles], listtemp->text);
+ numfiles++;
+ numchars += strlen(listtemp->text) + 1;
+ }
+ if (liststart)
+ stringlistfree(liststart);
+ }
+
+ Mem_Free(basepath);
+ return search;
+}
+
+void FS_FreeSearch(fssearch_t *search)
+{
+ Z_Free(search);
+}
+
+extern int con_linewidth;
+int FS_ListDirectory(const char *pattern, int oneperline)
+{
+ int numfiles;
+ int numcolumns;
+ int numlines;
+ int columnwidth;
+ int linebufpos;
+ int i, j, k, l;
+ const char *name;
+ char linebuf[4096];
+ fssearch_t *search;
+ search = FS_Search(pattern, true, true);
+ if (!search)
+ return 0;
+ numfiles = search->numfilenames;
+ if (!oneperline)
+ {
+ // FIXME: the names could be added to one column list and then
+ // gradually shifted into the next column if they fit, and then the
+ // next to make a compact variable width listing but it's a lot more
+ // complicated...
+ // find width for columns
+ columnwidth = 0;
+ for (i = 0;i < numfiles;i++)
+ {
+ l = strlen(search->filenames[i]);
+ if (columnwidth < l)
+ columnwidth = l;
+ }
+ // count the spacing character
+ columnwidth++;
+ // calculate number of columns
+ numcolumns = con_linewidth / columnwidth;
+ // don't bother with the column printing if it's only one column
+ if (numcolumns >= 2)
+ {
+ numlines = (numfiles + numcolumns - 1) / numcolumns;
+ for (i = 0;i < numlines;i++)
+ {
+ linebufpos = 0;
+ for (k = 0;k < numcolumns;k++)
+ {
+ l = i * numcolumns + k;
+ if (l < numfiles)
+ {
+ name = search->filenames[l];
+ for (j = 0;name[j] && j < (int)sizeof(linebuf) - 1;j++)
+ linebuf[linebufpos++] = name[j];
+ // space out name unless it's the last on the line
+ if (k < (numcolumns - 1) && l < (numfiles - 1))
+ for (;j < columnwidth && j < (int)sizeof(linebuf) - 1;j++)
+ linebuf[linebufpos++] = ' ';
+ }
+ }
+ linebuf[linebufpos] = 0;
+ Con_Printf("%s\n", linebuf);
+ }
+ }
+ else
+ oneperline = true;
+ }
+ if (oneperline)
+ for (i = 0;i < numfiles;i++)
+ Con_Printf("%s\n", search->filenames[i]);
+ FS_FreeSearch(search);
+ return numfiles;
+}
+
+static void FS_ListDirectoryCmd (const char* cmdname, int oneperline)
+{
+ const char *pattern;
+ if (Cmd_Argc() > 3)
+ {
+ Con_Printf("usage:\n%s [path/pattern]\n", cmdname);
+ return;
+ }
+ if (Cmd_Argc() == 2)
+ pattern = Cmd_Argv(1);
+ else
+ pattern = "*";
+ if (!FS_ListDirectory(pattern, oneperline))
+ Con_Print("No files found.\n");
+}
+
+void FS_Dir_f(void)
+{
+ FS_ListDirectoryCmd("dir", true);
+}
+
+void FS_Ls_f(void)
+{
+ FS_ListDirectoryCmd("ls", false);
+}
+