Debian Codegraph @

extension/readdir.c

raw
1 /*
2 * readdir.c --- Provide an input parser to read directories
3 *
4 * Arnold Robbins
5 * [email protected]
6 * Written 7/2012
7 *
8 * Andrew Schorr and Arnold Robbins: further fixes 8/2012.
9 * Simplified 11/2012.
10 * Improved 3/2019.
11 */
12
13 /*
14 * Copyright (C) 2012-2014, 2017, 2018, 2019, 2021, 2023,
15 * the Free Software Foundation, Inc.
16 *
17 * This file is part of GAWK, the GNU implementation of the
18 * AWK Programming Language.
19 *
20 * GAWK is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 3 of the License, or
23 * (at your option) any later version.
24 *
25 * GAWK is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #define _BSD_SOURCE
40 #include <stdio.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <stdbool.h>
46
47 #include <sys/types.h>
48 #include <sys/stat.h>
49
50 #ifdef HAVE_LIMITS_H
51 #include <limits.h>
52 #endif
53
54 #ifdef HAVE_DIRENT_H
55 #include <dirent.h>
56 #else
57 #error Cannot compile the readdir extension on this system!
58 #endif
59
60 #ifdef __MINGW32__
61 #define WIN32_LEAN_AND_MEAN
62 #include <windows.h>
63 #endif
64
65 #include "gawkapi.h"
66
67 #include "gawkdirfd.h"
68
69 #include "gettext.h"
70 #define _(msgid) gettext(msgid)
71 #define N_(msgid) msgid
72
73 #ifndef PATH_MAX
74 #define PATH_MAX 1024 /* a good guess */
75 #endif
76
77 static const gawk_api_t *api; /* for convenience macros to work */
78 static awk_ext_id_t ext_id;
79 static const char *ext_version = "readdir extension: version 3.0";
80
81 static awk_bool_t init_readdir(void);
82 static awk_bool_t (*init_func)(void) = init_readdir;
83
84 int plugin_is_GPL_compatible;
85
86 /* data type for the opaque pointer: */
87
88 typedef struct open_directory {
89 DIR *dp;
90 char *buf;
91 union {
92 awk_fieldwidth_info_t fw;
93 char buf[awk_fieldwidth_info_size(3)];
94 } u;
95 bool override;
96 } open_directory_t;
97 #define fw u.fw
98
99 /* ftype --- return type of file as a single character string */
100
101 static const char *
102 ftype(struct dirent *entry, const char *dirname)
103 {
104 #ifdef DT_BLK
105 (void) dirname; /* silence warnings */
106 switch (entry->d_type) {
107 case DT_BLK: return "b";
108 case DT_CHR: return "c";
109 case DT_DIR: return "d";
110 case DT_FIFO: return "p";
111 case DT_LNK: return "l";
112 case DT_REG: return "f";
113 case DT_SOCK: return "s";
114 default:
115 case DT_UNKNOWN: break; // JFS returns 'u', so fall through and stat
116 }
117 #endif
118 char fname[PATH_MAX];
119 struct stat sbuf;
120
121 sprintf(fname, "%s/%s", dirname, entry->d_name);
122 if (stat(fname, &sbuf) == 0) {
123 if (S_ISBLK(sbuf.st_mode))
124 return "b";
125 if (S_ISCHR(sbuf.st_mode))
126 return "c";
127 if (S_ISDIR(sbuf.st_mode))
128 return "d";
129 if (S_ISFIFO(sbuf.st_mode))
130 return "p";
131 if (S_ISREG(sbuf.st_mode))
132 return "f";
133 #ifdef S_ISLNK
134 if (S_ISLNK(sbuf.st_mode))
135 return "l";
136 #endif
137 #ifdef S_ISSOCK
138 if (S_ISSOCK(sbuf.st_mode))
139 return "s";
140 #endif
141 }
142 return "u";
143 }
144
145 /* get_inode --- get the inode of a file */
146
147 static long long
148 get_inode(struct dirent *entry, const char *dirname)
149 {
150 #ifdef __MINGW32__
151 char fname[PATH_MAX];
152 HANDLE fh;
153 BOOL ok;
154 BY_HANDLE_FILE_INFORMATION info;
155
156 sprintf(fname, "%s\\%s", dirname, entry->d_name);
157 fh = CreateFile(fname, 0, 0, NULL, OPEN_EXISTING,
158 FILE_FLAG_BACKUP_SEMANTICS, NULL);
159 if (fh == INVALID_HANDLE_VALUE)
160 return 0;
161 ok = GetFileInformationByHandle(fh, &info);
162 CloseHandle(fh);
163 if (ok) {
164 long long inode = info.nFileIndexHigh;
165
166 inode <<= 32;
167 inode += info.nFileIndexLow;
168 return inode;
169 }
170 return 0;
171 #else
172 (void) dirname; /* silence warnings */
173 return entry->d_ino;
174 #endif
175 }
176
177 /* dir_get_record --- get one record at a time out of a directory */
178
179 static int
180 dir_get_record(char **out, awk_input_buf_t *iobuf, int *errcode,
181 char **rt_start, size_t *rt_len,
182 const awk_fieldwidth_info_t **field_width)
183 {
184 DIR *dp;
185 struct dirent *dirent;
186 int len, flen;
187 open_directory_t *the_dir;
188 const char *ftstr;
189 unsigned long long ino;
190
191 /*
192 * The caller sets *errcode to 0, so we should set it only if an
193 * error occurs.
194 */
195
196 if (out == NULL || iobuf == NULL || iobuf->opaque == NULL)
197 return EOF;
198
199 the_dir = (open_directory_t *) iobuf->opaque;
200 dp = the_dir->dp;
201
202 /*
203 * Initialize errno, since readdir does not set it to zero on EOF.
204 */
205 errno = 0;
206 dirent = readdir(dp);
207 if (dirent == NULL) {
208 *errcode = errno; /* in case there was an error */
209 return EOF;
210 }
211
212 ino = get_inode(dirent, iobuf->name);
213
214 #if __MINGW32__
215 len = sprintf(the_dir->buf, "%I64u", ino);
216 #else
217 len = sprintf(the_dir->buf, "%llu", ino);
218 #endif
219 the_dir->fw.fields[0].len = len;
220 len += (flen = sprintf(the_dir->buf + len, "/%s", dirent->d_name));
221 the_dir->fw.fields[1].len = flen-1;
222
223 ftstr = ftype(dirent, iobuf->name);
224 len += (flen = sprintf(the_dir->buf + len, "/%s", ftstr));
225 the_dir->fw.fields[2].len = flen-1;
226
227 *out = the_dir->buf;
228
229 *rt_start = NULL;
230 *rt_len = 0; /* set RT to "" */
231 if (field_width != NULL && the_dir->override)
232 *field_width = & the_dir->fw;
233 return len;
234 }
235
236 /* dir_close --- close up when done */
237
238 static void
239 dir_close(awk_input_buf_t *iobuf)
240 {
241 open_directory_t *the_dir;
242
243 if (iobuf == NULL || iobuf->opaque == NULL)
244 return;
245
246 the_dir = (open_directory_t *) iobuf->opaque;
247
248 closedir(the_dir->dp);
249 gawk_free(the_dir->buf);
250 gawk_free(the_dir);
251
252 iobuf->fd = -1;
253 }
254
255 /* dir_can_take_file --- return true if we want the file */
256
257 static awk_bool_t
258 dir_can_take_file(const awk_input_buf_t *iobuf)
259 {
260 if (iobuf == NULL)
261 return awk_false;
262
263 return (S_ISDIR(iobuf->sbuf.st_mode));
264 }
265
266 /*
267 * dir_take_control_of --- set up input parser.
268 * We can assume that dir_can_take_file just returned true,
269 * and no state has changed since then.
270 */
271
272 static awk_bool_t
273 dir_take_control_of(awk_input_buf_t *iobuf)
274 {
275 DIR *dp;
276 open_directory_t *the_dir;
277 size_t size;
278
279 errno = 0;
280 #ifdef HAVE_FDOPENDIR
281 dp = fdopendir(iobuf->fd);
282 #else
283 dp = opendir(iobuf->name);
284 if (dp != NULL) {
285 if (iobuf->fd != INVALID_HANDLE)
286 (void) close(iobuf->fd);
287 iobuf->fd = dirfd(dp);
288 }
289 #endif
290 if (dp == NULL) {
291 warning(ext_id, _("dir_take_control_of: %s: opendir/fdopendir failed: %s"),
292 iobuf->name, strerror(errno));
293 update_ERRNO_int(errno);
294 return awk_false;
295 }
296
297 emalloc(the_dir, open_directory_t *, sizeof(open_directory_t), "dir_take_control_of");
298 the_dir->dp = dp;
299 /* pre-populate the field_width struct with constant values: */
300 the_dir->fw.use_chars = awk_false;
301 the_dir->fw.nf = 3;
302 the_dir->fw.fields[0].skip = 0; /* no leading space */
303 the_dir->fw.fields[1].skip = 1; /* single '/' separator */
304 the_dir->fw.fields[2].skip = 1; /* single '/' separator */
305 size = sizeof(struct dirent) + 21 /* max digits in inode */ + 2 /* slashes */;
306 emalloc(the_dir->buf, char *, size, "dir_take_control_of");
307
308 iobuf->opaque = the_dir;
309 iobuf->get_record = dir_get_record;
310 iobuf->close_func = dir_close;
311
312 awk_value_t array, index, value;
313 static const char readdir_override[] = "readdir_override";
314
315 if (! sym_lookup("PROCINFO", AWK_ARRAY, & array)) {
316 the_dir->override = false;
317 } else {
318 (void) make_const_string(readdir_override, strlen(readdir_override), & index);
319 the_dir->override = get_array_element(array.array_cookie, & index, AWK_UNDEFINED, & value);
320 }
321
322 return awk_true;
323 }
324
325 static awk_input_parser_t readdir_parser = {
326 "readdir",
327 dir_can_take_file,
328 dir_take_control_of,
329 NULL
330 };
331
332 #ifdef TEST_DUPLICATE
333 static awk_input_parser_t readdir_parser2 = {
334 "readdir2",
335 dir_can_take_file,
336 dir_take_control_of,
337 NULL
338 };
339 #endif
340
341 /* init_readdir --- set things ups */
342
343 static awk_bool_t
344 init_readdir()
345 {
346 register_input_parser(& readdir_parser);
347 #ifdef TEST_DUPLICATE
348 register_input_parser(& readdir_parser2);
349 #endif
350
351 return awk_true;
352 }
353
354 static awk_ext_func_t func_table[] = {
355 { NULL, NULL, 0, 0, awk_false, NULL }
356 };
357
358 /* define the dl_load function using the boilerplate macro */
359
360 dl_load_func(func_table, readdir, "")