1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*
* readdir.c --- Provide an open hook to read directories
*
* Arnold Robbins
* arnold@skeeve.com
* Written 7/2012
*/
/*
* Copyright (C) 2012 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GAWK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#define _BSD_SOURCE
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "config.h"
#include "gawkapi.h"
static const gawk_api_t *api; /* for convenience macros to work */
static awk_ext_id_t *ext_id;
int plugin_is_GPL_compatible;
/* ftype --- return type of file as a single character string */
static const char *
ftype(struct dirent *entry)
{
#ifdef DT_BLK
switch (entry->d_type) {
case DT_BLK: return "b";
case DT_CHR: return "c";
case DT_DIR: return "d";
case DT_FIFO: return "p";
case DT_LNK: return "l";
case DT_REG: return "f";
case DT_SOCK: return "s";
default:
case DT_UNKNOWN: return "u";
}
#else
struct stat sbuf;
if (lstat(entry->d_name, & sbuf) < 0)
return "u";
switch (sbuf.st_mode & S_IFMT) {
case S_IFREG: return "f";
case S_IFBLK: return "b";
case S_IFCHR: return "c";
case S_IFDIR: return "d";
#ifdef S_IFSOCK
case S_IFSOCK: return "s";
#endif
#ifdef S_IFIFO
case S_IFIFO: return "p";
#endif
#ifdef S_IFLNK
case S_IFLNK: return "l";
#endif
#ifdef S_IFDOOR /* Solaris weirdness */
case S_IFDOOR: return "D";
#endif /* S_IFDOOR */
}
return "u";
#endif
}
/* dir_get_record --- get one record at a time out of a directory */
static int
dir_get_record(char **out, struct iobuf_public *iobuf, int *errcode)
{
DIR *dp;
struct dirent *dirent;
char buf[1000];
size_t len;
if (out == NULL || iobuf == NULL || iobuf->opaque == NULL)
return EOF;
/*
* The caller sets *errcode to 0, so we should set it only if an
* error occurs.
*/
/* FIXME: Need stuff for setting RT */
dp = (DIR *) iobuf->opaque;
dirent = readdir(dp);
if (dirent == NULL)
return EOF;
sprintf(buf, "%ld/%s/%s", dirent->d_ino, dirent->d_name, ftype(dirent));
len = strlen(buf);
emalloc(*out, char *, len + 1, "dir_get_record");
strcpy(*out, buf);
return len;
}
/* dir_close --- close up when done */
static void
dir_close(struct iobuf_public *iobuf)
{
DIR *dp;
if (iobuf == NULL || iobuf->opaque == NULL)
return;
dp = (DIR *) iobuf->opaque;
closedir(dp);
iobuf->fd = -1;
}
/* dir_can_take_file --- return true if we want the file */
static int
dir_can_take_file(IOBUF_PUBLIC *iobuf)
{
struct stat sbuf;
int fd;
if (iobuf == NULL)
return 0;
fd = iobuf->fd;
return (fd >= 0 && fstat(fd, & sbuf) >= 0 && S_ISDIR(sbuf.st_mode));
}
/* dir_take_control_of --- set up input parser. We can assume that dir_can_take_file just returned true, and no state has changed since then. */
static int
dir_take_control_of(IOBUF_PUBLIC *iobuf)
{
struct stat sbuf;
DIR *dp;
dp = fdopendir(iobuf->fd);
if (dp == NULL)
return 0;
iobuf->opaque = dp;
iobuf->get_record = dir_get_record;
iobuf->close_func = dir_close;
return 1;
}
static awk_input_parser_t readdir_parser = {
"readdir",
dir_can_take_file,
dir_take_control_of,
NULL
};
#ifdef TEST_DUPLICATE
static awk_input_parser_t readdir_parser2 = {
"readdir2",
dir_can_take_file,
dir_take_control_of,
NULL
};
#endif
int dl_load(const gawk_api_t *const api_p, awk_ext_id_t id)
{
api = api_p;
ext_id = id;
if (api->major_version != GAWK_API_MAJOR_VERSION
|| api->minor_version < GAWK_API_MINOR_VERSION) {
fprintf(stderr, "readdir: version mismatch with gawk!\n");
fprintf(stderr, "\tmy version (%d, %d), gawk version (%d, %d)\n",
GAWK_API_MAJOR_VERSION, GAWK_API_MINOR_VERSION,
api->major_version, api->minor_version);
exit(1);
}
register_input_parser(& readdir_parser);
#ifdef TEST_DUPLICATE
register_input_parser(& readdir_parser2);
#endif
return 1;
}
|