1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* util/support/dir_filenames.c - fetch filenames in a directory */
3 /*
4 * Copyright (C) 2018 by the Massachusetts Institute of Technology.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "k5-platform.h"
34
35 void
k5_free_filenames(char ** fnames)36 k5_free_filenames(char **fnames)
37 {
38 char **fn;
39
40 for (fn = fnames; fn != NULL && *fn != NULL; fn++)
41 free(*fn);
42 free(fnames);
43 }
44
45 /* Resize the filename list and add a name. */
46 static int
add_filename(char *** fnames,int * n_fnames,const char * name)47 add_filename(char ***fnames, int *n_fnames, const char *name)
48 {
49 char **newlist;
50
51 newlist = realloc(*fnames, (*n_fnames + 2) * sizeof(*newlist));
52 if (newlist == NULL)
53 return ENOMEM;
54 *fnames = newlist;
55 newlist[*n_fnames] = strdup(name);
56 if (newlist[*n_fnames] == NULL)
57 return ENOMEM;
58 (*n_fnames)++;
59 newlist[*n_fnames] = NULL;
60 return 0;
61 }
62
63 static int
compare_with_strcmp(const void * a,const void * b)64 compare_with_strcmp(const void *a, const void *b)
65 {
66 return strcmp(*(char **)a, *(char **)b);
67 }
68
69 #ifdef _WIN32
70
71 int
k5_dir_filenames(const char * dirname,char *** fnames_out)72 k5_dir_filenames(const char *dirname, char ***fnames_out)
73 {
74 char *wildcard;
75 WIN32_FIND_DATA ffd;
76 HANDLE handle;
77 char **fnames = NULL;
78 int n_fnames = 0;
79
80 *fnames_out = NULL;
81
82 if (asprintf(&wildcard, "%s\\*", dirname) < 0)
83 return ENOMEM;
84 handle = FindFirstFile(wildcard, &ffd);
85 free(wildcard);
86 if (handle == INVALID_HANDLE_VALUE)
87 return ENOENT;
88
89 do {
90 if (add_filename(&fnames, &n_fnames, ffd.cFileName) != 0) {
91 k5_free_filenames(fnames);
92 FindClose(handle);
93 return ENOMEM;
94 }
95 } while (FindNextFile(handle, &ffd) != 0);
96
97 FindClose(handle);
98 qsort(fnames, n_fnames, sizeof(*fnames), compare_with_strcmp);
99 *fnames_out = fnames;
100 return 0;
101 }
102
103 #else /* _WIN32 */
104
105 #include <dirent.h>
106
107 int
k5_dir_filenames(const char * dirname,char *** fnames_out)108 k5_dir_filenames(const char *dirname, char ***fnames_out)
109 {
110 DIR *dir;
111 struct dirent *ent;
112 char **fnames = NULL;
113 int n_fnames = 0;
114
115 *fnames_out = NULL;
116
117 dir = opendir(dirname);
118 if (dir == NULL)
119 return ENOENT;
120
121 while ((ent = readdir(dir)) != NULL) {
122 if (add_filename(&fnames, &n_fnames, ent->d_name) != 0) {
123 k5_free_filenames(fnames);
124 closedir(dir);
125 return ENOMEM;
126 }
127 }
128
129 closedir(dir);
130 qsort(fnames, n_fnames, sizeof(*fnames), compare_with_strcmp);
131 *fnames_out = fnames;
132 return 0;
133 }
134
135 #endif /* not _WIN32 */
136