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