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
|
/*
* Author: Bob Withers
* Copyright (c) 1993, All Rights Reserved
*
* NOTICE
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted
* provided that the above copyright notice appears in all copies and
* that both the copyright notice and this permission notice appear in
* supporting documentation.
*
* The author makes no representations about the suitability of this
* software for any purpose. This software is provided ``as is''
* without express or implied warranty.
*/
#include <stdlib.h>
#include <string.h>
#define INCL_DOSFILEMGR
#define INCL_DOSERRORS
#include <os2.h>
#include "dirent.h"
#define DIRENT_INCR 25
DIR *opendir(char *filename)
{
auto size_t len;
auto DIR *dirp;
auto char *p;
auto HDIR hdir;
#ifdef OS2_16
auto USHORT rc; /* for 16 bit OS/2 */
auto FILEFINDBUF ff;
auto USHORT cnt;
#else
auto APIRET rc; /* for 32 bit OS/2 */
auto FILEFINDBUF3 ff;
auto ULONG cnt;
#endif /* OS2_16 */
if (NULL == filename || '\0' == filename[0])
filename = ".";
dirp = malloc(sizeof(*dirp));
if (NULL == dirp)
return(NULL);
len = strlen(filename);
dirp->dirname = malloc(len + 5);
if (NULL == dirp->dirname)
{
free(dirp);
return(NULL);
}
dirp->max_ent = 0;
dirp->tot_ent = 0;
dirp->cur_ent = 0;
dirp->entp = NULL;
strcpy(dirp->dirname, filename);
for (p = dirp->dirname; *p; ++p)
{
if ('/' == *p)
*p = '\\';
}
if ('\\' != dirp->dirname[len - 1])
strcat(dirp->dirname, "\\");
strcat(dirp->dirname, "*.*");
hdir = HDIR_SYSTEM;
cnt = 1;
rc = DosFindFirst(dirp->dirname, &hdir,
FILE_NORMAL | FILE_READONLY | FILE_HIDDEN |
FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED,
&ff, sizeof(ff), &cnt, FIL_STANDARD);
while (NO_ERROR == rc)
{
auto struct dirent *entp;
if (dirp->tot_ent >= dirp->max_ent)
{
auto struct dirent **p;
dirp->max_ent += DIRENT_INCR;
p = realloc(dirp->entp, dirp->max_ent * sizeof(entp));
if (NULL == p)
{
rc = ERROR_NOT_ENOUGH_MEMORY;
break;
}
dirp->entp = p;
}
entp = malloc(sizeof(*entp) + (size_t) ff.cchName);
if (NULL == entp)
{
rc = ERROR_NOT_ENOUGH_MEMORY;
break;
}
entp->d_ino = 0;
entp->d_off = dirp->tot_ent;
entp->d_namlen = (unsigned short) ff.cchName;
memcpy(entp->d_name, ff.achName, entp->d_namlen);
entp->d_name[entp->d_namlen] = '\0';
dirp->entp[dirp->tot_ent++] = entp;
cnt = 1;
rc = DosFindNext(hdir, &ff, sizeof(ff), &cnt);
}
DosFindClose(hdir);
if (ERROR_NO_MORE_FILES == rc)
return(dirp);
closedir(dirp);
return(NULL);
}
struct dirent *readdir(DIR *dirp)
{
if (dirp->cur_ent < 0 || dirp->cur_ent >= dirp->tot_ent)
return(NULL);
return(dirp->entp[dirp->cur_ent++]);
}
long telldir(DIR *dirp)
{
return((long) dirp->cur_ent);
}
void seekdir(DIR *dirp, long loc)
{
dirp->cur_ent = (int) loc;
return;
}
void rewinddir(DIR *dirp)
{
dirp->cur_ent = 0;
return;
}
void closedir(DIR *dirp)
{
if (dirp)
{
if (dirp->dirname)
free(dirp->dirname);
if (dirp->entp)
{
register int i;
for (i = 0; i < dirp->tot_ent; ++i)
free(dirp->entp[i]);
free(dirp->entp);
}
}
return;
}
|