Debian Codegraph @

lib/dirent.c

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