aboutsummaryrefslogtreecommitdiff
path: root/Src/Modules/stat.c
blob: ab4316f7b206819c690a87acb12ddd9e2e3aae54 (plain) (blame)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
 * stat.c - stat builtin interface to system call
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 1996-1997 Peter Stephenson
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Peter Stephenson or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Peter Stephenson and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Peter Stephenson and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Peter Stephenson and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

#include "stat.mdh"
#include "stat.pro"

enum statnum { ST_DEV, ST_INO, ST_MODE, ST_NLINK, ST_UID, ST_GID,
		   ST_RDEV, ST_SIZE, ST_ATIM, ST_MTIM, ST_CTIM,
		   ST_BLKSIZE, ST_BLOCKS, ST_READLINK, ST_COUNT };
enum statflags { STF_NAME = 1,  STF_FILE = 2, STF_STRING = 4, STF_RAW = 8,
		     STF_PICK = 16, STF_ARRAY = 32, STF_GMT = 64,
		     STF_HASH = 128 };
static char *statelts[] = { "device", "inode", "mode", "nlink",
				"uid", "gid", "rdev", "size", "atime",
				"mtime", "ctime", "blksize", "blocks",
				"link", NULL };
#define HNAMEKEY "name"

/**/
static void
statmodeprint(mode_t mode, char *outbuf, int flags)
{
    if (flags & STF_RAW) {
	sprintf(outbuf, "%lu", (unsigned long)mode);
	if (flags & STF_STRING)
	    strcat(outbuf, " (");
    }
    if (flags & STF_STRING) {
	static const char *modes = "?rwxrwxrwx";
	static const mode_t mflags[] = { S_IRUSR, S_IWUSR, S_IXUSR,
					 S_IRGRP, S_IWGRP, S_IXGRP,
					 S_IROTH, S_IWOTH, S_IXOTH };
	const mode_t *mfp = mflags;
	char pm[11];
	int i;

	if (S_ISBLK(mode))
	    *pm = 'b';
	else if (S_ISCHR(mode))
	    *pm = 'c';
	else if (S_ISDIR(mode))
	    *pm = 'd';
	else if (S_ISDOOR(mode))
	    *pm = 'D';
	else if (S_ISFIFO(mode))
	    *pm = 'p';
	else if (S_ISLNK(mode))
	    *pm = 'l';
	else if (S_ISMPC(mode))
	    *pm = 'm';
	else if (S_ISNWK(mode))
	    *pm = 'n';
	else if (S_ISOFD(mode))
	    *pm = 'M';
	else if (S_ISOFL(mode))
	    *pm = 'M';
	else if (S_ISREG(mode))
	    *pm = '-';
	else if (S_ISSOCK(mode))
	    *pm = 's';
	else
	    *pm = '?';

	for (i = 1; i <= 9; i++)
	    pm[i] = (mode & *mfp++) ? modes[i] : '-';
	pm[10] = '\0';

	if (mode & S_ISUID)
	    pm[3] = (mode & S_IXUSR) ? 's' : 'S';
	if (mode & S_ISGID)
	    pm[6] = (mode & S_IXGRP) ? 's' : 'S';
	if (mode & S_ISVTX)
	    pm[9] = (mode & S_IXOTH) ? 't' : 'T';

	pm[10] = 0;
	strcat(outbuf, pm);
	if (flags & STF_RAW)
	    strcat(outbuf, ")");
    }
}


/**/
static void
statuidprint(uid_t uid, char *outbuf, int flags)
{
    if (flags & STF_RAW) {
	sprintf(outbuf, "%lu", (unsigned long)uid);
	if (flags & STF_STRING)
	    strcat(outbuf, " (");
    }
    if (flags & STF_STRING) {
#ifdef HAVE_GETPWUID
	struct passwd *pwd;
	pwd = getpwuid(uid);
	strcat(outbuf, pwd ? pwd->pw_name : "???");
#else /* !HAVE_GETPWUID */
	strcat(outbuf, "???");
#endif /* !HAVE_GETPWUID */
	if (flags & STF_RAW)
	    strcat(outbuf, ")");
    }
}


/**/
static void
statgidprint(gid_t gid, char *outbuf, int flags)
{
    if (flags & STF_RAW) {
	sprintf(outbuf, "%lu", (unsigned long)gid);
	if (flags & STF_STRING)
	    strcat(outbuf, " (");
    }
    if (flags & STF_STRING) {
#ifdef HAVE_GETGRGID
	struct group *gr;
	gr = getgrgid(gid);
	strcat(outbuf, gr ? gr->gr_name : "???");
#else /* !HAVE_GETGRGID */
	strcat(outbuf, "???");
#endif /* !HAVE_GETGRGID */
	if (flags & STF_RAW)
	    strcat(outbuf, ")");
    }
}

