Mercurial > dwindows
annotate win/dirent.c @ 1789:a640714f9087
Fix handling the DW_DT_WORDBREAK flag on OS/2 to cause static text to word wrap.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sat, 04 Aug 2012 01:14:17 +0000 |
parents | f8d1da63fb77 |
children | 22225eb286e5 |
rev | line source |
---|---|
1617
f8d1da63fb77
Add code to allow building DW.DLL as Unicode on Windows.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1601
diff
changeset
|
1 #undef UNICODE |
f8d1da63fb77
Add code to allow building DW.DLL as Unicode on Windows.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1601
diff
changeset
|
2 #undef _UNICODE |
f8d1da63fb77
Add code to allow building DW.DLL as Unicode on Windows.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1601
diff
changeset
|
3 |
3 | 4 #include <stdio.h> |
5 #include <stdlib.h> | |
6 #include <string.h> | |
7 #include <ctype.h> | |
8 | |
9 #include <windows.h> | |
10 | |
1594
6baf177f335c
Rename compat.c/h dwcompat.c/h and configure option to --with-dwcompat.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1362
diff
changeset
|
11 #include "dwcompat.h" |
3 | 12 #include <errno.h> |
1362
412af8059331
Attempt to get it building with Mingw again... builds but crashes.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
572
diff
changeset
|
13 #include <direct.h> |
3 | 14 |
15 #define error(rc) errno = 255 | |
16 | |
17 struct _dirdescr { | |
18 HANDLE handle; /* DosFindFirst handle */ | |
19 char fstype; /* filesystem type */ | |
20 long count; /* valid entries in <ffbuf> */ | |
21 long number; /* absolute number of next entry */ | |
22 int index; /* relative number of next entry */ | |
23 char name[MAXPATHLEN+3]; /* directory name */ | |
24 unsigned attrmask; /* attribute mask for seekdir */ | |
25 struct dirent entry; /* buffer for directory entry */ | |
26 WIN32_FIND_DATA data; | |
27 }; | |
28 | |
29 /* | |
30 * Return first char of filesystem type, or 0 if unknown. | |
31 */ | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
32 static char API getFSType(const char *path) |
3 | 33 { |
34 static char cache[1+26]; | |
35 char drive[3]; | |
1601
71e0a3ad07f7
Enable level 3 warnings in DEBUG mode with Visual C to show deprecation warnings.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1594
diff
changeset
|
36 UCHAR unit; |
3 | 37 char r; |
38 | |
39 if (isalpha(path[0]) && path[1] == ':') { | |
40 unit = toupper(path[0]) - '@'; | |
41 path += 2; | |
42 } else { | |
43 return 0; | |
44 } | |
45 | |
46 if ((path[0] == '\\' || path[0] == '/') | |
47 && (path[1] == '\\' || path[1] == '/')) | |
48 return 0; | |
49 | |
50 if (cache [unit]) | |
51 return cache [unit]; | |
52 | |
53 drive[0] = '@' + unit; | |
54 drive[1] = ':'; | |
55 drive[2] = '\0'; | |
56 | |
57 r = GetDriveType(drive); | |
58 | |
59 return cache [unit] = r; | |
60 } | |
61 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
62 char * API abs_path(const char *name, char *buffer, int len) |
3 | 63 { |
260
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
64 LPTSTR file; |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
65 |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
66 if(isalpha(name[0]) && name[1] == ':' && name[2] == '\0') |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
67 { |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
68 int drive = _getdrive(); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
69 char newdrive = toupper(name[0]); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
70 |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
71 _chdrive((newdrive - 'A')+1); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
72 |
1601
71e0a3ad07f7
Enable level 3 warnings in DEBUG mode with Visual C to show deprecation warnings.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1594
diff
changeset
|
73 if(_getcwd(buffer, len)) |
260
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
74 { |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
75 _chdrive(drive); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
76 return buffer; |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
77 } |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
78 _chdrive(drive); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
79 return NULL; |
3 | 80 } |
260
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
81 if(GetFullPathName(name, len, buffer, &file)) |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
82 return buffer; |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
83 return NULL; |
3 | 84 } |
85 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
86 DIR * API openxdir(const char *path, unsigned att_mask) |
3 | 87 { |
88 DIR *dir; | |
89 char name[MAXPATHLEN+3]; | |
90 | |
91 dir = malloc(sizeof(DIR)); | |
92 if (dir == NULL) { | |
93 errno = ENOMEM; | |
94 return NULL; | |
95 } | |
96 | |
97 strncpy(name, path, MAXPATHLEN); | |
98 name[MAXPATHLEN] = '\0'; | |
99 switch (name[strlen(name)-1]) { | |
100 default: | |
101 strcat(name, "\\"); | |
102 case '\\': | |
103 case '/': | |
104 case ':': | |
105 ; | |
106 } | |
107 strcat(name, "."); | |
108 if (!abs_path(name, dir->name, MAXPATHLEN+1)) | |
109 strcpy(dir->name, name); | |
110 if (dir->name[strlen(dir->name)-1] == '\\') | |
111 strcat(dir->name, "*"); | |
112 else | |
113 strcat(dir->name, "\\*"); | |
114 | |
115 dir->fstype = getFSType(dir->name); | |
116 dir->attrmask = att_mask | A_DIR; | |
117 | |
118 dir->count = 100; | |
184
4ec906d40ce2
FindFirstFile returns INVALID_HANDLE_VALUE on error, not NULL.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
3
diff
changeset
|
119 if((dir->handle = FindFirstFile(dir->name, &dir->data))==INVALID_HANDLE_VALUE) |
3 | 120 { |
121 free(dir); | |
122 error(rc); | |
123 return NULL; | |
124 } | |
125 | |
126 dir->number = 0; | |
127 dir->index = 0; | |
128 | |
129 return (DIR *)dir; | |
130 } | |
131 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
132 DIR * API opendir(const char *pathname) |
3 | 133 { |
134 return openxdir(pathname, 0); | |
135 } | |
136 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
137 struct dirent * API readdir(DIR *dir) |
3 | 138 { |
139 static int dummy_ino = 2; | |
140 | |
141 if (dir->number) | |
142 { | |
143 dir->count = 100; | |
144 if(!FindNextFile(dir->handle, &(dir->data))) | |
145 { | |
146 error(rc); | |
147 return NULL; | |
148 } | |
149 | |
150 dir->index = 0; | |
151 } | |
152 | |
153 strcpy(dir->entry.d_name, dir->data.cFileName); | |
154 dir->entry.d_ino = dummy_ino++; | |
1601
71e0a3ad07f7
Enable level 3 warnings in DEBUG mode with Visual C to show deprecation warnings.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1594
diff
changeset
|
155 dir->entry.d_reclen = (int)strlen(dir->data.cFileName); |
71e0a3ad07f7
Enable level 3 warnings in DEBUG mode with Visual C to show deprecation warnings.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1594
diff
changeset
|
156 dir->entry.d_namlen = (int)strlen(dir->data.cFileName); |
3 | 157 dir->entry.d_size = dir->data.nFileSizeLow; |
158 dir->entry.d_attribute = dir->data.dwFileAttributes; | |
159 #if 0 | |
160 dir->entry.d_time = *(USHORT *)&dir->next->ftimeLastWrite; | |
161 dir->entry.d_date = *(USHORT *)&dir->next->fdateLastWrite; | |
162 #endif | |
163 | |
164 dir->number++; | |
165 dir->index++; | |
166 return &dir->entry; | |
167 } | |
168 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
169 long API telldir(DIR *dir) |
3 | 170 { |
171 return dir->number; | |
172 } | |
173 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
174 void API seekdir(DIR *dir, long off) |
3 | 175 { |
176 if (dir->number > off) { | |
177 char name[MAXPATHLEN+2]; | |
178 | |
179 FindClose(dir->handle); | |
180 | |
181 strcpy(name, dir->name); | |
182 strcat(name, "*"); | |
183 | |
184 if((dir->handle = FindFirstFile(name, &(dir->data)))==NULL) | |
185 { | |
186 error(rc); | |
187 return; | |
188 } | |
189 | |
190 dir->number = 0; | |
191 dir->index = 0; | |
192 } | |
193 | |
194 while (dir->number < off && readdir(dir)) | |
195 ; | |
196 } | |
197 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
198 void API closedir(DIR *dir) |
3 | 199 { |
200 FindClose(dir->handle); | |
201 free(dir); | |
202 } | |
203 | |
204 /*****************************************************************************/ | |
205 | |
206 #ifdef TEST | |
207 | |
208 main(int argc, char **argv) | |
209 { | |
210 int i; | |
211 DIR *dir; | |
212 struct dirent *ep; | |
213 | |
214 for (i = 1; i < argc; ++i) { | |
215 dir = opendir(argv[i]); | |
216 if (!dir) | |
217 continue; | |
218 while (ep = readdir(dir)) | |
219 if (strchr("\\/:", argv[i] [strlen(argv[i]) - 1])) | |
220 printf("%s%s\n", argv[i], ep->d_name); | |
221 else | |
222 printf("%s/%s\n", argv[i], ep->d_name); | |
223 closedir(dir); | |
224 } | |
225 | |
226 return 0; | |
227 } | |
228 | |
229 #endif |