Blame
Date:
Thu Jul 7 08:54:26 2022 UTC
Message:
don't set d_reclen

It's not actually used and doesn't seem to be available on DragonflyBSD
(at least) judging from the patching in dports:

https://github.com/DragonFlyBSD/DPorts/commit/f66bf875f75335ff63e0cad2f817a1c6e7106045
001
2021-04-25
op
/* $OpenBSD: scandir.c,v 1.21 2019/06/28 13:32:41 deraadt Exp $ */
002
2021-04-25
op
003
2021-04-25
op
/*
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.
006
2021-04-25
op
*/
007
2021-04-25
op
008
2021-04-25
op
/*
009
2021-04-25
op
* Copyright (c) 1983, 1993
010
2021-04-25
op
* The Regents of the University of California. All rights reserved.
011
2021-04-25
op
*
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.
023
2021-04-25
op
*
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.
035
2021-04-25
op
*/
036
2021-04-25
op
037
2021-04-25
op
/*
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.
042
2021-04-25
op
*/
043
2021-04-25
op
044
2021-04-25
op
#include "gmid.h"
045
2021-04-25
op
046
2021-04-25
op
#include <sys/types.h>
047
2021-04-25
op
#include <sys/stat.h>
048
2021-04-25
op
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>
054
2021-04-25
op
055
2021-04-25
op
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
056
2021-04-25
op
057
2021-04-25
op
/*
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.
062
2021-04-25
op
*/
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))
067
2021-04-25
op
068
2021-04-25
op
int
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 **))
072
2021-04-25
op
{
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;
077
2021-04-25
op
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;
082
2021-04-25
op
083
2021-04-25
op
/*
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.
086
2021-04-25
op
*/
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;
091
2021-04-25
op
}
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;
095
2021-04-25
op
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 */
099
2021-04-25
op
100
2021-04-25
op
/*
101
2021-04-25
op
* Check to make sure the array has space left and
102
2021-04-25
op
* realloc the maximum size.
103
2021-04-25
op
*/
104
2021-04-25
op
if (nitems >= arraysz) {
105
2021-04-25
op
struct dirent **nnames;
106
2021-04-25
op
107
2021-04-25
op
if (fstat(fd, &stb) == -1)
108
2021-04-25
op
goto fail;
109
2021-04-25
op
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;
117
2021-04-25
op
118
2021-04-25
op
names = nnames;
119
2021-04-25
op
}
120
2021-04-25
op
121
2021-04-25
op
/*
122
2021-04-25
op
* Make a minimum size copy of the data
123
2021-04-25
op
*/
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;
128
2021-04-25
op
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;
133
2021-04-25
op
}
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);
140
2021-04-25
op
141
2021-04-25
op
fail:
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);
147
2021-04-25
op
}
148
2021-04-25
op
149
2021-04-25
op
int
150
2021-04-25
op
select_non_dot(const struct dirent *d)
151
2021-04-25
op
{
152
2021-04-25
op
return strcmp(d->d_name, ".");
153
2021-04-25
op
}
154
2021-04-25
op
155
2021-04-25
op
int
156
2021-04-25
op
select_non_dotdot(const struct dirent *d)
157
2021-04-25
op
{
158
2021-04-25
op
return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
159
2021-04-25
op
}
Omar Polo