-
Notifications
You must be signed in to change notification settings - Fork 5
/
dir.c
219 lines (195 loc) · 5.7 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
/* This is part of pure_libc (a project related to ViewOS and Virtual Square)
*
* dir.c: Directory management
*
* Copyright 2006-2017 Renzo Davoli University of Bologna - Italy
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* The GNU C Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the GNU C Library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <config.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define PURE_DIRSTREAM_SIZE 2048
#define PURE_DIRBUF_SIZE (PURE_DIRSTREAM_SIZE - 3*sizeof(int))
struct __dirstream {
int fd;
int bufsize;
int bufpos;
char buf[PURE_DIRBUF_SIZE];
struct dirent de32;
};
DIR *fdopendir(int fd)
{
DIR *newdir = (DIR *) malloc (sizeof (struct __dirstream));
if (!newdir)
return NULL;
else {
newdir->fd=fd;
newdir->bufsize=newdir->bufpos=0;
}
return newdir;
}
DIR *opendirat(int dirfd, const char *name)
{
int fd;
DIR *newdir=NULL;
if (dirfd == AT_FDCWD)
fd = open(name, O_RDONLY | O_DIRECTORY);
else
fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY);
if (fd >= 0) {
if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
close(fd);
else {
newdir = fdopendir(fd);
if (!newdir) {
close(fd);
return NULL;
}
}
}
return newdir;
}
DIR *opendir(const char *name) {
return opendirat(AT_FDCWD, name);
}
int closedir(DIR *dir){
int fd=dir->fd;
free(dir);
return close(fd);
}
#define _MAX_OFF_T ((__off_t) -1)
ssize_t getdents64(int fd, void *buffer, size_t length);
#ifndef __USE_FILE_OFFSET64
struct dirent *readdir(DIR *dir){
register struct dirent64 *de64=readdir64(dir);
if(de64 == NULL)
return NULL;
else {
dir->de32.d_ino=de64->d_ino;
dir->de32.d_off=(de64->d_off > _MAX_OFF_T)?_MAX_OFF_T:de64->d_off;
dir->de32.d_reclen=de64->d_reclen;
dir->de32.d_type=de64->d_type;
strcpy(dir->de32.d_name,de64->d_name);
return &(dir->de32);
}
}
#endif
struct dirent64 *readdir64(DIR *dir){
register struct dirent64 *this;
this=((struct dirent64 *) (dir->buf + dir->bufpos));
if (dir->bufsize == 0 || (dir->bufpos += this->d_reclen) >= dir->bufsize) {
dir->bufsize = getdents64(dir->fd,(struct dirent64 *)dir->buf,PURE_DIRBUF_SIZE-1);
if (dir->bufsize <= 0)
return NULL;
else
dir->bufpos=0;
}
this=((struct dirent64 *) (dir->buf + dir->bufpos));
return this;
}
int dirfd(DIR *dir){
return dir->fd;
}
void rewinddir(DIR *dir){
lseek(dir->fd,0,SEEK_SET);
dir->bufsize=dir->bufpos=0;
}
void seekdir(DIR *dir, long int offset){
lseek(dir->fd,offset,SEEK_SET);
dir->bufsize=dir->bufpos=0;
}
long int telldir(DIR *dir){
off_t pos = lseek(dir->fd,0,SEEK_CUR);
if (pos != (off_t) -1)
return -1;
else
return pos + dir->bufpos;
}
#define NL_SIZE_INCR 100
typedef int(*filter_t)(const void *);
typedef int(*compar_t)(const void *, const void *);
typedef void *(*xreaddir_t)(DIR *dirp);
static int common_scandir(int dirfd, const char *dir, void ***namelist,
filter_t filter, compar_t compar, xreaddir_t xreaddir, size_t elsize){
int n = 0;
int size = 0;
DIR *d = opendirat(dirfd, dir);
void *de;
if (d == NULL)
return -1;
*namelist = NULL;
while ((errno = 0, de = xreaddir(d)) != NULL) {
if (filter && filter(de) == 0)
continue;
if (n >= size) {
int newsize = size + NL_SIZE_INCR;
void **newnamelist = realloc(*namelist, newsize * sizeof(void **));
if (newnamelist == NULL)
goto error;
size = newsize;
*namelist = newnamelist;
}
void *newel = malloc(elsize);
if (newel == NULL)
goto error;
(*namelist)[n] = newel;
memcpy(newel, de, elsize);
n++;
}
*namelist = realloc(*namelist, n * sizeof(void **));
if (n > 0)
qsort(*namelist, n, sizeof(void *), compar);
return n;
error:
if (*namelist) {
int i;
for (i = 0; i < n; i++)
free((*namelist)[i]);
free(namelist);
}
return -1;
}
#ifndef __USE_FILE_OFFSET64
int scandir(const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **)) {
return common_scandir(AT_FDCWD, dirp, (void *) namelist, (filter_t) filter, (compar_t) compar, (xreaddir_t) readdir, sizeof(struct dirent));
}
#endif
int scandir64(const char *dirp, struct dirent64 ***namelist,
int (*filter)(const struct dirent64 *),
int (*compar)(const struct dirent64 **, const struct dirent64 **)) {
return common_scandir(AT_FDCWD, dirp, (void *) namelist, (filter_t) filter, (compar_t) compar, (xreaddir_t) readdir, sizeof(struct dirent64));
}
#ifndef __USE_FILE_OFFSET64
int scandirat(int dirfd, const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **)) {
return common_scandir(dirfd, dirp, (void *) namelist, (filter_t) filter, (compar_t) compar, (xreaddir_t) readdir, sizeof(struct dirent));
}
#endif
int scandirat64(int dirfd, const char *dirp, struct dirent64 ***namelist,
int (*filter)(const struct dirent64 *),
int (*compar)(const struct dirent64 **, const struct dirent64 **)) {
return common_scandir(dirfd, dirp, (void *) namelist, (filter_t) filter, (compar_t) compar, (xreaddir_t) readdir, sizeof(struct dirent64));
}