1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* @(#)msd_dir.c 1.4 87/11/06 Public Domain.
*
* A public domain implementation of BSD directory routines for
* MS-DOS. Written by Michael Rendell ({uunet,utai}michael@garfield),
* August 1897
*
* Modified by Ian Stewartson, Data Logic (istewart@datlog.co.uk).
*
* Updates: 1. To support OS/2 1.x
* 2. To support HPFS long filenames
* 3. To support OS/2 2.x
* 4. To support TurboC
* 5. To support Windows NT
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <dirent.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define FILE_NAME_E cFileName
#define OS_CloseFH(a) FindClose (a)
#define FIND_BUFFER WIN32_FIND_DATA
#define DISABLE_HARD_ERRORS SetErrorMode (0)
#define ENABLE_HARD_ERRORS SetErrorMode (SEM_FAILCRITICALERRORS | \
SEM_NOOPENFILEERRORBOX);
# define ERROR_EMPTY_DIR ERROR_FILE_NOT_FOUND
# define ATTRIBUTES (_A_SUBDIR | _A_HIDDEN | _A_SYSTEM | \
_A_NORMAL | _A_RDONLY | _A_ARCH)
/*
* missing ??
*/
#ifndef ENOTDIR
# define ENOTDIR 120 /* Not a directory */
#endif
#ifndef S_IFMT
# define S_IFMT 0xf000 /* type of file */
#endif
#ifndef S_ISDIR
# define S_ISDIR(m) ((((m) & S_IFMT) == S_IFDIR))
#endif
/*
* Internals
*/
typedef struct _dircontents DIRCONT;
static void free_dircontents (DIRCONT *);
/*
* Open the directory stream
*/
DIR *
opendir (name)
const char *name;
{
struct stat statb;
DIR *dirp;
char *last;
DIRCONT *dp;
char *nbuf;
int len = strlen (name);
unsigned long rc;
FIND_BUFFER dtabuf;
HANDLE d_handle;
bool HPFS = FALSE;
if (!len)
{
errno = ENOTDIR;
return (DIR *)NULL;
}
if ((nbuf = malloc (len + 5)) == (char *)NULL)
return (DIR *) NULL;
strcpy (nbuf, name);
last = &nbuf[len - 1];
/* Ok, DOS is very picky about its directory names. The following are
* valid.
*
* c:/
* c:.
* c:name/name1
*
* c:name/ is not valid
*/
if (((*last == '\\') || (*last == '/')) && (len > 1) &&
(!((len == 3) && (name[1] == ':'))))
*(last--) = 0;
/* Check its a directory */
DISABLE_HARD_ERRORS;
rc = stat (nbuf, &statb);
ENABLE_HARD_ERRORS;
if (rc)
{
free (nbuf);
return (DIR *) NULL;
}
if (!S_ISDIR (statb.st_mode))
{
free (nbuf);
errno = ENOTDIR;
return (DIR *)NULL;
}
if ((dirp = (DIR *) malloc (sizeof (DIR))) == (DIR *) NULL)
{
free (nbuf);
return (DIR *) NULL;
}
/* Set up to find everything */
if ((*last != '\\') && (*last != '/'))
strcat (last, "/");
strcat (last, "*.*");
/* Find the file system type */
HPFS = IsHPFSFileSystem (nbuf);
dirp->dd_loc = 0;
dirp->dd_cp = (DIRCONT *) NULL;
dirp->dd_contents = (DIRCONT *) NULL;
DISABLE_HARD_ERRORS;
d_handle = FindFirstFile (nbuf, &dtabuf);
rc = (d_handle == INVALID_HANDLE_VALUE) ? GetLastError () : 0;
ENABLE_HARD_ERRORS;
/* Check for errors */
if (rc)
{
free (nbuf);
/* Empty directory */
#if defined (ERROR_EMPTY_DIR)
if (rc == ERROR_EMPTY_DIR)
return dirp;
#endif
free (dirp);
return (DIR *) NULL;
}
/* Process the directory */
do
{
if (((dp = (DIRCONT *) malloc (sizeof (DIRCONT))) == (DIRCONT *)NULL) ||
((dp->_d_entry = strdup (dtabuf.FILE_NAME_E)) == (char *) NULL))
{
if (dp->_d_entry != (char *)NULL)
free ((char *)dp);
free (nbuf);
free_dircontents (dirp->dd_contents);
OS_CloseFH (d_handle);
return (DIR *) NULL;
}
if (!HPFS)
strlwr (dp->_d_entry);
if (dirp->dd_contents != (DIRCONT *) NULL)
dirp->dd_cp = dirp->dd_cp->_d_next = dp;
else
dirp->dd_contents = dirp->dd_cp = dp;
dp->_d_next = (DIRCONT *) NULL;
} while (FindNextFile (d_handle, &dtabuf));
dirp->dd_cp = dirp->dd_contents;
free (nbuf);
OS_CloseFH (d_handle);
return dirp;
}
/*
* Close the directory stream
*/
int
closedir (dirp)
DIR *dirp;
{
if (dirp != (DIR *)NULL)
{
free_dircontents (dirp->dd_contents);
free ((char *)dirp);
}
return 0;
}
/*
* Read the next record from the stream
*/
struct dirent *
readdir (dirp)
DIR *dirp;
{
static struct dirent dp;
if ((dirp == (DIR *)NULL) || (dirp->dd_cp == (DIRCONT *) NULL))
return (struct dirent *) NULL;
dp.d_reclen = strlen (strcpy (dp.d_name, dirp->dd_cp->_d_entry));
dp.d_off = dirp->dd_loc * 32;
dp.d_ino = (ino_t)++dirp->dd_loc;
dirp->dd_cp = dirp->dd_cp->_d_next;
return &dp;
}
/*
* Restart the directory stream
*/
void
rewinddir (dirp)
DIR *dirp;
{
seekdir (dirp, (off_t)0);
}
/*
* Move to a know position in the stream
*/
void
seekdir (dirp, off)
DIR *dirp;
off_t off;
{
long i = off;
DIRCONT *dp;
if ((dirp == (DIR *)NULL) || (off < 0L))
return;
for (dp = dirp->dd_contents; (--i >= 0) && (dp != (DIRCONT *)NULL);
dp = dp->_d_next)
;
dirp->dd_loc = off - (i + 1);
dirp->dd_cp = dp;
}
/*
* Get the current position
*/
off_t
telldir(dirp)
DIR *dirp;
{
return (dirp == (DIR *)NULL) ? (off_t) -1 : dirp->dd_loc;
}
/*
* Release the internal structure
*/
static void
free_dircontents (dp)
DIRCONT *dp;
{
DIRCONT *odp;
while ((odp = dp) != (DIRCONT *)NULL)
{
if (dp->_d_entry != (char *)NULL)
free (dp->_d_entry);
dp = dp->_d_next;
free ((char *)odp);
}
}
/*
* Windows NT version
*/
bool
IsHPFSFileSystem (directory)
char *directory;
{
char bName[4];
DWORD flags;
DWORD maxname;
BOOL rc;
unsigned int nDrive;
char szCurDir [MAX_PATH];
if (isalpha (directory[0]) && (directory[1] == ':'))
nDrive = toupper (directory[0]) - '@';
else
{
GetCurrentDirectory (MAX_PATH, szCurDir);
nDrive = szCurDir[0] - 'A' + 1;
}
/* Set up the drive name */
strcpy (bName, "x:\\");
bName[0] = (char) (nDrive + '@');
/* Read the volume info, if we fail - assume non-HPFS */
DISABLE_HARD_ERRORS;
rc = GetVolumeInformation (bName, (LPTSTR)NULL, 0, (LPDWORD)NULL,
&maxname, &flags, (LPTSTR)NULL, 0);
ENABLE_HARD_ERRORS;
return ((rc) && (flags & (FS_CASE_SENSITIVE | FS_CASE_IS_PRESERVED)))
? TRUE : FALSE;
}
|