static char *timefmt;

/**/
static void
stattimeprint(time_t tim, char *outbuf, int flags)
{
    if (flags & STF_RAW) {
	sprintf(outbuf, "%ld", (unsigned long)tim);
	if (flags & STF_STRING)
	    strcat(outbuf, " (");
    }
    if (flags & STF_STRING) {
	char *oend = outbuf + strlen(outbuf);
	ztrftime(oend, 40, timefmt, (flags & STF_GMT) ? gmtime(&tim) :
		 localtime(&tim));
	if (flags & STF_RAW)
	    strcat(outbuf, ")");
    }
}


/**/
static void
statulprint(unsigned long num, char *outbuf)
{
    sprintf(outbuf, "%lu", num);
}


/**/
static void
statlinkprint(struct stat *sbuf, char *outbuf, char *fname)
{
    int num;

    /* fname is NULL if we are looking at an fd */
    if (fname && S_ISLNK(sbuf->st_mode) &&
 	(num = readlink(fname, outbuf, PATH_MAX)) > 0) {
	/* readlink doesn't terminate the buffer itself */
	outbuf[num] = '\0';
    }
}


/**/
static void
statprint(struct stat *sbuf, char *outbuf, char *fname, int iwhich, int flags)
{
    char *optr = outbuf;

    if (flags & STF_NAME) {
	sprintf(outbuf, (flags & (STF_PICK|STF_ARRAY)) ?
		"%s " : "%-8s", statelts[iwhich]);
	optr += strlen(outbuf);
    }
    *optr = '\0';

    /* cast values to unsigned long as safest bet */
    switch (iwhich) {
    case ST_DEV:
	statulprint((unsigned long)sbuf->st_dev, optr);
	break;

    case ST_INO:
#ifdef INO_T_IS_64_BIT
	convbase(optr, sbuf->st_ino, 0);
#else
	DPUTS(sizeof(sbuf->st_ino) > 4,
	      "Shell compiled with wrong ino_t size");
	statulprint((unsigned long)sbuf->st_ino, optr);
#endif
	break;

    case ST_MODE:
	statmodeprint(sbuf->st_mode, optr, flags);
	break;

    case ST_NLINK:
	statulprint((unsigned long)sbuf->st_nlink, optr);
	break;

    case ST_UID:
	statuidprint(sbuf->st_uid, optr, flags);
	break;

    case ST_GID:
	statgidprint(sbuf->st_gid, optr, flags);
	break;

    case ST_RDEV:
	statulprint((unsigned long)sbuf->st_rdev, optr);
	break;

    case ST_SIZE:
#ifdef OFF_T_IS_64_BIT
	convbase(optr, sbuf->st_size, 0);
#else
	DPUTS(sizeof(sbuf->st_size) > 4,
	      "Shell compiled with wrong off_t size");
	statulprint((unsigned long)sbuf->st_size, optr);
#endif
	break;

    case ST_ATIM:
	stattimeprint(sbuf->st_atime, optr, flags);
	break;

    case ST_MTIM:
	stattimeprint(sbuf->st_mtime, optr, flags);
	break;

    case ST_CTIM:
	stattimeprint(sbuf->st_ctime, optr, flags);
	break;

    case ST_BLKSIZE:
	statulprint((unsigned long)sbuf->st_blksize, optr);
	break;

    case ST_BLOCKS:
	statulprint((unsigned long)sbuf->st_blocks, optr);
	break;

    case ST_READLINK:
	statlinkprint(sbuf, optr, fname);
	break;

    case ST_COUNT:			/* keep some compilers happy */
	break;
    }
}


/*
 *
 * Options:
 *  -f fd:   stat fd instead of file
 *  -g:   use GMT rather than local time for time strings (forces -s on).
 *  -n:   always print file name of file being statted
 *  -N:   never print file name
 *  -l:   list stat types
 *  -L:   do lstat (else links are implicitly dereferenced by stat)
 *  -t:   always print name of stat type
 *  -T:   never print name of stat type
 *  -r:   print raw alongside string data
 *  -s:   string, print mode, times, uid, gid as appropriate strings:
 *        harmless but unnecessary when combined with -r.
 *  -A array:  assign results to given array, one stat result per element.
 *        File names and type names are only added if explicitly requested:
 *        file names are returned as a separate array element, type names as
 *        prefix to element.  Note the formatting deliberately contains
 *        fewer frills when -A is used.
 *  -H hash:  as for -A array, but returns a hash with the keys being those
 *        from stat -l
 *  -F fmt: specify a $TIME-like format for printing times; the default
 *        is the (CTIME-like) "%a %b %e %k:%M:%S".  This option implies
 *        -s as it is not useful for numerical times.
 *
 *  +type selects just element type of stat buffer (-l gives list):
 *        type can be shortened to unambiguous string.  only one type is
 *        allowed.  The extra type, +link, reads the target of a symbolic
 *        link; it is empty if the stat was not an lstat or if 
 *        a file descriptor was stat'd, if the stat'd file is
 *        not a symbolic link, or if symbolic links are not
 *        supported.  If +link is explicitly requested, the -L (lstat)
 *        option is set automatically.
 */
