This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsortdir.c
More file actions
197 lines (177 loc) · 4.95 KB
/
libsortdir.c
File metadata and controls
197 lines (177 loc) · 4.95 KB
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
/*
libsortdir -- sort directory entries
Copyright (C) 2002, 2003, 2007 Egmont Koblinger <egmont@uhulinux.hu>
This library 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.
This 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 this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <limits.h>
struct dirlist {
int pos;
int max;
struct dirent *entries;
struct dirent64 *entries64;
};
static struct dirlist dirlist[1024];
static locale_t locale = 0;
static int verscmp = 0;
static int reverse = 0;
static struct dirent *(*next_readdir)(DIR *dir);
static struct dirent64 *(*next_readdir64)(DIR *dir);
static DIR *(*next_opendir)(const char *name);
static DIR *(*next_fdopendir)(int fd);
static int (*next_closedir)(DIR *dir);
static int do_wrap = 0;
void sortdir_init (void) __attribute((constructor));
void sortdir_init (void)
{
char *sortdir_locale;
if (getenv("SORTDIR_VERSCMP") != NULL) {
verscmp = 1;
}
if ((sortdir_locale = getenv("SORTDIR_LOCALE")) != NULL) {
locale = newlocale(LC_ALL_MASK, sortdir_locale, NULL);
}
if (getenv("SORTDIR_REVERSE") != NULL) {
reverse = 1;
}
next_readdir = dlsym(RTLD_NEXT, "readdir");
next_readdir64 = dlsym(RTLD_NEXT, "readdir64");
next_opendir = dlsym(RTLD_NEXT, "opendir");
next_fdopendir = dlsym(RTLD_NEXT, "fdopendir");
next_closedir = dlsym(RTLD_NEXT, "closedir");
do_wrap = 1;
}
static int do_compare (const char *a, const char *b)
{
int i;
if (verscmp) {
i = strverscmp(a, b);
} else if (locale == 0) {
i = strcoll(a, b);
} else {
i = strcoll_l(a, b, locale);
}
return reverse ? -i : i;
}
static int compare (const void *a, const void *b)
{
const struct dirent *a2 = a;
const struct dirent *b2 = b;
return do_compare(a2->d_name, b2->d_name);
}
static int compare64 (const void *a, const void *b)
{
const struct dirent64 *a2 = a;
const struct dirent64 *b2 = b;
return do_compare(a2->d_name, b2->d_name);
}
struct dirent *readdir (DIR *dir)
{
int fd;
struct dirent *dirent;
dirent = next_readdir(dir);
if (!do_wrap) return (struct dirent *) dirent;
do_wrap = 0;
fd = dirfd(dir);
if (dirlist[fd].pos == dirlist[fd].max) {
do_wrap = 1;
return NULL;
}
dirent = &dirlist[fd].entries[dirlist[fd].pos];
dirlist[fd].pos++;
do_wrap = 1;
return (struct dirent *) dirent;
}
struct dirent64 *readdir64 (DIR *dir)
{
int fd;
struct dirent64 *dirent;
dirent = next_readdir64(dir);
if (!do_wrap) return (struct dirent64 *) dirent;
do_wrap = 0;
fd = dirfd(dir);
if (dirlist[fd].pos == dirlist[fd].max) {
do_wrap = 1;
return NULL;
}
dirent = &dirlist[fd].entries64[dirlist[fd].pos];
dirlist[fd].pos++;
do_wrap = 1;
return (struct dirent64 *) dirent;
}
static DIR *opendir_common (DIR *dir)
{
struct dirent *dirent;
int fd;
int i = 0;
int alloc = 64;
if (dir == NULL) return dir;
if (!do_wrap) return dir;
do_wrap = 0;
fd = dirfd(dir);
dirlist[fd].entries = malloc(alloc * sizeof(struct dirent));
dirlist[fd].entries64 = malloc(alloc * sizeof(struct dirent64));
while ((dirent = next_readdir(dir)) != NULL) {
if (i >= alloc) {
alloc *= 2;
dirlist[fd].entries = realloc(dirlist[fd].entries, alloc * sizeof(struct dirent));
dirlist[fd].entries64 = realloc(dirlist[fd].entries64, alloc * sizeof(struct dirent64));
}
memcpy(&dirlist[fd].entries[i], dirent, sizeof(struct dirent));
dirlist[fd].entries64[i].d_ino = dirent->d_ino;
dirlist[fd].entries64[i].d_off = dirent->d_off;
dirlist[fd].entries64[i].d_reclen = dirent->d_reclen;
dirlist[fd].entries64[i].d_type = dirent->d_type;
strcpy(dirlist[fd].entries64[i].d_name, dirent->d_name);
i++;
}
qsort(dirlist[fd].entries, i, sizeof(struct dirent), compare);
qsort(dirlist[fd].entries64, i, sizeof(struct dirent64), compare64);
dirlist[fd].pos = 0;
dirlist[fd].max = i;
do_wrap = 1;
rewinddir(dir);
return dir;
}
DIR *opendir (const char *name)
{
return opendir_common(next_opendir(name));
}
DIR *fdopendir (int fd)
{
return opendir_common(next_fdopendir(fd));
}
int closedir (DIR *dir)
{
int fd;
int retval;
if (!do_wrap) return next_closedir(dir);
do_wrap = 0;
fd = dirfd(dir);
free(dirlist[fd].entries64);
free(dirlist[fd].entries);
retval = next_closedir(dir);
do_wrap = 1;
return retval;
}