Mercurial > dwindows
annotate os2/dirent.c @ 100:bb2b01c3122f
Switch back to long from long long on Windows.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 06 Aug 2002 05:31:04 +0000 |
parents | d9e87e8bcf1d |
children | 75bf3051235f |
rev | line source |
---|---|
3 | 1 #ifdef OS2 |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include <ctype.h> | |
7 | |
24
d9e87e8bcf1d
Updated dynamic windows to build with EMX.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
3
diff
changeset
|
8 #include "platform/dirent.h" |
3 | 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 | |
24
d9e87e8bcf1d
Updated dynamic windows to build with EMX.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
3
diff
changeset
|
125 if(!path) |
d9e87e8bcf1d
Updated dynamic windows to build with EMX.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
3
diff
changeset
|
126 return NULL; |
d9e87e8bcf1d
Updated dynamic windows to build with EMX.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
3
diff
changeset
|
127 |
3 | 128 dir = malloc(sizeof(DIR)); |
129 if (dir == NULL) { | |
130 errno = ENOMEM; | |
131 return NULL; | |
132 } | |
133 | |
134 strncpy(name, path, MAXPATHLEN); | |
135 name[MAXPATHLEN] = '\0'; | |
136 switch (name[strlen(name)-1]) { | |
137 default: | |
138 strcat(name, "\\"); | |
139 case '\\': | |
140 case '/': | |
141 case ':': | |
142 ; | |
143 } | |
144 strcat(name, "."); | |
145 if (!abs_path(name, dir->name, MAXPATHLEN+1)) | |
146 strcpy(dir->name, name); | |
147 if (dir->name[strlen(dir->name)-1] == '\\') | |
148 strcat(dir->name, "*"); | |
149 else | |
150 strcat(dir->name, "\\*"); | |
151 | |
152 dir->fstype = getFSType(dir->name); | |
153 dir->attrmask = att_mask | A_DIR; | |
154 | |
155 dir->handle = HDIR_CREATE; | |
156 dir->count = 100; | |
157 rc = DosFindFirst(dir->name, &dir->handle, dir->attrmask, | |
158 dir->ffbuf, sizeof dir->ffbuf, &dir->count, FIL_STANDARD); | |
159 switch (rc) { | |
160 default: | |
161 free(dir); | |
162 error(rc); | |
163 return NULL; | |
164 case NO_ERROR: | |
165 case ERROR_NO_MORE_FILES: | |
166 ; | |
167 } | |
168 | |
169 dir->number = 0; | |
170 dir->index = 0; | |
171 dir->next = (FFBUF *)dir->ffbuf; | |
172 | |
173 return (DIR *)dir; | |
174 } | |
175 | |
176 DIR * | |
177 opendir(const char *pathname) | |
178 { | |
179 return openxdir(pathname, 0); | |
180 } | |
181 | |
182 struct dirent * | |
183 readdir(DIR *dir) | |
184 { | |
185 static int dummy_ino = 2; | |
186 | |
187 if (dir->index == dir->count) { | |
188 Word rc; | |
189 dir->count = 100; | |
190 rc = DosFindNext(dir->handle, dir->ffbuf, | |
191 sizeof dir->ffbuf, &dir->count); | |
192 if (rc) { | |
193 error(rc); | |
194 return NULL; | |
195 } | |
196 | |
197 dir->index = 0; | |
198 dir->next = (FFBUF *)dir->ffbuf; | |
199 } | |
200 | |
201 if (dir->index == dir->count) | |
202 return NULL; | |
203 | |
204 memcpy(dir->entry.d_name, dir->next->achName, dir->next->cchName); | |
205 dir->entry.d_name[dir->next->cchName] = '\0'; | |
206 dir->entry.d_ino = dummy_ino++; | |
207 dir->entry.d_reclen = dir->next->cchName; | |
208 dir->entry.d_namlen = dir->next->cchName; | |
209 dir->entry.d_size = dir->next->cbFile; | |
210 dir->entry.d_attribute = dir->next->attrFile; | |
211 dir->entry.d_time = *(USHORT *)&dir->next->ftimeLastWrite; | |
212 dir->entry.d_date = *(USHORT *)&dir->next->fdateLastWrite; | |
213 | |
214 switch (dir->fstype) { | |
215 case 'F': /* FAT */ | |
216 case 'C': /* CDFS */ | |
217 if (dir->next->attrFile & FILE_DIRECTORY) | |
218 strupr(dir->entry.d_name); | |
219 else | |
220 strlwr(dir->entry.d_name); | |
221 } | |
222 | |
223 dir->next = (FFBUF *)((BYTE *)dir->next + dir->next->oNextEntryOffset); | |
224 ++dir->number; | |
225 ++dir->index; | |
226 | |
227 return &dir->entry; | |
228 } | |
229 | |
230 long | |
231 telldir(DIR *dir) | |
232 { | |
233 return dir->number; | |
234 } | |
235 | |
236 void | |
237 seekdir(DIR *dir, long off) | |
238 { | |
239 if (dir->number > off) { | |
240 char name[MAXPATHLEN+2]; | |
241 Word rc; | |
242 | |
243 DosFindClose(dir->handle); | |
244 | |
245 strcpy(name, dir->name); | |
246 strcat(name, "*"); | |
247 | |
248 dir->handle = HDIR_CREATE; | |
249 dir->count = 32767; | |
250 rc = DosFindFirst(name, &dir->handle, dir->attrmask, | |
251 dir->ffbuf, sizeof dir->ffbuf, &dir->count, FIL_STANDARD); | |
252 switch (rc) { | |
253 default: | |
254 error(rc); | |
255 return; | |
256 case NO_ERROR: | |
257 case ERROR_NO_MORE_FILES: | |
258 ; | |
259 } | |
260 | |
261 dir->number = 0; | |
262 dir->index = 0; | |
263 dir->next = (FFBUF *)dir->ffbuf; | |
264 } | |
265 | |
266 while (dir->number < off && readdir(dir)) | |
267 ; | |
268 } | |
269 | |
270 void | |
271 closedir(DIR *dir) | |
272 { | |
273 DosFindClose(dir->handle); | |
274 free(dir); | |
275 } | |
276 | |
277 /*****************************************************************************/ | |
278 | |
279 #ifdef TEST | |
280 | |
281 main(int argc, char **argv) | |
282 { | |
283 int i; | |
284 DIR *dir; | |
285 struct dirent *ep; | |
286 | |
287 for (i = 1; i < argc; ++i) { | |
288 dir = opendir(argv[i]); | |
289 if (!dir) | |
290 continue; | |
291 while (ep = readdir(dir)) | |
292 if (strchr("\\/:", argv[i] [strlen(argv[i]) - 1])) | |
293 printf("%s%s\n", argv[i], ep->d_name); | |
294 else | |
295 printf("%s/%s\n", argv[i], ep->d_name); | |
296 closedir(dir); | |
297 } | |
298 | |
299 return 0; | |
300 } | |
301 | |
302 #endif | |
303 | |
304 #endif /* OS2 */ | |
305 |