]> icculus.org git repositories - btb/d2x.git/blob - misc/d_glob.c
This commit was manufactured by cvs2svn to create tag 'd2x-0_1_3'.
[btb/d2x.git] / misc / d_glob.c
1 /* Globbing functions for descent. Calls the relevant system handlers... */
2
3 #ifdef HAVE_CONFIG_H
4 #include <conf.h>
5 #endif
6
7 #include "d_glob.h"
8 #include "error.h"
9 #include <string.h>
10 //added 05/17/99 Matt Mueller
11 #include "u_mem.h"
12 //end addition -MM
13
14 #if defined(__DJGPP__) || defined(__linux__)
15 #include <glob.h>
16
17 int d_glob(const char *pattern, d_glob_t *g)
18 {
19  glob_t a;
20  int r;
21
22  Assert(g!=NULL);
23
24  a.gl_offs=0;
25
26  r=glob(pattern,0,NULL,&a);
27  g->gl_pathc=a.gl_pathc;
28  g->gl_pathv=a.gl_pathv;
29  return r;
30 }
31
32 void d_globfree(d_glob_t *g)
33 {
34 #ifndef __linux__ // Linux doesn't believe in freeing glob structures... :-)
35  glob_t a;
36
37  Assert (g!=NULL);
38
39  a.gl_offs=0;
40  a.gl_pathc=g->gl_pathc;
41  a.gl_pathv=g->gl_pathv;
42
43  globfree(&a);
44 #endif
45 }
46
47 #elif defined(__WINDOWS__)
48
49 #include <windows.h>
50
51 #define MAX_GLOB_FILES 500
52
53 /* Using a global variable stops this from being reentrant, but in descent,
54    who cares? */
55 static char *globfiles[MAX_GLOB_FILES];
56
57 int d_glob(const char *pattern, d_glob_t *g)
58 {
59  WIN32_FIND_DATA wfd;
60  HANDLE wfh;
61  int c;
62
63  Assert (g!=NULL);
64  c=0;
65  if ((wfh=FindFirstFile(pattern,&wfd))!=INVALID_HANDLE_VALUE)
66  {
67    do {
68          LPSTR sz = wfd.cAlternateFileName;
69          if (sz == NULL || sz [0] == '\0')
70                 sz = wfd.cFileName;
71      globfiles[c] = d_strdup (*wfd.cAlternateFileName ?
72        wfd.cAlternateFileName : wfd.cFileName);
73
74      c++; // Ho ho ho... :-)
75      if (c>=MAX_GLOB_FILES) return 0;
76    } while (FindNextFile(wfh,&wfd));
77    FindClose(wfh);
78  }
79
80  g->gl_pathc = c;
81  g->gl_pathv = globfiles;
82  return (!c)?-1:0;
83 }
84
85 void d_globfree(d_glob_t *g)
86 {
87  unsigned int i;
88  Assert (g!=NULL);
89  for (i=0; i < g->gl_pathc; i++) {
90    if (globfiles[i]) d_free(globfiles[i]);
91  }
92 }
93
94 #else // Must be good old Watcom C.
95 #error "FIXME: Globbing isn't yet supported under Watcom C. Look at misc/d_glob.c"
96 #endif
97