Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: fusl/src/dirent/scandir.c

Issue 1573973002: Add a "fork" of musl as //fusl. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fusl/src/dirent/rewinddir.c ('k') | fusl/src/dirent/seekdir.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <dirent.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <errno.h>
6 #include <stddef.h>
7 #include "libc.h"
8
9 int scandir(const char *path, struct dirent ***res,
10 int (*sel)(const struct dirent *),
11 int (*cmp)(const struct dirent **, const struct dirent **))
12 {
13 DIR *d = opendir(path);
14 struct dirent *de, **names=0, **tmp;
15 size_t cnt=0, len=0;
16 int old_errno = errno;
17
18 if (!d) return -1;
19
20 while ((errno=0), (de = readdir(d))) {
21 if (sel && !sel(de)) continue;
22 if (cnt >= len) {
23 len = 2*len+1;
24 if (len > SIZE_MAX/sizeof *names) break;
25 tmp = realloc(names, len * sizeof *names);
26 if (!tmp) break;
27 names = tmp;
28 }
29 names[cnt] = malloc(de->d_reclen);
30 if (!names[cnt]) break;
31 memcpy(names[cnt++], de, de->d_reclen);
32 }
33
34 closedir(d);
35
36 if (errno) {
37 if (names) while (cnt-->0) free(names[cnt]);
38 free(names);
39 return -1;
40 }
41 errno = old_errno;
42
43 if (cmp) qsort(names, cnt, sizeof *names, (int (*)(const void *, const v oid *))cmp);
44 *res = names;
45 return cnt;
46 }
47
48 LFS64(scandir);
OLDNEW
« no previous file with comments | « fusl/src/dirent/rewinddir.c ('k') | fusl/src/dirent/seekdir.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698