aboutsummaryrefslogtreecommitdiff
path: root/contrib/Win32/dirent.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/Win32/dirent.c')
-rw-r--r--contrib/Win32/dirent.c277
1 files changed, 277 insertions, 0 deletions
diff --git a/contrib/Win32/dirent.c b/contrib/Win32/dirent.c
new file mode 100644
index 00000000..5f31686e
--- /dev/null
+++ b/contrib/Win32/dirent.c
@@ -0,0 +1,277 @@
1/*
2 dir.c for MS-DOS by Samuel Lam <skl@van-bc.UUCP>, June/87
3 */
4
5/* #ifdef WIN32 */
6/*
7 * @(#)dir.c 1.4 87/11/06 Public Domain.
8 *
9 * A public domain implementation of BSD directory routines for
10 * MS-DOS. Written by Michael Rendell ({uunet,utai}michael@garfield),
11 * August 1897
12 * Ported to OS/2 by Kai Uwe Rommel
13 * December 1989, February 1990
14 * Ported to Windows NT 22 May 91
15 * other mods Summer '92 brianmo@microsoft.com
16 * opendirx() was horribly written, very inefficient, and did not take care
17 * of all cases. It is still not too clean, but it is far more efficient.
18 * Changes made by Gordon Chaffee (chaffee@bugs-bunny.cs.berkeley.edu)
19 */
20
21
22/*Includes:
23 * crt
24 */
25#include <windows.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys\types.h>
29#include <sys\stat.h>
30#include "dirent.h"
31
32#define stat _stat
33
34/*
35 * NT specific
36 */
37#include <stdio.h>
38
39/*
40 * random typedefs
41 */
42#define HDIR HANDLE
43#define HFILE HANDLE
44#define PHFILE PHANDLE
45
46/*
47 * local functions
48 */
49static char *getdirent(char *);
50static void free_dircontents(struct _dircontents *);
51
52static HDIR FindHandle;
53static WIN32_FIND_DATA FileFindData;
54
55static struct dirent dp;
56
57DIR *opendirx(char *name, char *pattern)
58{
59 struct stat statb;
60 DIR *dirp;
61 char c;
62 char *s;
63 struct _dircontents *dp;
64 int len;
65 int unc;
66 char path[OFS_MAXPATHNAME];
67 register char *ip, *op;
68
69 for (ip = name, op = path;; op++, ip++) {
70 *op = *ip;
71 if (*ip == '\0') {
72 break;
73 }
74 }
75 len = ip - name;
76 if (len > 0) {
77 unc = ((path[0] == '\\' || path[0] == '/') &&
78 (path[1] == '\\' || path[1] == '/'));
79 c = path[len - 1];
80 if (unc) {
81 if (c != '\\' && c != '/') {
82 path[len] = '/';
83 len++;
84 path[len] = '\0';
85 }
86 } else {
87 if ((c == '\\' || c == '/') && (len > 1)) {
88 len--;
89 path[len] = '\0';
90
91 if (path[len - 1] == ':') {
92 path[len] = '/';
93 len++;
94 path[len] = '.';
95 len++;
96 path[len] = '\0';
97 }
98 } else if (c == ':') {
99 path[len] = '.';
100 len++;
101 path[len] = '\0';
102 }
103 }
104 } else {
105 unc = 0;
106 path[0] = '.';
107 path[1] = '\0';
108 len = 1;
109 }
110
111 if (stat(path, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR) {
112 return NULL;
113 }
114 dirp = malloc(sizeof(DIR));
115 if (dirp == NULL) {
116 return dirp;
117 }
118 c = path[len - 1];
119 if (c == '.') {
120 if (len == 1) {
121 len--;
122 } else {
123 c = path[len - 2];
124 if (c == '\\' || c == ':') {
125 len--;
126 } else {
127 path[len] = '/';
128 len++;
129 }
130 }
131 } else if (!unc && ((len != 1) || (c != '\\' && c != '/'))) {
132 path[len] = '/';
133 len++;
134 }
135 strcpy(path + len, pattern);
136
137 dirp->dd_loc = 0;
138 dirp->dd_contents = dirp->dd_cp = NULL;
139
140 if ((s = getdirent(path)) == NULL) {
141 return dirp;
142 }
143 do {
144 if (((dp = malloc(sizeof(struct _dircontents))) == NULL) ||
145 ((dp->_d_entry = malloc(strlen(s) + 1)) == NULL)) {
146 if (dp)
147 free(dp);
148 free_dircontents(dirp->dd_contents);
149
150 return NULL;
151 }
152 if (dirp->dd_contents)
153 dirp->dd_cp = dirp->dd_cp->_d_next = dp;
154 else
155 dirp->dd_contents = dirp->dd_cp = dp;
156
157 strcpy(dp->_d_entry, s);
158 dp->_d_next = NULL;
159
160 }
161 while ((s = getdirent(NULL)) != NULL);
162
163 dirp->dd_cp = dirp->dd_contents;
164 return dirp;
165}
166
167DIR *opendir(char *name)
168{
169 return opendirx(name, "*");
170}
171
172void closedir(DIR * dirp)
173{
174 free_dircontents(dirp->dd_contents);
175 free(dirp);
176}
177
178struct dirent *readdir(DIR * dirp)
179{
180 /* static struct dirent dp; */
181 if (dirp->dd_cp == NULL)
182 return NULL;
183
184 /*strcpy(dp.d_name,dirp->dd_cp->_d_entry); */
185
186 dp.d_name = dirp->dd_cp->_d_entry;
187
188 dp.d_namlen = dp.d_reclen =
189 strlen(dp.d_name);
190
191 dp.d_ino = dirp->dd_loc + 1; /* fake the inode */
192
193 dirp->dd_cp = dirp->dd_cp->_d_next;
194 dirp->dd_loc++;
195
196
197 return &dp;
198}
199
200void seekdir(DIR * dirp, long off)
201{
202 long i = off;
203 struct _dircontents *dp;
204
205 if (off >= 0) {
206 for (dp = dirp->dd_contents; --i >= 0 && dp; dp = dp->_d_next);
207
208 dirp->dd_loc = off - (i + 1);
209 dirp->dd_cp = dp;
210 }
211}
212
213
214long telldir(DIR * dirp)
215{
216 return dirp->dd_loc;
217}
218
219static void free_dircontents(struct _dircontents *dp)
220{
221 struct _dircontents *odp;
222
223 while (dp) {
224 if (dp->_d_entry)
225 free(dp->_d_entry);
226
227 dp = (odp = dp)->_d_next;
228 free(odp);
229 }
230}
231/* end of "free_dircontents" */
232
233static char *getdirent(char *dir)
234{
235 int got_dirent;
236
237 if (dir != NULL) { /* get first entry */
238 if ((FindHandle = FindFirstFile(dir, &FileFindData))
239 == (HDIR) 0xffffffff) {
240 return NULL;
241 }
242 got_dirent = 1;
243 } else /* get next entry */
244 got_dirent = FindNextFile(FindHandle, &FileFindData);
245
246 if (got_dirent)
247 return FileFindData.cFileName;
248 else {
249 FindClose(FindHandle);
250 return NULL;
251 }
252}
253/* end of getdirent() */
254
255struct passwd *_cdecl
256 getpwnam(char *name)
257{
258 return NULL;
259}
260
261struct passwd *_cdecl
262 getpwuid(int uid)
263{
264 return NULL;
265}
266
267int getuid()
268{
269 return 0;
270}
271
272void _cdecl
273 endpwent(void)
274{
275}
276
277/* #endif */