Mercurial > dwindows
comparison os2/dirent.c @ 3:67a643a734d9
Import
author | ktk@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 03 Jul 2001 07:50:39 +0000 |
parents | |
children | d9e87e8bcf1d |
comparison
equal
deleted
inserted
replaced
2:36c5f0ce3fbe | 3:67a643a734d9 |
---|---|
1 #ifdef OS2 | |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include <ctype.h> | |
7 | |
8 #include "dirent.h" | |
9 #include <errno.h> | |
10 | |
11 /*#ifndef __EMX__ | |
12 #include <libx.h> | |
13 #endif */ | |
14 | |
15 #define INCL_DOSFILEMGR | |
16 #define INCL_DOSERRORS | |
17 #include <os2.h> | |
18 | |
19 # define FFBUF FILEFINDBUF3 | |
20 # define Word ULONG | |
21 /* | |
22 * LS20 recommends a request count of 100, but according to the | |
23 * APAR text it does not lead to missing files, just to funny | |
24 * numbers of returned entries. | |
25 * | |
26 * LS30 HPFS386 requires a count greater than 2, or some files | |
27 * are missing (those starting with a character less that '.'). | |
28 * | |
29 * Novell looses entries which overflow the buffer. In previous | |
30 * versions of dirent2, this could have lead to missing files | |
31 * when the average length of 100 directory entries was 40 bytes | |
32 * or more (quite unlikely for files on a Novell server). | |
33 * | |
34 * Conclusion: Make sure that the entries all fit into the buffer | |
35 * and that the buffer is large enough for more than 2 entries | |
36 * (each entry is at most 300 bytes long). And ignore the LS20 | |
37 * effect. | |
38 */ | |
39 # define Count 25 | |
40 # define BufSz (25 * (sizeof(FILEFINDBUF3)+1)) | |
41 | |
42 #if defined(__IBMC__) || defined(__IBMCPP__) | |
43 #define error(rc) _doserrno = rc, errno = EOS2ERR | |
44 #else | |
45 #define error(rc) errno = 255 | |
46 #endif | |
47 | |
48 struct _dirdescr { | |
49 HDIR handle; /* DosFindFirst handle */ | |
50 char fstype; /* filesystem type */ | |
51 Word count; /* valid entries in <ffbuf> */ | |
52 long number; /* absolute number of next entry */ | |
53 int index; /* relative number of next entry */ | |
54 FFBUF * next; /* pointer to next entry */ | |
55 char name[MAXPATHLEN+3]; /* directory name */ | |
56 unsigned attrmask; /* attribute mask for seekdir */ | |
57 struct dirent entry; /* buffer for directory entry */ | |
58 BYTE ffbuf[BufSz]; | |
59 }; | |
60 | |
61 /* | |
62 * Return first char of filesystem type, or 0 if unknown. | |
63 */ | |
64 static char | |
65 getFSType(const char *path) | |
66 { | |
67 static char cache[1+26]; | |
68 char drive[3], info[512]; | |
69 Word unit, infolen; | |
70 char r; | |
71 | |
72 if (isalpha(path[0]) && path[1] == ':') { | |
73 unit = toupper(path[0]) - '@'; | |
74 path += 2; | |
75 } else { | |
76 ULONG driveMap; | |
77 if (DosQueryCurrentDisk(&unit, &driveMap)) | |
78 return 0; | |
79 } | |
80 | |
81 if ((path[0] == '\\' || path[0] == '/') | |
82 && (path[1] == '\\' || path[1] == '/')) | |
83 return 0; | |
84 | |
85 if (cache [unit]) | |
86 return cache [unit]; | |
87 | |
88 drive[0] = '@' + unit; | |
89 drive[1] = ':'; | |
90 drive[2] = '\0'; | |
91 infolen = sizeof info; | |
92 if (DosQueryFSAttach(drive, 0, FSAIL_QUERYNAME, (PVOID)info, &infolen)) | |
93 return 0; | |
94 if (infolen >= sizeof(FSQBUFFER2)) { | |
95 FSQBUFFER2 *p = (FSQBUFFER2 *)info; | |
96 r = p->szFSDName[p->cbName]; | |
97 } else | |
98 r = 0; | |
99 return cache [unit] = r; | |
100 } | |
101 | |
102 char * | |
103 abs_path(const char *name, char *buffer, int len) | |
104 { | |
105 char buf[4]; | |
106 if (isalpha(name[0]) && name[1] == ':' && name[2] == '\0') { | |
107 buf[0] = name[0]; | |
108 buf[1] = name[1]; | |
109 buf[2] = '.'; | |
110 buf[3] = '\0'; | |
111 name = buf; | |
112 } | |
113 if (DosQueryPathInfo((PSZ)name, FIL_QUERYFULLNAME, buffer, len)) | |
114 return NULL; | |
115 return buffer; | |
116 } | |
117 | |
118 DIR * | |
119 openxdir(const char *path, unsigned att_mask) | |
120 { | |
121 DIR *dir; | |
122 char name[MAXPATHLEN+3]; | |
123 Word rc; | |
124 | |
125 dir = malloc(sizeof(DIR)); | |
126 if (dir == NULL) { | |
127 errno = ENOMEM; | |
128 return NULL; | |
129 } | |
130 | |
131 strncpy(name, path, MAXPATHLEN); | |
132 name[MAXPATHLEN] = '\0'; | |
133 switch (name[strlen(name)-1]) { | |
134 default: | |
135 strcat(name, "\\"); | |
136 case '\\': | |
137 case '/': | |
138 case ':': | |
139 ; | |
140 } | |
141 strcat(name, "."); | |
142 if (!abs_path(name, dir->name, MAXPATHLEN+1)) | |
143 strcpy(dir->name, name); | |
144 if (dir->name[strlen(dir->name)-1] == '\\') | |
145 strcat(dir->name, "*"); | |
146 else | |
147 strcat(dir->name, "\\*"); | |
148 | |
149 dir->fstype = getFSType(dir->name); | |
150 dir->attrmask = att_mask | A_DIR; | |
151 | |
152 dir->handle = HDIR_CREATE; | |
153 dir->count = 100; | |
154 rc = DosFindFirst(dir->name, &dir->handle, dir->attrmask, | |
155 dir->ffbuf, sizeof dir->ffbuf, &dir->count, FIL_STANDARD); | |
156 switch (rc) { | |
157 default: | |
158 free(dir); | |
159 error(rc); | |
160 return NULL; | |
161 case NO_ERROR: | |
162 case ERROR_NO_MORE_FILES: | |
163 ; | |
164 } | |
165 | |
166 dir->number = 0; | |
167 dir->index = 0; | |
168 dir->next = (FFBUF *)dir->ffbuf; | |
169 | |
170 return (DIR *)dir; | |
171 } | |
172 | |
173 DIR * | |
174 opendir(const char *pathname) | |
175 { | |
176 return openxdir(pathname, 0); | |
177 } | |
178 | |
179 struct dirent * | |
180 readdir(DIR *dir) | |
181 { | |
182 static int dummy_ino = 2; | |
183 | |
184 if (dir->index == dir->count) { | |
185 Word rc; | |
186 dir->count = 100; | |
187 rc = DosFindNext(dir->handle, dir->ffbuf, | |
188 sizeof dir->ffbuf, &dir->count); | |
189 if (rc) { | |
190 error(rc); | |
191 return NULL; | |
192 } | |
193 | |
194 dir->index = 0; | |
195 dir->next = (FFBUF *)dir->ffbuf; | |
196 } | |
197 | |
198 if (dir->index == dir->count) | |
199 return NULL; | |
200 | |
201 memcpy(dir->entry.d_name, dir->next->achName, dir->next->cchName); | |
202 dir->entry.d_name[dir->next->cchName] = '\0'; | |
203 dir->entry.d_ino = dummy_ino++; | |
204 dir->entry.d_reclen = dir->next->cchName; | |
205 dir->entry.d_namlen = dir->next->cchName; | |
206 dir->entry.d_size = dir->next->cbFile; | |
207 dir->entry.d_attribute = dir->next->attrFile; | |
208 dir->entry.d_time = *(USHORT *)&dir->next->ftimeLastWrite; | |
209 dir->entry.d_date = *(USHORT *)&dir->next->fdateLastWrite; | |
210 | |
211 switch (dir->fstype) { | |
212 case 'F': /* FAT */ | |
213 case 'C': /* CDFS */ | |
214 if (dir->next->attrFile & FILE_DIRECTORY) | |
215 strupr(dir->entry.d_name); | |
216 else | |
217 strlwr(dir->entry.d_name); | |
218 } | |
219 | |
220 dir->next = (FFBUF *)((BYTE *)dir->next + dir->next->oNextEntryOffset); | |
221 ++dir->number; | |
222 ++dir->index; | |
223 | |
224 return &dir->entry; | |
225 } | |
226 | |
227 long | |
228 telldir(DIR *dir) | |
229 { | |
230 return dir->number; | |
231 } | |
232 | |
233 void | |
234 seekdir(DIR *dir, long off) | |
235 { | |
236 if (dir->number > off) { | |
237 char name[MAXPATHLEN+2]; | |
238 Word rc; | |
239 | |
240 DosFindClose(dir->handle); | |
241 | |
242 strcpy(name, dir->name); | |
243 strcat(name, "*"); | |
244 | |
245 dir->handle = HDIR_CREATE; | |
246 dir->count = 32767; | |
247 rc = DosFindFirst(name, &dir->handle, dir->attrmask, | |
248 dir->ffbuf, sizeof dir->ffbuf, &dir->count, FIL_STANDARD); | |
249 switch (rc) { | |
250 default: | |
251 error(rc); | |
252 return; | |
253 case NO_ERROR: | |
254 case ERROR_NO_MORE_FILES: | |
255 ; | |
256 } | |
257 | |
258 dir->number = 0; | |
259 dir->index = 0; | |
260 dir->next = (FFBUF *)dir->ffbuf; | |
261 } | |
262 | |
263 while (dir->number < off && readdir(dir)) | |
264 ; | |
265 } | |
266 | |
267 void | |
268 closedir(DIR *dir) | |
269 { | |
270 DosFindClose(dir->handle); | |
271 free(dir); | |
272 } | |
273 | |
274 /*****************************************************************************/ | |
275 | |
276 #ifdef TEST | |
277 | |
278 main(int argc, char **argv) | |
279 { | |
280 int i; | |
281 DIR *dir; | |
282 struct dirent *ep; | |
283 | |
284 for (i = 1; i < argc; ++i) { | |
285 dir = opendir(argv[i]); | |
286 if (!dir) | |
287 continue; | |
288 while (ep = readdir(dir)) | |
289 if (strchr("\\/:", argv[i] [strlen(argv[i]) - 1])) | |
290 printf("%s%s\n", argv[i], ep->d_name); | |
291 else | |
292 printf("%s/%s\n", argv[i], ep->d_name); | |
293 closedir(dir); | |
294 } | |
295 | |
296 return 0; | |
297 } | |
298 | |
299 #endif | |
300 | |
301 #endif /* OS2 */ | |
302 |