001
2021-04-25
op
/* $OpenBSD: scandir.c,v 1.21 2019/06/28 13:32:41 deraadt Exp $ */
004
2021-04-25
op
* This is a modified verision of scandir that takes an explicit
005
2021-04-25
op
* directory file descriptor as parameter.
009
2021-04-25
op
* Copyright (c) 1983, 1993
010
2021-04-25
op
* The Regents of the University of California. All rights reserved.
012
2021-04-25
op
* Redistribution and use in source and binary forms, with or without
013
2021-04-25
op
* modification, are permitted provided that the following conditions
014
2021-04-25
op
* are met:
015
2021-04-25
op
* 1. Redistributions of source code must retain the above copyright
016
2021-04-25
op
* notice, this list of conditions and the following disclaimer.
017
2021-04-25
op
* 2. Redistributions in binary form must reproduce the above copyright
018
2021-04-25
op
* notice, this list of conditions and the following disclaimer in the
019
2021-04-25
op
* documentation and/or other materials provided with the distribution.
020
2021-04-25
op
* 3. Neither the name of the University nor the names of its contributors
021
2021-04-25
op
* may be used to endorse or promote products derived from this software
022
2021-04-25
op
* without specific prior written permission.
024
2021-04-25
op
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
025
2021-04-25
op
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
026
2021-04-25
op
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
027
2021-04-25
op
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
028
2021-04-25
op
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
029
2021-04-25
op
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
030
2021-04-25
op
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
031
2021-04-25
op
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
032
2021-04-25
op
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
033
2021-04-25
op
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
034
2021-04-25
op
* SUCH DAMAGE.
038
2021-04-25
op
* Scan the directory fd calling select to make a list of selected
039
2021-04-25
op
* directory entries then sort using qsort and compare routine dcomp.
040
2021-04-25
op
* Returns the number of entries and a pointer to a list of pointers to
041
2021-04-25
op
* struct dirent (through namelist). Returns -1 if there were any errors.
044
2021-04-25
op
#include "gmid.h"
046
2021-04-25
op
#include <sys/types.h>
047
2021-04-25
op
#include <sys/stat.h>
049
2021-04-25
op
#include <dirent.h>
050
2021-04-25
op
#include <errno.h>
051
2021-04-25
op
#include <stdint.h>
052
2021-04-25
op
#include <stdlib.h>
053
2021-04-25
op
#include <string.h>
055
2021-04-25
op
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
058
2021-04-25
op
* The DIRSIZ macro is the minimum record length which will hold the directory
059
2021-04-25
op
* entry. This requires the amount of space in struct dirent without the
060
2021-04-25
op
* d_name field, plus enough space for the name and a terminating nul byte
061
2021-04-25
op
* (dp->d_namlen + 1), rounded up to a 4 byte boundary.
063
2021-04-25
op
#undef DIRSIZ
064
2021-04-25
op
#define DIRSIZ(dp, namlen) \
065
2021-04-25
op
((sizeof(struct dirent) - sizeof(dp)->d_name) + \
066
2021-04-25
op
((namlen + 1 + 3) &~ 3))
069
2021-04-25
op
scandir_fd(int fd, struct dirent ***namelist,
070
2021-04-25
op
int (*select)(const struct dirent *),
071
2021-04-25
op
int (*dcomp)(const struct dirent **, const struct dirent **))
073
2021-04-25
op
struct dirent *d, *p, **names = NULL;
074
2021-04-25
op
size_t arraysz, namlen, nitems = 0;
075
2021-04-25
op
struct stat stb;
076
2021-04-25
op
DIR *dirp;
078
2021-04-25
op
if ((dirp = fdopendir(fd)) == NULL)
079
2021-04-25
op
return (-1);
080
2021-04-25
op
if (fstat(fd, &stb) == -1)
081
2021-04-25
op
goto fail;
084
2021-04-25
op
* estimate the array size by taking the size of the directory file
085
2021-04-25
op
* and dividing it by a multiple of the minimum size entry.
087
2021-04-25
op
arraysz = MAXIMUM(stb.st_size / 24, 16);
088
2021-04-25
op
if (arraysz > SIZE_MAX / sizeof(struct dirent *)) {
089
2021-04-25
op
errno = ENOMEM;
090
2021-04-25
op
goto fail;
092
2021-04-25
op
names = calloc(arraysz, sizeof(struct dirent *));
093
2021-04-25
op
if (names == NULL)
094
2021-04-25
op
goto fail;
096
2021-04-25
op
while ((d = readdir(dirp)) != NULL) {
097
2021-04-25
op
if (select != NULL && !(*select)(d))
098
2021-04-25
op
continue; /* just selected names */
101
2021-04-25
op
* Check to make sure the array has space left and
102
2021-04-25
op
* realloc the maximum size.
104
2021-04-25
op
if (nitems >= arraysz) {
105
2021-04-25
op
struct dirent **nnames;
107
2021-04-25
op
if (fstat(fd, &stb) == -1)
108
2021-04-25
op
goto fail;
110
2021-04-25
op
arraysz *= 2;
111
2021-04-25
op
if (SIZE_MAX / sizeof(struct dirent *) < arraysz)
112
2021-04-25
op
goto fail;
113
2021-04-25
op
nnames = reallocarray(names,
114
2021-04-25
op
arraysz, sizeof(struct dirent *));
115
2021-04-25
op
if (nnames == NULL)
116
2021-04-25
op
goto fail;
118
2021-04-25
op
names = nnames;
122
2021-04-25
op
* Make a minimum size copy of the data
124
2021-04-25
op
namlen = strlen(d->d_name);
125
2021-04-25
op
p = malloc(DIRSIZ(d, namlen));
126
2021-04-25
op
if (p == NULL)
127
2021-04-25
op
goto fail;
129
2021-04-25
op
p->d_ino = d->d_ino;
130
2021-04-25
op
p->d_type = d->d_type;
131
2021-04-25
op
memcpy(p->d_name, d->d_name, namlen + 1);
132
2021-04-25
op
names[nitems++] = p;
134
2021-04-25
op
closedir(dirp);
135
2021-04-25
op
if (nitems && dcomp != NULL)
136
2021-04-25
op
qsort(names, nitems, sizeof(struct dirent *),
137
2021-04-25
op
(int(*)(const void *, const void *))dcomp);
138
2021-04-25
op
*namelist = names;
139
2021-04-25
op
return (nitems);
142
2021-04-25
op
while (nitems > 0)
143
2021-04-25
op
free(names[--nitems]);
144
2021-04-25
op
free(names);
145
2021-04-25
op
closedir(dirp);
146
2021-04-25
op
return (-1);
150
2021-04-25
op
select_non_dot(const struct dirent *d)
152
2021-04-25
op
return strcmp(d->d_name, ".");
156
2021-04-25
op
select_non_dotdot(const struct dirent *d)
158
2021-04-25
op
return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");