forked from Leffmann/vlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.c
306 lines (243 loc) · 5.46 KB
/
dir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* $VER: vlink dir.c V0.14a (07.08.11)
*
* This file is part of vlink, a portable linker for multiple
* object formats.
* Copyright (c) 1997-2011 Frank Wille
*/
#define DIR_C
#include "vlink.h"
#ifdef AMIGAOS
#pragma amiga-align
#include <dos/dosextens.h>
#include <dos/dostags.h>
#include <proto/dos.h>
#pragma default-align
struct Dir {
struct FileInfoBlock fib;
BPTR lock;
char name[FNAMEBUFSIZE];
};
#elif defined(ATARI)
#include <tos.h>
#define MAX_PATH_LEN 127
struct Dir {
DTA dta;
DTA *olddta;
int err;
char name[MAX_PATH_LEN+1];
};
#elif defined(_WIN32)
#include <windows.h>
struct Dir {
WIN32_FIND_DATA fnd;
HANDLE hndl;
char name[MAX_PATH];
};
#else /* UNIX */
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#endif
char *path_append(char *buf,const char *path,const char *add,size_t bufsize)
/* append a file- or path-name to an existing path and convert */
{
size_t len = strlen(path);
#if defined(AMIGAOS) || defined(_WIN32)
char *p;
#endif
if ((len+strlen(add)+1) < bufsize) {
if (buf != path)
strcpy(buf,path);
#if defined(AMIGAOS)
if (len>0 && buf[len-1]!='/' && buf[len-1]!=':')
buf[len++] = '/';
strcpy(buf+len,add);
path = p = buf;
while (*path) {
if (*path == '.') {
if (*(path+1) == '\0') {
path++;
continue;
}
else if (*(path+1)=='/') {
path += 2;
continue;
}
else if (*(path+1)=='.' && *(path+2)=='/')
path += 2;
}
*p++ = *path++;
}
*p = '\0';
#elif defined(_WIN32)
if (len>0 && buf[len-1]!='\\' && buf[len-1]!='/' && buf[len-1]!=':')
buf[len++] = '\\';
strcpy(buf+len,add);
for (p=buf; *p; *p++) {
if (*p == '/')
*p = '\\';
}
#else
if (len>0 && buf[len-1]!='/')
buf[len++] = '/';
strcpy(buf+len,add);
#endif
return buf;
}
return NULL;
}
#ifdef AMIGAOS
char *open_dir(const char *dirname)
/* open a directory for examination */
{
struct Dir *d;
if (d = malloc(sizeof(struct Dir))) {
strcpy(d->name,dirname);
if (!strcmp(d->name,".")) { /* current directory? */
d->name[0] = 0;
}
if (d->lock = Lock(d->name,ACCESS_READ)) {
if (Examine(d->lock,&(d->fib))) {
return (char *)d;
}
UnLock(d->lock);
}
free(d);
}
return NULL;
}
char *read_dir(char *d)
/* get next file name from opened directory, NULL if no more entries */
{
if (ExNext(((struct Dir *)d)->lock,&(((struct Dir *)d)->fib)))
return ((struct Dir *)d)->fib.fib_FileName;
if (IoErr() != ERROR_NO_MORE_ENTRIES)
error(10,((struct Dir *)d)->name);
return NULL;
}
void close_dir(char *d)
/* finish directory access */
{
if (d) {
UnLock(((struct Dir *)d)->lock);
free(d);
}
}
void set_exec(const char *path)
{
SetProtection(path,0); /* "rwed" */
}
#elif defined(ATARI)
char *open_dir(const char *dirname)
/* open a directory for examination */
{
struct Dir *d;
if (d = malloc(sizeof(struct Dir))) {
strncpy(d->name,dirname,MAX_PATH_LEN);
d->olddta = Fgetdta();
Fsetdta(&d->dta);
d->err = Fsfirst(d->name,FA_READONLY|FA_ARCHIVE);
if (d->err != E_OK) {
Fsetdta(d->olddta);
free(d);
d = NULL;
}
}
return (char *)d;
}
char *read_dir(char *p)
/* get next file name from opened directory, NULL if no more entries */
{
struct Dir *d = (struct Dir *)p;
if (d->err == E_OK) {
strncpy(d->name,d->dta.d_fname,MAX_PATH_LEN);
d->err = Fsnext();
return d->name;
}
return NULL;
}
void close_dir(char *d)
/* finish directory access */
{
if (d) {
Fsetdta(((struct Dir *)d)->olddta);
free(d);
}
}
void set_exec(const char *path)
{
/* Fattrib() - no flag for executables exists? */
}
#elif defined(_WIN32)
char *open_dir(const char *dirname)
/* open a directory for examination */
{
struct Dir *d;
if (d = (struct Dir *)malloc(sizeof(struct Dir))) {
wsprintf(d->name, "%s\\*", dirname);
d->hndl = NULL;
}
return (char *)d;
}
char *read_dir(char *d)
/* get next file name from opened directory, NULL if no more entries */
{
do {
if (!((struct Dir *)d)->hndl) {
((struct Dir *)d)->hndl = FindFirstFile(((struct Dir *)d)->name,
&((struct Dir *)d)->fnd);
if (((struct Dir *)d)->hndl == INVALID_HANDLE_VALUE) {
((struct Dir *)d)->hndl = NULL;
return NULL;
}
}
else {
if (!FindNextFile(((struct Dir *)d)->hndl, &((struct Dir *)d)->fnd))
return NULL;
}
}
while (((struct Dir *)d)->fnd.dwFileAttributes
& (FILE_ATTRIBUTE_DIRECTORY |
FILE_ATTRIBUTE_HIDDEN |
FILE_ATTRIBUTE_OFFLINE));
return (char *)((struct Dir *)d)->fnd.cFileName;
}
void close_dir(char *d)
/* finish directory access */
{
if (d) {
if (((struct Dir *)d)->hndl)
FindClose(((struct Dir *)d)->hndl);
free(d);
}
}
/* @@@ FIXME! */
void set_exec(const char *path)
{
chmod(path,0755); /* rwxr-xr-x */
}
#else /* UNIX */
char *open_dir(const char *dirname)
/* open a directory for examination */
{
return (char *)opendir(dirname);
}
char *read_dir(char *d)
/* get next file name from opened directory, NULL if no more entries */
{
struct dirent *dp;
if (dp = readdir((DIR *)d))
return dp->d_name;
return NULL;
}
void close_dir(char *d)
/* finish directory access */
{
if (d)
closedir((DIR *)d);
}
void set_exec(const char *path)
{
chmod(path,0755); /* rwxr-xr-x */
}
#endif