1
/* ========================================================================
2
* Copyright 1988-2006 University of Washington
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
11
* ========================================================================
15
* Program: Scan directories
17
* Author: Mark Crispin
18
* Networks and Distributed Computing
19
* Computing & Communications
20
* University of Washington
21
* Administration Building, AG-44
23
* Internet: MRC@CAC.Washington.EDU
26
* Last Edited: 15 September 2006
29
/* Emulator for BSD scandir() call
30
* Accepts: directory name
31
* destination pointer of names array
34
* Returns: number of elements in the array or -1 if error
37
int scandir (char *dirname,struct direct ***namelist,select_t select,
40
struct direct *p,*d,**names;
44
DIR *dirp = opendir (dirname);/* open directory and get status poop */
45
if ((!dirp) || (fstat (dirp->dd_fd,&stb) < 0)) return -1;
46
nlmax = stb.st_size / 24; /* guesstimate at number of files */
47
names = (struct direct **) fs_get (nlmax * sizeof (struct direct *));
48
nitems = 0; /* initially none found */
49
while (d = readdir (dirp)) { /* read directory item */
50
/* matches select criterion? */
51
if (select && !(*select) (d)) continue;
52
/* get size of direct record for this file */
53
p = (struct direct *) fs_get (DIR_SIZE (d));
54
p->d_ino = d->d_ino; /* copy the poop */
55
strcpy (p->d_name,d->d_name);
56
if (++nitems >= nlmax) { /* if out of space, try bigger guesstimate */
57
void *s = (void *) names; /* stupid language */
58
nlmax *= 2; /* double it */
59
fs_resize ((void **) &s,nlmax * sizeof (struct direct *));
60
names = (struct direct **) s;
62
names[nitems - 1] = p; /* store this file there */
64
closedir (dirp); /* done with directory */
65
/* sort if necessary */
66
if (nitems && compar) qsort (names,nitems,sizeof (struct direct *),compar);
67
*namelist = names; /* return directory */
68
return nitems; /* and size */
71
/* Alphabetic file name comparision
72
* Accepts: first candidate directory entry
73
* second candidate directory entry
74
* Returns: negative if d1 < d2, 0 if d1 == d2, postive if d1 > d2
77
int alphasort (void *d1,void *d2)
79
return strcmp ((*(struct direct **) d1)->d_name,
80
(*(struct direct **) d2)->d_name);