summaryrefslogtreecommitdiff
path: root/extension/filefuncs.c
blob: 194db28f0d641873f3775b7ee28fd1470d61721a (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * filefuncs.c - Builtin functions that provide initial minimal iterface
 *		 to the file system.
 *
 * Arnold Robbins, update for 3.1, Mon Nov 23 12:53:39 EST 1998
 */

/*
 * Copyright (C) 2001 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 2 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 */

#include "awk.h"

#include <sys/sysmacros.h>

/*  do_chdir --- provide dynamically loaded chdir() builtin for gawk */

static NODE *
do_chdir(tree)
NODE *tree;
{
	NODE *newdir;
	int ret = -1;

	if  (do_lint && tree->param_cnt > 1)
		lintwarn("chdir: called with too many arguments");

	newdir = get_argument(tree, 0);
	if (newdir != NULL) {
		(void) force_string(newdir);
		ret = chdir(newdir->stptr);
		if (ret < 0)
			update_ERRNO();

		free_temp(newdir);
	} else if (do_lint)
		lintwarn("chdir: called with no arguments");


	/* Set the return value */
	set_value(tmp_number((AWKNUM) ret));

	/* Just to make the interpreter happy */
	return tmp_number((AWKNUM) 0);
}

/* format_mode --- turn a stat mode field into something readable */

static char *
format_mode(fmode)
unsigned long fmode;
{
	static char outbuf[12];
	int i;

	strcpy(outbuf, "----------");
	/* first, get the file type */
	i = 0;
	switch (fmode & S_IFMT) {
#ifdef S_IFSOCK
	case S_IFSOCK:
		outbuf[i] = 's';
		break;
#endif
#ifdef S_IFLNK
	case S_IFLNK:
		outbuf[i] = 'l';
		break;
#endif
	case S_IFREG:
		outbuf[i] = '-';	/* redundant */
		break;
	case S_IFBLK:
		outbuf[i] = 'b';
		break;
	case S_IFDIR:
		outbuf[i] = 'd';
		break;
#ifdef S_IFDOOR	/* Solaris weirdness */
	case S_IFDOOR:
		outbuf[i] = 'D';
		break;
#endif /* S_IFDOOR */
	case S_IFCHR:
		outbuf[i] = 'c';
		break;
#ifdef S_IFIFO
	case S_IFIFO:
		outbuf[i] = 'p';
		break;
#endif
	}

	i++;
	if ((fmode & S_IRUSR) != 0)
		outbuf[i] = 'r';
	i++;
	if ((fmode & S_IWUSR) != 0)
		outbuf[i] = 'w';
	i++;
	if ((fmode & S_IXUSR) != 0)
		outbuf[i] = 'x';
	i++;

	if ((fmode & S_IRGRP) != 0)
		outbuf[i] = 'r';
	i++;
	if ((fmode & S_IWGRP) != 0)
		outbuf[i] = 'w';
	i++;
	if ((fmode & S_IXGRP) != 0)
		outbuf[i] = 'x';
	i++;

	if ((fmode & S_IROTH) != 0)
		outbuf[i] = 'r';
	i++;
	if ((fmode & S_IWOTH) != 0)
		outbuf[i] = 'w';
	i++;
	if ((fmode & S_IXOTH) != 0)
		outbuf[i] = 'x';
	i++;

	outbuf[i] = '\0';

	if ((fmode & S_ISUID) != 0) {
		if (outbuf[3] == 'x')
			outbuf[3] = 's';
		else
			outbuf[3] = 'S';
	}

	/* setgid without execute == locking */
	if ((fmode & S_ISGID) != 0) {
		if (outbuf[6] == 'x')
			outbuf[6] = 's';
		else
			outbuf[6] = 'l';
	}

	if ((fmode & S_ISVTX) != 0) {
		if (outbuf[9] == 'x')
			outbuf[9] = 't';
		else
			outbuf[9] = 'T';
	}

	return outbuf;
}

/* do_stat --- provide a stat() function for gawk */

static NODE *
do_stat(tree)
NODE *tree;
{
	NODE *file, *array;
	struct stat sbuf;
	int ret;
	NODE **aptr;
	char *pmode;	/* printable mode */
	char *type = "unknown";

	/* check arg count */
	if (tree->param_cnt != 2)
		fatal(
	"stat: called with incorrect number of arguments (%d), should be 2",
			tree->param_cnt);

	/* directory is first arg, array to hold results is second */
	file = get_argument(tree, 0);
	array = get_argument(tree, 1);

	array = get_array(array);

	/* empty out the array */
	assoc_clear(array);

	/* lstat the file, if error, set ERRNO and return */
	(void) force_string(file);
	ret = lstat(file->stptr, & sbuf);
	if (ret < 0) {
		update_ERRNO();

		set_value(tmp_number((AWKNUM) ret));

		free_temp(file);
		return tmp_number((AWKNUM) 0);
	}

	/* fill in the array */
	aptr = assoc_lookup(array, tmp_string("name", 4), FALSE);
	*aptr = dupnode(file);

	aptr = assoc_lookup(array, tmp_string("dev", 3), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_dev);

	aptr = assoc_lookup(array, tmp_string("ino", 3), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_ino);

	aptr = assoc_lookup(array, tmp_string("mode", 4), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_mode);

	aptr = assoc_lookup(array, tmp_string("nlink", 5), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_nlink);

	aptr = assoc_lookup(array, tmp_string("uid", 3), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_uid);

	aptr = assoc_lookup(array, tmp_string("gid", 3), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_gid);

	aptr = assoc_lookup(array, tmp_string("size", 4), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_size);

	aptr = assoc_lookup(array, tmp_string("blocks", 6), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_blocks);

	aptr = assoc_lookup(array, tmp_string("atime", 5), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_atime);

	aptr = assoc_lookup(array, tmp_string("mtime", 5), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_mtime);

	aptr = assoc_lookup(array, tmp_string("ctime", 5), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_ctime);

	/* for block and character devices, add rdev, major and minor numbers */
	if (S_ISBLK(sbuf.st_mode) || S_ISCHR(sbuf.st_mode)) {
		aptr = assoc_lookup(array, tmp_string("rdev", 4), FALSE);
		*aptr = make_number((AWKNUM) sbuf.st_rdev);

		aptr = assoc_lookup(array, tmp_string("major", 5), FALSE);
		*aptr = make_number((AWKNUM) major(sbuf.st_rdev));

		aptr = assoc_lookup(array, tmp_string("minor", 5), FALSE);
		*aptr = make_number((AWKNUM) minor(sbuf.st_rdev));
	}

#ifdef HAVE_ST_BLKSIZE
	aptr = assoc_lookup(array, tmp_string("blksize", 7), FALSE);
	*aptr = make_number((AWKNUM) sbuf.st_blksize);
#endif /* HAVE_ST_BLKSIZE */

	aptr = assoc_lookup(array, tmp_string("pmode", 5), FALSE);
	pmode = format_mode(sbuf.st_mode);
	*aptr = make_string(pmode, strlen(pmode));

	/* for symbolic links, add a linkval field */
	if (S_ISLNK(sbuf.st_mode)) {
		char buf[BUFSIZ*2];
		int linksize;

		linksize = readlink(file->stptr, buf, sizeof buf);
		/* should make this smarter */
		if (linksize == sizeof(buf))
			fatal("size of symbolic link too big");
		buf[linksize] = '\0';

		aptr = assoc_lookup(array, tmp_string("linkval", 7), FALSE);
		*aptr = make_string(buf, linksize);
	}

	/* add a type field */
	switch (sbuf.st_mode & S_IFMT) {
#ifdef S_IFSOCK
	case S_IFSOCK:
		type = "socket";
		break;
#endif
#ifdef S_IFLNK
	case S_IFLNK:
		type = "symlink";
		break;
#endif
	case S_IFREG:
		type = "file";
		break;
	case S_IFBLK:
		type = "blockdev";
		break;
	case S_IFDIR:
		type = "directory";
		break;
#ifdef S_IFDOOR
	case S_IFDOOR:
		type = "door";
		break;
#endif
	case S_IFCHR:
		type = "chardev";
		break;
#ifdef S_IFIFO
	case S_IFIFO:
		type = "fifo";
		break;
#endif
	}

	aptr = assoc_lookup(array, tmp_string("type", 4), FALSE);
	*aptr = make_string(type, strlen(type));

	free_temp(file);

	/* Set the return value */
	set_value(tmp_number((AWKNUM) ret));

	/* Just to make the interpreter happy */
	return tmp_number((AWKNUM) 0);
}

/* dlload --- load new builtins in this library */

NODE *
dlload(tree, dl)
NODE *tree;
void *dl;
{
	make_builtin("chdir", do_chdir, 1);
	make_builtin("stat", do_stat, 2);

	return tmp_number((AWKNUM) 0);
}