]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/vfspk3/vfs.cpp
ec44fc9ff71e9be458a6ed6026300e9c6c9f405c
[xonotic/netradiant.git] / plugins / vfspk3 / vfs.cpp
1 /*
2 Copyright (c) 2001, Loki software, inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
10
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14
15 Neither the name of Loki software nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
17 written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 //
32 // Rules:
33 //
34 // - Directories should be searched in the following order: ~/.q3a/baseq3,
35 //   install dir (/usr/local/games/quake3/baseq3) and cd_path (/mnt/cdrom/baseq3).
36 //
37 // - Pak files are searched first inside the directories.
38 // - Case insensitive.
39 // - Unix-style slashes (/) (windows is backwards .. everyone knows that)
40 //
41 // Leonardo Zide (leo@lokigames.com)
42 //
43
44 #include <glib.h>
45 #include <stdio.h>
46
47 #if defined (__linux__) || defined (__APPLE__)
48   #include <dirent.h>
49   #include <unistd.h>
50 #else
51   #include <wtypes.h>
52   #include <io.h>
53   #define R_OK 04
54   #define S_ISDIR(mode) (mode & _S_IFDIR)
55 #endif
56
57 // TTimo: String functions
58 //   see http://www.qeradiant.com/faq/index.cgi?file=175
59 #include "str.h"
60
61 #include <stdlib.h>
62 #include <sys/stat.h>
63
64 #include "vfspk3.h"
65 #include "vfs.h"
66 #include "unzip-vfspk3.h"
67
68 typedef struct
69 {
70   char*   name;
71   unz_s   zipinfo;
72   unzFile zipfile;
73   guint32   size;
74 } VFS_PAKFILE;
75
76 // =============================================================================
77 // Global variables
78
79 static GSList* g_unzFiles;
80 static GSList* g_pakFiles;
81 static char    g_strDirs[VFS_MAXDIRS][PATH_MAX];
82 static int     g_numDirs;
83 static bool    g_bUsePak = true;
84
85 // =============================================================================
86 // Static functions
87
88 static void vfsAddSlash (char *str)
89 {
90   int n = strlen (str);
91   if (n > 0)
92   {
93     if (str[n-1] != '\\' && str[n-1] != '/')
94       strcat (str, "/");
95   }
96 }
97
98 static void vfsFixDOSName (char *src)
99 {
100   if (src == NULL)
101     return;
102
103   while (*src)
104   {
105     if (*src == '\\')
106       *src = '/';
107     src++;
108   }
109 }
110
111 static void vfsInitPakFile (const char *filename)
112 {
113   unz_global_info gi;
114   unzFile uf;
115   guint32 i;
116   int err;
117
118   uf = unzOpen (filename);
119   if (uf == NULL)
120   {
121     g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "  failed to init pak file %s\n", filename);
122     return;
123   }
124   g_FuncTable.m_pfnSysPrintf("  pak file: %s\n", filename);
125
126   g_unzFiles = g_slist_append (g_unzFiles, uf);
127
128   err = unzGetGlobalInfo (uf,&gi);
129   if (err != UNZ_OK)
130     return;
131   unzGoToFirstFile(uf);
132
133   for (i = 0; i < gi.number_entry; i++)
134   {
135     char filename_inzip[NAME_MAX];
136     unz_file_info file_info;
137     VFS_PAKFILE* file;
138
139     err = unzGetCurrentFileInfo (uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
140     if (err != UNZ_OK)
141       break;
142
143     file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));
144     g_pakFiles = g_slist_append (g_pakFiles, file);
145
146     vfsFixDOSName (filename_inzip);
147     g_strdown (filename_inzip);
148
149     file->name = g_strdup (filename_inzip);
150     file->size = file_info.uncompressed_size;
151     file->zipfile = uf;
152     memcpy (&file->zipinfo, uf, sizeof (unz_s));
153
154     if ((i+1) < gi.number_entry)
155     {
156       err = unzGoToNextFile(uf);
157       if (err!=UNZ_OK)
158         break;
159     }
160   }
161 }
162
163 static GSList* vfsGetListInternal (const char *refdir, const char *ext, bool directories)
164 {
165   GSList *lst, *lst_aux, *files = NULL;
166   char dirname[NAME_MAX], extension[NAME_MAX], filename[NAME_MAX];
167   char basedir[NAME_MAX];
168   int dirlen;
169   char *ptr;
170   char *dirlist;
171   struct stat st;
172   GDir *diskdir;
173   int i;
174
175   if (refdir != NULL)
176   {
177     strcpy (dirname, refdir);
178     g_strdown (dirname);
179     vfsFixDOSName (dirname);
180     vfsAddSlash (dirname);
181   } else
182     dirname[0] = '\0';
183   dirlen = strlen (dirname);
184
185   if (ext != NULL)
186     strcpy (extension, ext);
187   else
188     extension[0] = '\0';
189   g_strdown (extension);
190
191   for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
192   {
193     VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
194     gboolean found = FALSE;
195     ptr = file->name;
196
197     // check that the file name begins with dirname
198     for (i = 0; (*ptr && i < dirlen); i++, ptr++)
199       if (*ptr != dirname[i])
200         break;
201
202     if (i != dirlen)
203       continue;
204
205     if (directories)
206     {
207       char *sep = strchr (ptr, '/');
208       if (sep == NULL)
209         continue;
210
211       i = sep-ptr;
212
213       // check for duplicates
214       for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
215         if (strncmp ((char*)lst_aux->data, ptr, i) == 0)
216         {
217           found = TRUE;
218           break;
219         }
220
221       if (!found)
222       {
223         char *name = g_strndup (ptr, i+1);
224         name[i] = '\0';
225         files = g_slist_append (files, name);
226       }
227     } else
228     {
229       // check extension
230       char *ptr_ext = strrchr (ptr, '.');
231       if ((ext != NULL) && ((ptr_ext == NULL) || (strcmp (ptr_ext+1, extension) != 0)))
232         continue;
233
234       // check for duplicates
235       for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
236         if (strcmp ((char*)lst_aux->data, ptr) == 0)
237         {
238           found = TRUE;
239           break;
240         }
241
242       if (!found)
243         files = g_slist_append (files, g_strdup (ptr));
244     }
245   }
246
247   for (i = 0; i < g_numDirs; i++)
248   {
249     strcpy (basedir, g_strDirs[i]);
250     strcat (basedir, dirname);
251
252     diskdir = g_dir_open (basedir, 0, NULL);
253
254     if (diskdir != NULL)
255     {
256       while (1)
257       {
258         const char* name = g_dir_read_name(diskdir);
259         if(name == NULL)
260           break;
261
262         if (directories && (name[0] == '.'))
263           continue;
264
265         sprintf (filename, "%s%s", basedir, name);
266         stat (filename, &st);
267
268         if ((S_ISDIR (st.st_mode) != 0) != directories)
269           continue;
270
271         gboolean found = FALSE;
272
273         dirlist = g_strdup(name);
274
275         g_strdown (dirlist);
276
277         char *ptr_ext = strrchr (dirlist, '.');
278         if(ext == NULL
279           || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))
280         {
281
282           // check for duplicates
283           for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
284             if (strcmp ((char*)lst_aux->data, dirlist) == 0)
285             {
286               found = TRUE;
287               break;
288             }
289
290           if (!found)
291             files = g_slist_append (files, g_strdup (dirlist));
292         }
293
294         g_free(dirlist);
295       }
296       g_dir_close (diskdir);
297     }
298   }
299
300   return files;
301 }
302
303 /*!
304 This behaves identically to -stricmp(a,b), except that ASCII chars
305 [\]^`_ come AFTER alphabet chars instead of before. This is because
306 it effectively converts all alphabet chars to uppercase before comparison,
307 while stricmp converts them to lowercase.
308 */
309 //!\todo Analyse the code in rtcw/q3 to see how it behaves.
310 static int vfsPakSort (const void *a, const void *b)
311 {
312         char    *s1, *s2;
313         int             c1, c2;
314
315         s1 = (char*)a;
316         s2 = (char*)b;
317
318         do {
319                 c1 = *s1++;
320                 c2 = *s2++;
321
322                 if (c1 >= 'a' && c1 <= 'z')
323                 {
324                         c1 -= ('a' - 'A');
325                 }
326                 if (c2 >= 'a' && c2 <= 'z')
327                 {
328                         c2 -= ('a' - 'A');
329                 }
330
331                 if ( c1 == '\\' || c1 == ':' )
332                 {
333                         c1 = '/';
334                 }
335                 if ( c2 == '\\' || c2 == ':' )
336                 {
337                         c2 = '/';
338                 }
339
340                 // Arnout: note - sort pakfiles in reverse order. This ensures that
341                 // later pakfiles override earlier ones. This because the vfs module
342                 // returns a filehandle to the first file it can find (while it should
343                 // return the filehandle to the file in the most overriding pakfile, the
344                 // last one in the list that is).
345                 if (c1 < c2)
346                 {
347                         //return -1;            // strings not equal
348                         return 1;               // strings not equal
349                 }
350                 if (c1 > c2)
351                 {
352                         //return 1;
353                         return -1;
354                 }
355         } while (c1);
356
357         return 0;               // strings are equal
358 }
359
360 // =============================================================================
361 // Global functions
362
363 // reads all pak files from a dir
364 /*!
365 The gamemode hacks in here will do undefined things with files called zz_*.
366 This is simple to fix by cleaning up the hacks, but may be better left alone
367 if the engine code does the same thing.
368 */
369 void vfsInitDirectory (const char *path)
370 {
371   char filename[PATH_MAX];
372   GDir *dir;
373         GSList *dirlist = NULL;
374         int iGameMode; // 0: no filtering 1: SP 2: MP
375
376   if (g_numDirs == (VFS_MAXDIRS-1))
377     return;
378
379   // See if we are in "sp" or "mp" mapping mode
380   const char* gamemode = g_FuncTable.m_pfnReadProjectKey("gamemode");
381
382         if (gamemode)
383         {
384                 if (strcmp (gamemode, "sp") == 0)
385                         iGameMode = 1;
386                 else if (strcmp (gamemode, "mp") == 0)
387                         iGameMode = 2;
388                 else
389                         iGameMode = 0;
390         } else
391                 iGameMode = 0;
392
393   strcpy (g_strDirs[g_numDirs], path);
394   vfsFixDOSName (g_strDirs[g_numDirs]);
395   vfsAddSlash (g_strDirs[g_numDirs]);
396   g_numDirs++;
397
398   if (g_bUsePak)
399   {
400     dir = g_dir_open (path, 0, NULL);
401
402     if (dir != NULL)
403     {
404       g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);
405
406       for(;;)
407       {
408         const char* name = g_dir_read_name(dir);
409         if(name == NULL)
410           break;
411
412         char *ext = (char*)strrchr(name, '.');
413         if ((ext == NULL) || (strcasecmp (ext, ".pk3") != 0))
414           continue;
415
416         char* direntry = g_strdup(name);
417
418                                 // using the same kludge as in engine to ensure consistency
419                                 switch (iGameMode)
420                                 {
421                                 case 1: // SP
422                                         if (strncmp(direntry,"sp_",3) == 0)
423                                                 memcpy(direntry,"zz",2);
424                                         break;
425                                 case 2: // MP
426                                         if (strncmp(direntry,"mp_",3) == 0)
427                                                 memcpy(direntry,"zz",2);
428                                         break;
429                                 }
430
431                                 dirlist = g_slist_append (dirlist, direntry);
432       }
433
434       g_dir_close (dir);
435
436                         // sort them
437                         dirlist = g_slist_sort (dirlist, vfsPakSort);
438
439                         // add the entries to the vfs and free the list
440                         while (dirlist)
441                         {
442                                 GSList *cur = dirlist;
443                                 char* name = (char*)cur->data;
444
445                                 switch (iGameMode)
446                                 {
447                                 case 1: // SP
448                                         if (strncmp(name,"mp_",3) == 0)
449                                         {
450                                                 g_free (name);
451                                                 dirlist = g_slist_remove (cur, name);
452                                                 continue;
453                                         } else if (strncmp(name,"zz_",3) == 0)
454                                                 memcpy(name,"sp",2);
455                                         break;
456                                 case 2: // MP
457                                         if (strncmp(name,"sp_",3) == 0)
458                                         {
459                                                 g_free (name);
460                                                 dirlist = g_slist_remove (cur, name);
461                                                 continue;
462                                         } else if (strncmp(name,"zz_",3) == 0)
463                                                 memcpy(name,"mp",2);
464                                         break;
465                                 }
466
467                                 sprintf (filename, "%s/%s", path, name);
468         vfsInitPakFile (filename);
469
470                                 g_free (name);
471                                 dirlist = g_slist_remove (cur, name);
472                         }
473     }
474     else
475       g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);
476   }
477 }
478
479 // frees all memory that we allocated
480 // FIXME TTimo this should be improved so that we can shutdown and restart the VFS without exiting Radiant?
481 //   (for instance when modifying the project settings)
482 void vfsShutdown ()
483 {
484   while (g_unzFiles)
485   {
486     unzClose ((unzFile)g_unzFiles->data);
487     g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);
488   }
489
490   // avoid dangling pointer operation (makes BC hangry)
491   GSList *cur = g_pakFiles;
492   GSList *next = cur;
493   while (next)
494   {
495     cur = next;
496     VFS_PAKFILE* file = (VFS_PAKFILE*)cur->data;
497     g_free (file->name);
498     g_free (file);
499     next = g_slist_remove (cur, file);
500   }
501   g_pakFiles = NULL;
502 }
503
504 void vfsFreeFile (void *p)
505 {
506   g_free(p);
507 }
508
509 GSList* vfsGetFileList (const char *dir, const char *ext)
510 {
511   return vfsGetListInternal (dir, ext, false);
512 }
513
514 GSList* vfsGetDirList (const char *dir)
515 {
516   return vfsGetListInternal (dir, NULL, true);
517 }
518
519 void vfsClearFileDirList (GSList **lst)
520 {
521   while (*lst)
522   {
523     g_free ((*lst)->data);
524     *lst = g_slist_remove (*lst, (*lst)->data);
525   }
526 }
527
528 int vfsGetFileCount (const char *filename, int flag)
529 {
530   int i, count = 0;
531   char fixed[NAME_MAX], tmp[NAME_MAX];
532   GSList *lst;
533
534   strcpy (fixed, filename);
535   vfsFixDOSName (fixed);
536   g_strdown (fixed);
537
538   if (!flag || (flag & VFS_SEARCH_PAK))
539   {
540     for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
541     {
542       VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
543
544       if (strcmp (file->name, fixed) == 0)
545         count++;
546     }
547   }
548
549   if (!flag || (flag & VFS_SEARCH_DIR))
550   {
551     for (i = 0; i < g_numDirs; i++)
552     {
553       strcpy (tmp, g_strDirs[i]);
554       strcat (tmp, fixed);
555       if (access (tmp, R_OK) == 0)
556         count++;
557     }
558   }
559
560   return count;
561 }
562
563 // open a full path file
564 int vfsLoadFullPathFile (const char *filename, void **bufferptr)
565 {
566   FILE *f;
567   long len;
568
569   f = fopen (filename, "rb");
570   if (f == NULL)
571     return -1;
572
573   fseek (f, 0, SEEK_END);
574   len = ftell (f);
575   rewind (f);
576
577   *bufferptr = g_malloc (len+1);
578   if (*bufferptr == NULL)
579     return -1;
580
581   fread (*bufferptr, 1, len, f);
582   fclose (f);
583
584   // we need to end the buffer with a 0
585   ((char*) (*bufferptr))[len] = 0;
586
587   return len;
588 }
589
590 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
591 int vfsLoadFile (const char *filename, void **bufferptr, int index)
592 {
593   int i, count = 0;
594   char tmp[NAME_MAX], fixed[NAME_MAX];
595   GSList *lst;
596
597   *bufferptr = NULL;
598   strcpy (fixed, filename);
599   vfsFixDOSName (fixed);
600   g_strdown (fixed);
601
602   for (i = 0; i < g_numDirs; i++)
603   {
604     strcpy (tmp, g_strDirs[i]);
605     strcat (tmp, filename);
606     if (access (tmp, R_OK) == 0)
607     {
608       if (count == index)
609       {
610         return vfsLoadFullPathFile(tmp,bufferptr);
611       }
612
613       count++;
614     }
615   }
616
617   for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
618   {
619     VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
620
621     if (strcmp (file->name, fixed) != 0)
622       continue;
623
624     if (count == index)
625     {
626       memcpy (file->zipfile, &file->zipinfo, sizeof (unz_s));
627
628       if (unzOpenCurrentFile (file->zipfile) != UNZ_OK)
629         return -1;
630
631       *bufferptr = g_malloc (file->size+1);
632       // we need to end the buffer with a 0
633       ((char*) (*bufferptr))[file->size] = 0;
634
635       i = unzReadCurrentFile (file->zipfile , *bufferptr, file->size);
636       unzCloseCurrentFile (file->zipfile);
637       if (i > 0)
638         return file->size;
639       else
640         return -1;
641     }
642
643     count++;
644   }
645
646   return -1;
647 }
648
649 //#ifdef _DEBUG
650 #if 1
651   #define DBG_RLTPATH
652 #endif
653
654 /*!
655 \param shorten will try to match against the short version
656 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=144
657 recent switch back to short path names in project settings has broken some stuff
658 with shorten == true, we will convert in to short version before looking for root
659 FIXME WAAA .. the stuff below is much more simple on linux .. add appropriate #ifdef
660 */
661 char* vfsExtractRelativePath_short(const char *in, bool shorten)
662 {
663   int i;
664   char l_in[PATH_MAX];
665   char check[PATH_MAX];
666   static char out[PATH_MAX];
667   out[0] = 0;
668
669 #ifdef DBG_RLTPATH
670   Sys_Printf("vfsExtractRelativePath: %s\n", in);
671 #endif
672
673 #ifdef _WIN32
674   if (shorten)
675   {
676     // make it short
677     if (GetShortPathName(in, l_in, PATH_MAX) == 0)
678     {
679 #ifdef DBG_RLTPATH
680       Sys_Printf("GetShortPathName failed\n");
681 #endif
682       return NULL;
683     }
684   }
685   else
686   {
687     strcpy(l_in,in);
688   }
689   vfsCleanFileName(l_in);
690 #else
691   strcpy(l_in, in);
692   vfsCleanFileName(l_in);
693 #endif // ifdef WIN32
694
695
696 #ifdef DBG_RLTPATH
697   Sys_Printf("cleaned path: %s\n", l_in);
698 #endif
699
700   for (i = 0; i < g_numDirs; i++)
701   {
702     strcpy(check,g_strDirs[i]);
703     vfsCleanFileName(check);
704 #ifdef DBG_RLTPATH
705     Sys_Printf("Matching against %s\n", check);
706 #endif
707
708     // try to find a match
709     if (strstr(l_in, check))
710     {
711       strcpy(out,l_in+strlen(check)+1);
712       break;
713     }
714
715   }
716   if (out[0]!=0)
717   {
718 #ifdef DBG_RLTPATH
719     Sys_Printf("vfsExtractRelativePath: success\n");
720 #endif
721     return out;
722   }
723 #ifdef DBG_RLTPATH
724   Sys_Printf("vfsExtractRelativePath: failed\n");
725 #endif
726   return NULL;
727 }
728
729
730 // FIXME TTimo: this and the above should be merged at some point
731 char* vfsExtractRelativePath(const char *in)
732 {
733   static char out[PATH_MAX];
734   unsigned int i, count;
735   char *chunk, *backup = NULL; // those point to out stuff
736   char *ret = vfsExtractRelativePath_short(in, false);
737   if (!ret)
738   {
739 #ifdef DBG_RLTPATH
740     Sys_Printf("trying with a short version\n");
741 #endif
742     ret = vfsExtractRelativePath_short(in, true);
743     if (ret)
744     {
745       // ok, but we have a relative short version now
746       // hack the long relative version out of here
747       count = 0;
748       for(i=0;i<strlen(ret);i++)
749       {
750         if (ret[i]=='/')
751           count++;
752       }
753       // this is the clean, not short version
754       strcpy(out, in);
755       vfsCleanFileName(out);
756       for(i=0;i<=count;i++)
757       {
758         chunk = strrchr(out, '/');
759         if (backup)
760           backup[0] = '/';
761         chunk[0] = '\0';
762         backup = chunk;
763       }
764       return chunk+1;
765     }
766   }
767   return ret;
768 }
769
770 void vfsCleanFileName(char *in)
771 {
772   strlwr(in);
773   vfsFixDOSName(in);
774   int n = strlen(in);
775   if (in[n-1] == '/')
776     in[n-1] = '\0';
777 }
778
779 // HYDRA: this now searches VFS/PAK files in addition to the filesystem
780 // if FLAG is unspecified then ONLY dirs are searched.
781 // PAK's are searched before DIRs to mimic engine behaviour
782 // index is ignored when searching PAK files.
783 // see ifilesystem.h
784 char* vfsGetFullPath(const char *in, int index, int flag)
785 {
786   int count = 0;
787   static char out[PATH_MAX];
788   char tmp[NAME_MAX];
789   int i;
790
791   if (flag & VFS_SEARCH_PAK)
792   {
793     char fixed[NAME_MAX];
794     GSList *lst;
795
796     strcpy (fixed, in);
797     vfsFixDOSName (fixed);
798     g_strdown (fixed);
799
800     for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
801     {
802       VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
803
804       char *ptr,*lastptr;
805       lastptr = file->name;
806
807       while ((ptr = strchr(lastptr,'/')) != NULL)
808         lastptr = ptr+1;
809
810       if (strcmp (lastptr, fixed) == 0)
811       {
812         strncpy(out,file->name,PATH_MAX);
813         return out;
814       }
815     }
816
817   }
818
819   if (!flag || (flag & VFS_SEARCH_DIR))
820   {
821   for (i = 0; i < g_numDirs; i++)
822   {
823     strcpy (tmp, g_strDirs[i]);
824     strcat (tmp, in);
825     if (access (tmp, R_OK) == 0)
826     {
827       if (count == index)
828       {
829         strcpy (out, tmp);
830         return out;
831       }
832       count++;
833     }
834   }
835   }
836   return NULL;
837 }
838
839
840 // TODO TTimo on linux the base prompt is ~/.q3a/<fs_game>
841 // given the file dialog, we could push the strFSBasePath and ~/.q3a into the directory shortcuts
842 // FIXME TTimo is this really a VFS functionality?
843 //   actually .. this should be the decision of the core isn't it?
844 //   or .. add an API so that the base prompt can be set during VFS init
845 const char* vfsBasePromptPath()
846 {
847 #ifdef _WIN32
848   static const char* path = "C:";
849 #else
850   static const char* path = "/";
851 #endif
852   return path;
853 }
854