/**/
static int
bin_stat(char *name, char **args, char *ops, int func)
{
    char **aptr, *arrnam = NULL, **array = NULL, **arrptr = NULL;
    char *hashnam = NULL, **hash = NULL, **hashptr = NULL;
    int len, iwhich = -1, ret = 0, flags = 0, arrsize = 0, fd = 0;
    struct stat statbuf;
    int found = 0, nargs;

    timefmt = "%a %b %e %k:%M:%S";

    for (; *args && (**args == '+' || **args == '-'); args++) {
	char *arg = *args+1;
	if (!*arg || *arg == '-' || *arg == '+') {
	    args++;
	    break;
	}

	if (**args == '+') {
	    if (found)
		break;
	    len = strlen(arg);
	    for (aptr = statelts; *aptr; aptr++)
		if (!strncmp(*aptr, arg, len)) {
		    found++;
		    iwhich = aptr - statelts;
		}
	    if (found > 1) {
		zwarnnam(name, "%s: ambiguous stat element", arg, 0);
		return 1;
	    } else if (found == 0) {
		zwarnnam(name, "%s: no such stat element", arg, 0);
		return 1;
	    }
	    /* if name of link requested, turn on lstat */
	    if (iwhich == ST_READLINK)
		ops['L'] = 1;
	    flags |= STF_PICK;
	} else {
	    for (; *arg; arg++) {
		if (strchr("glLnNrstT", *arg))
		    ops[STOUC(*arg)] = 1;
		else if (*arg == 'A') {
		    if (arg[1]) {
			arrnam = arg+1;
		    } else if (!(arrnam = *++args)) {
			zwarnnam(name, "missing parameter name",
				NULL, 0);
			return 1;
		    }
		    flags |= STF_ARRAY;
		    break;
		} else if (*arg == 'H') {
		    if (arg[1]) {
			hashnam = arg+1;
		    } else if (!(hashnam = *++args)) {
			zwarnnam(name, "missing parameter name",
				NULL, 0);
			return 1;
		    }
		    flags |= STF_HASH;
		    break;
		} else if (*arg == 'f') {
		    char *sfd;
		    ops['f'] = 1;
		    if (arg[1]) {
			sfd = arg+1;
		    } else if (!(sfd = *++args)) {
			zwarnnam(name, "missing file descriptor", NULL, 0);
			return 1;
		    }
		    fd = zstrtol(sfd, &sfd, 10);
		    if (*sfd) {
			zwarnnam(name, "bad file descriptor", NULL, 0);
			return 1;
		    }
		    break;
		} else if (*arg == 'F') {
		    if (arg[1]) {
			timefmt = arg+1;
		    } else if (!(timefmt = *++args)) {
			zwarnnam(name, "missing time format", NULL, 0);
			return 1;
		    }
		    /* force string format in order to use time format */
		    ops['s'] = 1;
		    break;
		} else {
		    zwarnnam(name, "bad option: -%c", NULL, *arg);
		    return 1;
		}
	    }
	}
    }

    if ((flags & STF_ARRAY) && (flags & STF_HASH)) {
    	/* We don't implement setting multiple variables at once */
	zwarnnam(name, "both array and hash requested", NULL, 0);
	return 1;
	/* Alternate method would be to make -H blank arrnam etc etc *
	 * and so get 'silent loss' of earlier choice, which would   *
	 * be similar to stat -A foo -A bar filename                 */
    }

    if (ops['l']) {
	/* list types and return:  can also list to array */
	if (arrnam) {
	    arrptr = array = (char **)zalloc((ST_COUNT+1)*sizeof(char *));
	    array[ST_COUNT] = NULL;
	}
	for (aptr = statelts; *aptr; aptr++) {
	    if (arrnam) {
		*arrptr++ = ztrdup(*aptr);
	    } else {
		printf("%s", *aptr);
		if (aptr[1])
		    putchar(' ');
	    }
	}
	if (arrnam) {
	    setaparam(arrnam, array);
	    if (errflag)
		return 1;
	} else
	    putchar('\n');
	return 0;
    }

    if (!*args && !ops['f']) {
	zwarnnam(name, "no files given", NULL, 0);
	return 1;
    } else if (*args && ops['f']) {
	zwarnnam(name, "no files allowed with -f", NULL, 0);
	return 1;
    }

    nargs = 0;
    if (ops['f'])
	nargs = 1;
    else
	for (aptr = args; *aptr; aptr++)
	    nargs++;

    if (ops['g']) {
	flags |= STF_GMT;
	ops['s'] = 1;
    }
    if (ops['s'] || ops['r'])
	flags |= STF_STRING;
    if (ops['r'] || !ops['s'])
	flags |= STF_RAW;
    if (ops['n'])
	flags |= STF_FILE;
    if (ops['t'])
	flags |= STF_NAME;

    if (!(arrnam || hashnam)) {
	if (nargs > 1)
	    flags |= STF_FILE;
	if (!(flags & STF_PICK))
	    flags |= STF_NAME;
    }

    if (ops['N'] || ops['f'])
	flags &= ~STF_FILE;
    if (ops['T'] || ops['H'])
	flags &= ~STF_NAME;

    if (hashnam) {
    	if (nargs > 1) {
	    zwarnnam(name, "only one file allowed with -H", NULL, 0);
	    return 1;
	}
	arrsize = (flags & STF_PICK) ? 1 : ST_COUNT;
	if (flags & STF_FILE)
	    arrsize++;
	hashptr = hash = (char **)zcalloc((arrsize+1)*2*sizeof(char *));
    }

    if (arrnam) {
	arrsize = (flags & STF_PICK) ? 1 : ST_COUNT;
	if (flags & STF_FILE)
	    arrsize++;
	arrsize *= nargs;
	arrptr = array = (char **)zcalloc((arrsize+1)*sizeof(char *));
    }

    for (; ops['f'] || *args; args++) {
	char outbuf[PATH_MAX + 9]; /* "link   " + link name + NULL */
	int rval = ops['f'] ? fstat(fd, &statbuf) :
	    ops['L'] ? lstat(*args, &statbuf) : stat(*args, &statbuf);
	if (rval) {
	    if (ops['f'])
		sprintf(outbuf, "%d", fd);
	    zwarnnam(name, "%s: %e", ops['f'] ? outbuf : *args, errno);
	    ret = 1;
	    if (ops['f'] || arrnam)
		break;
	    else
		continue;
	}

	if (flags & STF_FILE) {
	    if (arrnam)
		*arrptr++ = ztrdup(*args);
	    else if (hashnam) {
	    	*hashptr++ = ztrdup(HNAMEKEY);
		*hashptr++ = ztrdup(*args);
	    } else
		printf("%s%s", *args, (flags & STF_PICK) ? " " : ":\n");
	}
	if (iwhich > -1) {
	    statprint(&statbuf, outbuf, *args, iwhich, flags);
	    if (arrnam)
		*arrptr++ = ztrdup(outbuf);
	    else if (hashnam) {
		/* STF_NAME explicitly turned off for ops['H'] above */
	    	*hashptr++ = ztrdup(statelts[iwhich]);
		*hashptr++ = ztrdup(outbuf);
	    } else
		printf("%s\n", outbuf);
	} else {
	    int i;
	    for (i = 0; i < ST_COUNT; i++) {
		statprint(&statbuf, outbuf, *args, i, flags);
		if (arrnam)
		    *arrptr++= ztrdup(outbuf);
		else if (hashnam) {
		    /* STF_NAME explicitly turned off for ops['H'] above */
		    *hashptr++ = ztrdup(statelts[i]);
		    *hashptr++ = ztrdup(outbuf);
		} else
		    printf("%s\n", outbuf);
	    }
	}
	if (ops['f'])
	    break;

	if (!arrnam && !hashnam && args[1] && !(flags & STF_PICK))
	    putchar('\n');
    }

    if (arrnam) {
	if (ret)
	    freearray(array);
	else {
	    setaparam(arrnam, array);
	    if (errflag)
		return 1;
	}
    }

    if (hashnam) {
    	if (ret)
	    freearray(hash);
	else {
	    sethparam(hashnam, hash);
	    if (errflag)
		return 1;
	}
    }

    return ret;
}

static struct builtin bintab[] = {
    BUILTIN("stat", 0, bin_stat, 0, -1, 0, NULL, NULL),
};

/**/
int
setup_(Module m)
{
    return 0;
}

/**/
int
boot_(Module m)
{
    return !addbuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
}

/**/
int
cleanup_(Module m)
{
    deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
    return 0;
}

/**/
int
finish_(Module m)
{
    return 0;
}