]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/vfswad/vfs.cpp
some updates to the Linux build system - obtained a core binary and all required...
[xonotic/netradiant.git] / plugins / vfswad / vfs.cpp
1 /*\r
2 Copyright (c) 2001, Loki software, inc.\r
3 All rights reserved.\r
4 \r
5 Redistribution and use in source and binary forms, with or without modification,\r
6 are permitted provided that the following conditions are met:\r
7 \r
8 Redistributions of source code must retain the above copyright notice, this list\r
9 of conditions and the following disclaimer.\r
10 \r
11 Redistributions in binary form must reproduce the above copyright notice, this\r
12 list of conditions and the following disclaimer in the documentation and/or\r
13 other materials provided with the distribution.\r
14 \r
15 Neither the name of Loki software nor the names of its contributors may be used\r
16 to endorse or promote products derived from this software without specific prior\r
17 written permission.\r
18 \r
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''\r
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY\r
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\r
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
29 */\r
30 \r
31 //\r
32 // Rules:\r
33 //\r
34 // - Directories should be searched in the following order: ~/.q3a/baseq3,\r
35 //   install dir (/usr/local/games/quake3/baseq3) and cd_path (/mnt/cdrom/baseq3).\r
36 //\r
37 // - Pak files are searched first inside the directories.\r
38 // - Case insensitive.\r
39 // - Unix-style slashes (/) (windows is backwards .. everyone knows that)\r
40 //\r
41 // Leonardo Zide (leo@lokigames.com)\r
42 //\r
43 \r
44 #include <glib.h>\r
45 #include <stdio.h>\r
46 \r
47 #if defined (__linux__) || defined (__APPLE__)\r
48   #include <dirent.h>\r
49   #include <unistd.h>\r
50 #else\r
51   #include <wtypes.h>\r
52   #include <io.h>\r
53   #define R_OK 04\r
54   #define S_ISDIR(mode) (mode & _S_IFDIR)\r
55 #endif\r
56 \r
57 // TTimo: String functions\r
58 //   see http://www.qeradiant.com/faq/index.cgi?file=175\r
59 #include "str.h"\r
60 \r
61 #include <stdlib.h>\r
62 #include <sys/stat.h>\r
63 \r
64 #include "vfswad.h"\r
65 #include "vfs.h"\r
66 #include "unwad.h"\r
67 \r
68 typedef struct\r
69 {\r
70   char*         name;\r
71   WAD3_LUMP     wadlump;\r
72   wadFile_t     *wadfile;\r
73   unsigned long filenumber;\r
74   unsigned long size;\r
75 } VFS_PAKFILE;\r
76 \r
77 // =============================================================================\r
78 // Global variables\r
79 \r
80 static GSList* g_wadFiles;\r
81 static GSList* g_pakFiles;\r
82 static char    g_strDirs[VFS_MAXDIRS][PATH_MAX];\r
83 static int     g_numDirs;\r
84 \r
85 // =============================================================================\r
86 // Static functions\r
87 \r
88 static void vfsAddSlash (char *str)\r
89 {\r
90   int n = strlen (str);\r
91   if (n > 0)\r
92   {\r
93     if (str[n-1] != '\\' && str[n-1] != '/')\r
94       strcat (str, "/");\r
95   }\r
96 }\r
97 \r
98 static void vfsFixDOSName (char *src)\r
99 {\r
100   if (src == NULL)\r
101     return;\r
102 \r
103   while (*src)\r
104   {\r
105     if (*src == '\\')\r
106       *src = '/';\r
107     src++;\r
108   }\r
109 }\r
110 \r
111 //FIXME: STUPID short filenames.. get RID of it asap\r
112 // copied verbatim from qe3.cpp\r
113 int vfsBuildShortPathName(const char* pPath, char* pBuffer, int nBufferLen)\r
114 {\r
115 #ifdef _WIN32\r
116   char *pFile = NULL;\r
117   int nResult = GetFullPathName(pPath, nBufferLen, pBuffer, &pFile);\r
118   nResult = GetShortPathName(pPath, pBuffer, nBufferLen);\r
119   if (nResult == 0)\r
120     strcpy(pBuffer, pPath);                     // Use long filename\r
121   return nResult;\r
122 #endif\r
123 \r
124 #if defined (__linux__) || defined (__APPLE__)\r
125 \r
126   // remove /../ from directories\r
127   const char *scr = pPath; char *dst = pBuffer;\r
128   for (int i = 0; (i < nBufferLen) && (*scr != 0); i++)\r
129   {\r
130     if (*scr == '/' && *(scr+1) == '.' && *(scr+2) == '.')\r
131     {\r
132       scr += 3;\r
133       while (dst != pBuffer && *(--dst) != '/')\r
134       {\r
135         i--;\r
136       }\r
137     }\r
138 \r
139     *dst = *scr;\r
140 \r
141     scr++; dst++;\r
142   }\r
143   *dst = 0;\r
144 \r
145   return strlen (pBuffer);\r
146 #endif\r
147 }\r
148 \r
149 static void vfsInitPakFile (const char *filename)\r
150 {\r
151   wadFile_t *wf;\r
152   unsigned int i;\r
153   int err;\r
154   char *wadnameptr;\r
155   char wadname[NAME_MAX];\r
156 \r
157   wf = wadOpen (filename);\r
158   if (wf == NULL)\r
159   {\r
160     g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "  failed to init wad file %s\n", filename);\r
161     return;\r
162   }\r
163   g_FuncTable.m_pfnSysPrintf("  wad file: %s\n", filename);\r
164 \r
165   for (i = strlen(filename)-1 ; i >= 0 && filename[i] != '\\' && filename[i] != '/' ; i --)\r
166     wadnameptr = (char *)filename + i;\r
167 \r
168   strcpy(wadname,wadnameptr);\r
169   wadname[strlen(wadname)-4] = 0; // ditch the .wad so everthing looks nice!\r
170 \r
171   g_wadFiles = g_slist_append (g_wadFiles, wf); // store the wadfile handle\r
172 \r
173   wadGoToFirstFile(wf);\r
174 \r
175   for (i = 0; i < wf->lpHeader->numlumps; i++)\r
176   {\r
177     char filename_inwad[NAME_MAX];\r
178     char filename_inwadfixed[NAME_MAX];\r
179     unsigned long filesize;\r
180     VFS_PAKFILE* file;\r
181 \r
182     err = wadGetCurrentFileInfo (wf, filename_inwad, sizeof(filename_inwad) - 5, &filesize); // -5 for extension + null terminator\r
183     if (err != 1)\r
184       break;\r
185 \r
186     file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));\r
187     g_pakFiles = g_slist_append (g_pakFiles, file);\r
188 \r
189     vfsFixDOSName (filename_inwad);\r
190     g_strdown (filename_inwad);\r
191 \r
192     // texturenames in wad files don't have an extensions or paths, so we must add them!\r
193     if (wf->lpLump->type == WAD2_TYPE_MIP)\r
194     {\r
195       sprintf(filename_inwadfixed,"textures/%s/%s.mip",wadname,filename_inwad);\r
196     }else {\r
197       sprintf(filename_inwadfixed,"textures/%s/%s.hlw",wadname,filename_inwad);\r
198     }\r
199 \r
200     //g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "  scanned %s\\%s\n", filename,filename_inwad);\r
201 \r
202     file->name = g_strdup (filename_inwadfixed);\r
203     file->size = filesize;\r
204     file->filenumber = wf->currentfile;\r
205     file->wadfile = wf;\r
206     memcpy(&file->wadlump, wf->lpLump, sizeof(WAD3_LUMP));\r
207 \r
208     err = wadGoToNextFile(wf);\r
209     if (err != 1)\r
210       break;\r
211   }\r
212 }\r
213 \r
214 static GSList* vfsGetListInternal (const char *refdir, const char *ext, bool directories)\r
215 {\r
216   GSList *lst, *lst_aux, *files = NULL;\r
217   char dirname[NAME_MAX], extension[NAME_MAX], filename[NAME_MAX];\r
218   char basedir[NAME_MAX];\r
219   int dirlen;\r
220   char *ptr;\r
221   struct stat st;\r
222   int i;\r
223 \r
224   if (refdir != NULL)\r
225   {\r
226     strcpy (dirname, refdir);\r
227     g_strdown (dirname);\r
228     vfsFixDOSName (dirname);\r
229     vfsAddSlash (dirname);\r
230   } else\r
231     dirname[0] = '\0';\r
232   dirlen = strlen (dirname);\r
233 \r
234   if (ext != NULL)\r
235     strcpy (extension, ext);\r
236   else\r
237     extension[0] = '\0';\r
238   g_strdown (extension);\r
239 \r
240   for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))\r
241   {\r
242     VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;\r
243     gboolean found = FALSE;\r
244     ptr = file->name;\r
245 \r
246     // check that the file name begins with dirname\r
247     for (i = 0; (*ptr && i < dirlen); i++, ptr++)\r
248       if (*ptr != dirname[i])\r
249         break;\r
250 \r
251     if (i != dirlen)\r
252       continue;\r
253 \r
254     if (directories)\r
255     {\r
256       char *sep = strchr (ptr, '/');\r
257       if (sep == NULL)\r
258         continue;\r
259 \r
260       i = sep-ptr;\r
261 \r
262       // check for duplicates\r
263       for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))\r
264         if (strncmp ((char*)lst_aux->data, ptr, i) == 0)\r
265         {\r
266           found = TRUE;\r
267           break;\r
268         }\r
269 \r
270       if (!found)\r
271       {\r
272         char *name = g_strndup (ptr, i+1);\r
273         name[i] = '\0';\r
274         files = g_slist_append (files, name);\r
275       }\r
276     } else\r
277     {\r
278       // check extension\r
279       char *ptr_ext = strrchr (ptr, '.');\r
280       if ((ext != NULL) && ((ptr_ext == NULL) || (strcmp (ptr_ext+1, extension) != 0)))\r
281         continue;\r
282 \r
283       // check for duplicates\r
284       for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))\r
285         if (strcmp ((char*)lst_aux->data, ptr) == 0)\r
286         {\r
287           found = TRUE;\r
288           break;\r
289         }\r
290 \r
291       if (!found)\r
292         files = g_slist_append (files, g_strdup (ptr));\r
293     }\r
294   }\r
295 \r
296   for (i = 0; i < g_numDirs; i++)\r
297   {\r
298     strcpy (basedir, g_strDirs[i]);\r
299     strcat (basedir, dirname);\r
300 \r
301     GDir* dir = g_dir_open (basedir, 0, NULL);\r
302 \r
303     if (dir != NULL)\r
304     {\r
305       for(;;)\r
306       {\r
307         const char* name = g_dir_read_name(dir);\r
308         if(name == NULL)\r
309           break;\r
310 \r
311         if (directories && (name[0] == '.'))\r
312           continue;\r
313 \r
314         sprintf (filename, "%s%s", basedir, name);\r
315         stat (filename, &st);\r
316 \r
317         if ((S_ISDIR (st.st_mode) != 0) != directories)\r
318           continue;\r
319 \r
320         gboolean found = FALSE;\r
321 \r
322         char* direntry = g_strdup(name);\r
323 \r
324         g_strdown (direntry);\r
325 \r
326         char *ptr_ext = strrchr (direntry, '.');\r
327 \r
328         if(ext == NULL\r
329           || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))\r
330         {\r
331 \r
332           // check for duplicates\r
333           for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))\r
334             if (strcmp ((char*)lst_aux->data, direntry) == 0)\r
335             {\r
336               found = TRUE;\r
337               break;\r
338             }\r
339 \r
340           if (!found)\r
341             files = g_slist_append (files, g_strdup (direntry));\r
342         }\r
343 \r
344         g_free(direntry);\r
345       }\r
346       g_dir_close(dir);\r
347     }\r
348   }\r
349 \r
350   return files;\r
351 }\r
352 \r
353 // =============================================================================\r
354 // Global functions\r
355 \r
356 // reads all pak files from a dir\r
357 void vfsInitDirectory (const char *path)\r
358 {\r
359   char filename[PATH_MAX];\r
360 \r
361   if (g_numDirs == (VFS_MAXDIRS-1))\r
362     return;\r
363 \r
364   strcpy (g_strDirs[g_numDirs], path);\r
365   vfsFixDOSName (g_strDirs[g_numDirs]);\r
366   vfsAddSlash (g_strDirs[g_numDirs]);\r
367   g_numDirs++;\r
368 \r
369 //  if (g_PrefsDlg.m_bPAK)\r
370   // TODO: can't read prefs from a module, bah..\r
371   if (1)\r
372   {\r
373     GDir* dir = g_dir_open (path, 0, NULL);\r
374     if (dir != NULL)\r
375     {\r
376       g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);\r
377       while (1)\r
378       {\r
379         const char* name = g_dir_read_name(dir);\r
380         if(name == NULL)\r
381           break;\r
382 \r
383         char *ext = strrchr (name, '.');\r
384         if ((ext == NULL) || (strcmp (ext, ".wad") != 0))\r
385           continue;\r
386 \r
387         sprintf (filename, "%s/%s", path, name);\r
388         vfsInitPakFile (filename);\r
389       }\r
390       g_dir_close (dir);\r
391     } else\r
392       g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);\r
393   }\r
394 }\r
395 \r
396 // frees all memory that we allocated\r
397 // FIXME TTimo this should be improved so that we can shutdown and restart the VFS without exiting Radiant?\r
398 //   (for instance when modifying the project settings)\r
399 void vfsShutdown ()\r
400 {\r
401   wadFile_t *tmpptr;\r
402 \r
403   while (g_wadFiles)\r
404   {\r
405     wadCleanup((wadFile_t *)g_wadFiles->data);\r
406     g_wadFiles = g_slist_remove (g_wadFiles, g_wadFiles->data);\r
407   }\r
408 \r
409   // avoid dangling pointer operation (makes BC hangry)\r
410   GSList *cur = g_pakFiles;\r
411   GSList *next = cur;\r
412   while (next)\r
413   {\r
414     cur = next;\r
415     VFS_PAKFILE* file = (VFS_PAKFILE*)cur->data;\r
416     g_free (file->name);\r
417     g_free (file);\r
418     next = g_slist_remove (cur, file);\r
419   }\r
420   g_pakFiles = NULL;\r
421 }\r
422 \r
423 void vfsFreeFile (void *p)\r
424 {\r
425   g_free(p);\r
426 }\r
427 \r
428 GSList* vfsGetFileList (const char *dir, const char *ext)\r
429 {\r
430   return vfsGetListInternal (dir, ext, false);\r
431 }\r
432 \r
433 GSList* vfsGetDirList (const char *dir)\r
434 {\r
435   return vfsGetListInternal (dir, NULL, true);\r
436 }\r
437 \r
438 void vfsClearFileDirList (GSList **lst)\r
439 {\r
440   while (*lst)\r
441   {\r
442     g_free ((*lst)->data);\r
443     *lst = g_slist_remove (*lst, (*lst)->data);\r
444   }\r
445 }\r
446 \r
447 // return the number of files that match\r
448 int vfsGetFileCount (const char *filename, int flag)\r
449 {\r
450   int i, count = 0;\r
451   char fixed[NAME_MAX], tmp[NAME_MAX];\r
452   GSList *lst;\r
453 \r
454   strcpy (fixed, filename);\r
455   vfsFixDOSName (fixed);\r
456   g_strdown (fixed);\r
457 \r
458   if (!flag || (flag & VFS_SEARCH_PAK))\r
459   {\r
460     for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))\r
461     {\r
462       VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;\r
463 \r
464       if (strcmp (file->name, fixed) == 0)\r
465         count++;\r
466     }\r
467   }\r
468 \r
469   if (!flag || (flag & VFS_SEARCH_DIR))\r
470   {\r
471     for (i = 0; i < g_numDirs; i++)\r
472     {\r
473       strcpy (tmp, g_strDirs[i]);\r
474       strcat (tmp, fixed);\r
475       if (access (tmp, R_OK) == 0)\r
476         count++;\r
477     }\r
478   }\r
479 \r
480   return count;\r
481 }\r
482 \r
483 // open a full path file\r
484 int vfsLoadFullPathFile (const char *filename, void **bufferptr)\r
485 {\r
486   FILE *f;\r
487   long len;\r
488 \r
489   f = fopen (filename, "rb");\r
490   if (f == NULL)\r
491     return -1;\r
492 \r
493   fseek (f, 0, SEEK_END);\r
494   len = ftell (f);\r
495   rewind (f);\r
496 \r
497   *bufferptr = g_malloc (len+1);\r
498   if (*bufferptr == NULL)\r
499     return -1;\r
500 \r
501   fread (*bufferptr, 1, len, f);\r
502   fclose (f);\r
503 \r
504   // we need to end the buffer with a 0\r
505   ((char*) (*bufferptr))[len] = 0;\r
506 \r
507   return len;\r
508 }\r
509 \r
510 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0\r
511 int vfsLoadFile (const char *filename, void **bufferptr, int index)\r
512 {\r
513   int i, count = 0;\r
514   char tmp[NAME_MAX], fixed[NAME_MAX];\r
515   GSList *lst;\r
516 \r
517   *bufferptr = NULL;\r
518   strcpy (fixed, filename);\r
519   vfsFixDOSName (fixed);\r
520   g_strdown (fixed);\r
521 \r
522   for (i = 0; i < g_numDirs; i++)\r
523   {\r
524     strcpy (tmp, g_strDirs[i]);\r
525     strcat (tmp, filename);\r
526     if (access (tmp, R_OK) == 0)\r
527     {\r
528       if (count == index)\r
529       {\r
530         return vfsLoadFullPathFile(tmp,bufferptr);\r
531         /*\r
532   long len;\r
533   FILE *f;\r
534 \r
535   f = fopen (tmp, "rb");\r
536   if (f == NULL)\r
537     return -1;\r
538 \r
539   fseek (f, 0, SEEK_END);\r
540   len = ftell (f);\r
541   rewind (f);\r
542 \r
543   *bufferptr = g_malloc (len+1);\r
544   if (*bufferptr == NULL)\r
545     return -1;\r
546 \r
547   fread (*bufferptr, 1, len, f);\r
548   fclose (f);\r
549 \r
550         // we need to end the buffer with a 0\r
551         ((char*) (*bufferptr))[len] = 0;\r
552 \r
553   return len;\r
554   */\r
555       }\r
556 \r
557       count++;\r
558     }\r
559   }\r
560 \r
561 \r
562   // Textures in HalfLife wads don't have paths, but in the list of files\r
563   // we store the actual full paths of the files and what WAD they're in.\r
564   // so what we have to do is strip the paths and just compare filenames.\r
565 \r
566   // Hydra: well, we did do this, but now we don't, as the map loader now\r
567   // fills in the correct paths for each texture.\r
568 \r
569   /*\r
570   char *searchname;\r
571   char *fixedptr;\r
572 \r
573   fixedptr = fixed;\r
574 \r
575   for (i = strlen(fixed)-1 ; i >= 0 && fixed[i] != '\\' && fixed[i] != '/' ; i --)\r
576     fixedptr = (char *)fixed + i;\r
577   */\r
578   for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))\r
579   {\r
580     VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;\r
581 \r
582 \r
583     /*\r
584     searchname = file->name;\r
585     for (i = strlen(file->name)-1 ; i >= 0 && file->name[i] != '\\' && file->name[i] != '/' ; i --)\r
586       searchname = (char *)file->name + i;\r
587     if (strcmp (searchname, fixedptr) != 0)\r
588       continue;\r
589     */\r
590 \r
591     if (strcmp (file->name, fixed) != 0)\r
592       continue;\r
593 \r
594     if (count == index)\r
595     {\r
596       // Useful for debugging\r
597       //Sys_Printf("VFSWAD: reading from %s\n",file->wadfile->wadfilename);\r
598 \r
599       if (wadOpenCurrentFileByNum (file->wadfile, file->filenumber) != 1)\r
600         return -1;\r
601 \r
602       *bufferptr = g_malloc (file->size+1);\r
603       // we need to end the buffer with a 0\r
604       ((char*) (*bufferptr))[file->size] = 0;\r
605 \r
606       i = wadReadCurrentFile (file->wadfile , (char *)*bufferptr, file->size);\r
607       wadCloseCurrentFile (file->wadfile);\r
608       if (i > 0)\r
609         return file->size;\r
610       else\r
611         return -1;\r
612     }\r
613 \r
614     count++;\r
615   }\r
616 \r
617   return -1;\r
618 }\r
619 \r
620 //#ifdef _DEBUG\r
621 #if 0\r
622   #define DBG_RLTPATH\r
623 #endif\r
624 \r
625 char* vfsExtractRelativePath(const char *in)\r
626 {\r
627   int i;\r
628   char l_in[PATH_MAX];\r
629   char check[PATH_MAX];\r
630   static char out[PATH_MAX];\r
631   out[0] = 0;\r
632 \r
633 #ifdef DBG_RLTPATH\r
634   Sys_Printf("vfsExtractRelativePath: %s\n", in);\r
635 #endif\r
636 \r
637   strcpy(l_in,in);\r
638   vfsCleanFileName(l_in);\r
639 \r
640 #ifdef DBG_RLTPATH\r
641   Sys_Printf("cleaned path: %s\n", l_in);\r
642 #endif\r
643 \r
644   for (i = 0; i < g_numDirs; i++)\r
645   {\r
646     strcpy(check,g_strDirs[i]);\r
647     vfsCleanFileName(check);\r
648 #ifdef DBG_RLTPATH\r
649     Sys_Printf("Matching against %s\n", check);\r
650 #endif\r
651 \r
652     // try to find a match\r
653     if (strstr(l_in, check))\r
654     {\r
655       strcpy(out,l_in+strlen(check)+1);\r
656       break;\r
657     }\r
658   }\r
659   if (out[0]!=0)\r
660   {\r
661 #ifdef DBG_RLTPATH\r
662     Sys_Printf("vfsExtractRelativePath: success\n");\r
663 #endif\r
664     return out;\r
665   }\r
666 #ifdef DBG_RLTPATH\r
667   Sys_Printf("vfsExtractRelativePath: failed\n");\r
668 #endif\r
669   return NULL;\r
670 }\r
671 \r
672 // removed CString usage\r
673 void vfsCleanFileName(char *in)\r
674 {\r
675   char str[PATH_MAX];\r
676   vfsBuildShortPathName (in, str, PATH_MAX);\r
677   strlwr(str);\r
678   vfsFixDOSName(str);\r
679   int n = strlen(str);\r
680   if (str[n-1] == '/')\r
681     str[n-1] = '\0';\r
682   strcpy (in, str);\r
683 }\r
684 \r
685 // HYDRA: this now searches VFS/PAK files in addition to the filesystem\r
686 // if FLAG is unspecified then ONLY dirs are searched.\r
687 // PAK's are searched before DIRs to mimic engine behaviour\r
688 // index is ignored when searching PAK files.\r
689 // see ifilesystem.h\r
690 char* vfsGetFullPath(const char *in, int index, int flag)\r
691 {\r
692   int count = 0;\r
693   static char out[PATH_MAX];\r
694   char tmp[NAME_MAX];\r
695   int i;\r
696 \r
697   if (flag & VFS_SEARCH_PAK)\r
698   {\r
699     char fixed[NAME_MAX];\r
700     GSList *lst;\r
701 \r
702     strcpy (fixed, in);\r
703     vfsFixDOSName (fixed);\r
704     g_strdown (fixed);\r
705 \r
706     for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))\r
707     {\r
708       VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;\r
709 \r
710       char *ptr,*lastptr;\r
711       lastptr = file->name;\r
712 \r
713       while (ptr = strchr(lastptr,'/'))\r
714         lastptr = ptr+1;\r
715 \r
716       if (strcmp (lastptr, fixed) == 0)\r
717       {\r
718         strncpy(out,file->name,PATH_MAX);\r
719         return out;\r
720       }\r
721     }\r
722 \r
723   }\r
724 \r
725   if (!flag || (flag & VFS_SEARCH_DIR))\r
726   {\r
727   for (i = 0; i < g_numDirs; i++)\r
728   {\r
729     strcpy (tmp, g_strDirs[i]);\r
730     strcat (tmp, in);\r
731     if (access (tmp, R_OK) == 0)\r
732     {\r
733       if (count == index)\r
734       {\r
735         strcpy (out, tmp);\r
736         return out;\r
737       }\r
738       count++;\r
739     }\r
740   }\r
741   }\r
742   return NULL;\r
743 }\r
744 \r
745 // TODO TTimo on linux the base prompt is ~/.q3a/<fs_game>\r
746 // given the file dialog, we could push the strFSBasePath and ~/.q3a into the directory shortcuts\r
747 // FIXME TTimo is this really a VFS functionality?\r
748 //   actually .. this should be the decision of the core isn't it?\r
749 //   or .. add an API so that the base prompt can be set during VFS init\r
750 const char* vfsBasePromptPath()\r
751 {\r
752 #ifdef _WIN32\r
753   static char* path = "C:";\r
754 #else\r
755   static char* path = "/";\r
756 #endif\r
757   return path;\r
758 }\r
759 \r