Repository navigation
Expand file tree
/
Copy pathgetsdir.c
More file actions
322 lines (280 loc) · 8.88 KB
/
Copy pathgetsdir.c
File metadata and controls
322 lines (280 loc) · 8.88 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* getsdir.c
*
* Get and return a sorted directory listing
*
* Copyright (c) 1998 by James S. Seymour (jseymour@jimsun.LinxNet.com)
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Note: this code uses "wildmat.c", which has different copyright
* and licensing conditions. See the source, Luke.
*
*
* 2011: getsdir() has been simplified wrt memory management by
* Adam Lackorzynski
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "getsdir.h"
#include "intl.h"
#include "minicom.h"
/* locally defined constants */
#define MAX_CNT 100 /* number of entries to hold in holding buf */
typedef struct dat_buf { /* structure of input buffers */
struct dat_buf *nxt; /* pointer to next buffer */
unsigned cnt; /* data count in present buffer */
GETSDIR_ENTRY data[MAX_CNT]; /* data in present buffer */
} DAT_BUF;
static int g_sortflags; /* sort flags */
/* sort compare routines */
/*
* name: namecmpr
*
* purpose: return stat to qsort on comparison between name fields in
* directory entry.
*
* synopsis: static in namecmpr(d1, d2)
* GETSDIR_ENTRY *d1;
* GETSDIR_ENTRY *d2;
*
* input: See explanation of qsort
*
* process: See explanation of qsort
*
* output: See explanation of qsort
*
* notes: See explanation of qsort
*/
static int namecmpr(GETSDIR_ENTRY *d1, GETSDIR_ENTRY *d2)
{
if (g_sortflags & (GETSDIR_DIRSF | GETSDIR_DIRSL)) {
if (S_ISDIR((d1->mode)) && !S_ISDIR((d2->mode)))
return (g_sortflags & GETSDIR_DIRSF) ? -1 : 1;
else if (S_ISDIR((d2->mode)) && ! S_ISDIR((d1->mode)))
return (g_sortflags & GETSDIR_DIRSF) ? 1 : -1;
}
return (g_sortflags & GETSDIR_RSORT)
? strcmp(d2->fname, d1->fname) : strcmp(d1->fname, d2->fname);
} /* namecmpr */
/*
* name: timecmpr
*
* purpose: return stat to qsort on comparison between time fields in
* directory entry.
*
* synopsis: static in timecmpr(d1, d2)
* GETSDIR_ENTRY *d1;
* GETSDIR_ENTRY *d2;
*
* input: See explanation of qsort
*
* process: See explanation of qsort
*
* output: See explanation of qsort
*
* notes: See explanation of qsort
*/
static int timecmpr(GETSDIR_ENTRY *d1, GETSDIR_ENTRY *d2)
{
if (g_sortflags & (GETSDIR_DIRSF | GETSDIR_DIRSL)) {
if (S_ISDIR((d1->mode)) && !S_ISDIR((d2->mode)))
return (g_sortflags & GETSDIR_DIRSF) ? -1 : 1;
else if (S_ISDIR((d2->mode)) && ! S_ISDIR((d1->mode)))
return (g_sortflags & GETSDIR_DIRSF) ? 1 : -1;
}
return (g_sortflags & GETSDIR_RSORT)
? (d2->time - d1->time) : (d1->time - d2->time);
} /* timecmpr */
/*
* name: getsdir
*
* purpose: To return a directory listing - possibly sorted
*
* synopsis: #include <dirent.h>
*
* int getsdir(dirpath, pattern, sortflags, modemask, datptr, len)
* const char *dirpath;
* const char *pattern;
* int sortflags;
* mode_t modemask;
* GETSDIR_ENTRY **datptr;
* int *len;
*
* input: *dirpath - pointer to path to directory to get list of
* files from
* *pattern - pointer to optional wildmat pattern
* sortflags - specification flags of how to sort the
* resulting list. See descriptions below.
* modemask - caller-supplied mode mask. Bits in this will
* be ANDed against directory entries to determine
* whether to return them. Ignored if 0.
* **datptr - pointer to a destination pointer variable.
* getsdir will allocate the required amount
* of memory for the results and will return a
* pointer to the returned data in this variable.
*
* The data will be in the form:
* typedef struct dirEntry {
* char fname[MAXNAMLEN + 1];
* time_t time;
* mode_t mode;
* } GETSDIR_ENTRY;
* *len - pointer to int to contain length of longest
* string in returned array.
*
* process: For each 0..MAX_CNT values read from specified directory,
* allocates a temporary buffer to store the entries into.
* When end of directory is detected, merges the buffers into
* a single array of data and sorts into order based on key.
*
* output: Count of number of data items pointed to by datptr or
* -1 if error. errno may or may not be valid, based on
* type of error encountered.
*
* notes: If there is any error, -1 is returned.
*
* It is the caller's responsibility to free the memory
* block pointed to by datptr on return when done with the
* data, except in case of error return.
*
* See also: opendir(3C), readdir(3C), closedir(3C), qsort(3C)
*
* The pattern parameter is optional and may be 0-length or
* a NULL pointer.
*
* The sort flags affect the output as follows:
*
* GETSDIR_PARNT - include parent dir (..)
* GETSDIR_NSORT - sort by name
* GETSDIR_TSORT - sort by time (NSORT wins if both)
*
* The following are only meaningful if GETSDIR_NSORT or
* GETSDIR_TSORT are specified:
*
* GETSDIR_DIRSF - dirs first
* GETSDIR_DIRSL - dirs last
* GETSDIR_RSORT - reverse sort (does not affect
* GETSDIR_DIRSF/GETSDIR_DIRSL)
*
* So-called "hidden" files (those beginning with a ".") are
* not returned unless a pattern like ".*" is specified.
*
* The present directory (".") is never returned.
*/
int getsdir(const char *dirpath, const char *pattern, int sortflags,
mode_t modemask, GETSDIR_ENTRY **datptr, int *len)
{
unsigned cnt = 0; /* data count */
DIR *dirp; /* point to open dir */
struct dirent *dp; /* structure of dir as per system */
struct stat statbuf; /* structure of file stat as per system */
char fpath[BUFSIZ]; /* filename with dir path prepended */
int cmprstat;
g_sortflags = sortflags; /* for sort funcs */
*len = 0; /* longest name */
/* open the specified directory */
if ((dirp = opendir(dirpath)) == NULL)
return -1;
while ((dp = readdir(dirp)))
{
if (!strcmp(dp->d_name, "."))
continue;
if ((sortflags & GETSDIR_PARNT) && !strcmp(dp->d_name, ".."))
cmprstat = 1;
else if (pattern && *pattern)
cmprstat = wildmat(dp->d_name, pattern);
else
cmprstat = 1;
if (cmprstat)
{ /* matching name? */
*datptr = realloc(*datptr, sizeof(**datptr) * (cnt + 1));
if (!*datptr)
{
free(*datptr);
return -1;
}
/* copy the filename */
strncpy((*datptr)[cnt].fname, dp->d_name, MAXNAMLEN);
/* get information about the directory entry */
snprintf(fpath, sizeof(fpath), "%s/%s", dirpath, dp->d_name);
if (stat(fpath, &statbuf)) /* if error getting stat... */
continue;
if (modemask && !(S_IFMT & modemask & statbuf.st_mode))
continue;
int l;
if ((l = strlen(dp->d_name)) > *len)
*len = l;
(*datptr)[cnt].time = statbuf.st_mtime;
(*datptr)[cnt].mode = statbuf.st_mode;
(*datptr)[cnt].cflags = 0;
cnt++;
}
}
closedir(dirp); /* close file pointer */
/* post-process array by option */
if (cnt && sortflags) {
if (sortflags & GETSDIR_NSORT)
qsort(*datptr, cnt, sizeof(GETSDIR_ENTRY),
(int (*)(const void *, const void *))namecmpr);
else if (sortflags & GETSDIR_TSORT)
qsort(*datptr, cnt, sizeof(GETSDIR_ENTRY),
(int (*)(const void *, const void *))timecmpr);
}
return cnt;
} /* getsdir */
#ifdef GETSDIR_STANDALONE_TEST
/*
* debug for getsdir()
*
* usage: getsdir <dirpath>
*
*/
extern char *ctime(void);
void main(int argc, char **argv)
{
GETSDIR_ENTRY *dirdat;
int cnt, index;
int sortflags = 0;
mode_t modemask = (mode_t) 0;
int len;
if (argc != 4) {
fprintf(stderr,"usage: %s <dirpath> <pattern> <sortflags>\n", argv[0]);
exit(1);
}
switch (argv[3][0]) {
case 'n': sortflags = GETSDIR_NSORT;
break;
case 't': sortflags = GETSDIR_TSORT;
break;
}
/* sortflags |= GETSDIR_DIRSL | GETSDIR_RSORT; */
sortflags |= GETSDIR_DIRSF;
/* sortflags |= GETSDIR_PARNT; */
/* modemask = S_IFDIR | S_IFREG; */
printf("modemask==%x\n", modemask);
if ((cnt = getsdir(argv[1], argv[2], sortflags, modemask, &dirdat, &len)) == -1) {
fprintf(stderr, "%s: error getting directory\n", argv[0]);
exit(1);
}
printf(_("%d files:\n"), cnt);
for (index = 1; index <= cnt; ++index, ++dirdat)
printf("%2d: %-20s%s", index, dirdat->fname, ctime(&dirdat->time));
free(dirdat);
return 0;
}
#endif