]>
Commit | Line | Data |
---|---|---|
ccc6cda3 JA |
1 | /* |
2 | * finfo - print file info | |
3 | */ | |
4 | ||
5 | #include <sys/types.h> | |
6 | #include "posixstat.h" | |
7 | #include <stdio.h> | |
8 | #include <pwd.h> | |
9 | #include <grp.h> | |
10 | #include <errno.h> | |
11 | ||
28ef6c31 | 12 | #include "bashansi.h" |
ccc6cda3 JA |
13 | #include "shell.h" |
14 | #include "builtins.h" | |
15 | #include "common.h" | |
16 | ||
17 | #ifndef errno | |
18 | extern int errno; | |
19 | #endif | |
20 | ||
ccc6cda3 JA |
21 | extern char **make_builtin_argv (); |
22 | ||
23 | static int printst(); | |
24 | static int printsome(); | |
25 | static int printfinfo(); | |
26 | static int finfo_main(); | |
27 | ||
28 | extern int sh_optind; | |
29 | extern char *sh_optarg; | |
30 | extern char *this_command_name; | |
31 | ||
32 | static char *prog; | |
33 | static int pmask; | |
34 | ||
35 | #define OPT_UID 0x00001 | |
36 | #define OPT_GID 0x00002 | |
37 | #define OPT_DEV 0x00004 | |
38 | #define OPT_INO 0x00008 | |
39 | #define OPT_PERM 0x00010 | |
40 | #define OPT_LNKNAM 0x00020 | |
41 | #define OPT_FID 0x00040 | |
42 | #define OPT_NLINK 0x00080 | |
43 | #define OPT_RDEV 0x00100 | |
44 | #define OPT_SIZE 0x00200 | |
45 | #define OPT_ATIME 0x00400 | |
46 | #define OPT_MTIME 0x00800 | |
47 | #define OPT_CTIME 0x01000 | |
48 | #define OPT_BLKSIZE 0x02000 | |
49 | #define OPT_BLKS 0x04000 | |
50 | #define OPT_FTYPE 0x08000 | |
51 | #define OPT_PMASK 0x10000 | |
52 | #define OPT_OPERM 0x20000 | |
53 | ||
54 | #define OPT_ASCII 0x1000000 | |
55 | ||
56 | #define OPTIONS "acdgiflmnopsuACGMP:U" | |
57 | ||
58 | static int | |
59 | octal(s) | |
60 | char *s; | |
61 | { | |
62 | int r; | |
63 | ||
64 | r = *s - '0'; | |
65 | while (*++s >= '0' && *s <= '7') | |
66 | r = (r * 8) + (*s - '0'); | |
67 | return r; | |
68 | } | |
69 | ||
70 | static int | |
71 | finfo_main(argc, argv) | |
72 | int argc; | |
73 | char **argv; | |
74 | { | |
75 | register int i; | |
76 | int mode, flags, opt; | |
77 | ||
78 | sh_optind = 0; /* XXX */ | |
79 | prog = base_pathname(argv[0]); | |
80 | if (argc == 1) { | |
81 | builtin_usage(); | |
82 | return(1); | |
83 | } | |
84 | flags = 0; | |
85 | while ((opt = sh_getopt(argc, argv, OPTIONS)) != EOF) { | |
86 | switch(opt) { | |
87 | case 'a': flags |= OPT_ATIME; break; | |
88 | case 'A': flags |= OPT_ATIME|OPT_ASCII; break; | |
89 | case 'c': flags |= OPT_CTIME; break; | |
90 | case 'C': flags |= OPT_CTIME|OPT_ASCII; break; | |
91 | case 'd': flags |= OPT_DEV; break; | |
92 | case 'i': flags |= OPT_INO; break; | |
93 | case 'f': flags |= OPT_FID; break; | |
94 | case 'g': flags |= OPT_GID; break; | |
95 | case 'G': flags |= OPT_GID|OPT_ASCII; break; | |
96 | case 'l': flags |= OPT_LNKNAM; break; | |
97 | case 'm': flags |= OPT_MTIME; break; | |
98 | case 'M': flags |= OPT_MTIME|OPT_ASCII; break; | |
99 | case 'n': flags |= OPT_NLINK; break; | |
100 | case 'o': flags |= OPT_OPERM; break; | |
101 | case 'p': flags |= OPT_PERM; break; | |
102 | case 'P': flags |= OPT_PMASK; pmask = octal(sh_optarg); break; | |
103 | case 's': flags |= OPT_SIZE; break; | |
104 | case 'u': flags |= OPT_UID; break; | |
105 | case 'U': flags |= OPT_UID|OPT_ASCII; break; | |
106 | default: builtin_usage (); return(1); | |
107 | } | |
108 | } | |
109 | ||
110 | argc -= sh_optind; | |
111 | argv += sh_optind; | |
112 | ||
113 | if (argc == 0) { | |
114 | builtin_usage(); | |
115 | return(1); | |
116 | } | |
117 | ||
118 | for (i = 0; i < argc; i++) | |
119 | opt = flags ? printsome (argv[i], flags) : printfinfo(argv[i]); | |
120 | ||
121 | return(opt); | |
122 | } | |
123 | ||
124 | static struct stat * | |
125 | getstat(f) | |
126 | char *f; | |
127 | { | |
128 | static struct stat st; | |
129 | int fd, r; | |
130 | long lfd; | |
131 | ||
132 | if (strncmp(f, "/dev/fd/", 8) == 0) { | |
133 | if (legal_number(f + 8, &lfd) == 0) { | |
134 | builtin_error("%s: invalid fd", f + 8); | |
135 | return ((struct stat *)0); | |
136 | } | |
137 | fd = lfd; | |
138 | r = fstat(fd, &st); | |
139 | } else | |
28ef6c31 JA |
140 | #ifdef HAVE_LSTAT |
141 | r = lstat(f, &st); | |
142 | #else | |
ccc6cda3 | 143 | r = stat(f, &st); |
28ef6c31 | 144 | #endif |
ccc6cda3 JA |
145 | if (r < 0) { |
146 | builtin_error("%s: cannot stat: %s", f, strerror(errno)); | |
147 | return ((struct stat *)0); | |
148 | } | |
149 | return (&st); | |
150 | } | |
151 | ||
152 | static int | |
153 | printfinfo(f) | |
154 | char *f; | |
155 | { | |
156 | struct stat *st; | |
157 | ||
158 | st = getstat(f); | |
159 | return (st ? printst(st) : 1); | |
160 | } | |
161 | ||
162 | static int | |
163 | getperm(m) | |
164 | int m; | |
165 | { | |
166 | return (m & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID)); | |
167 | } | |
168 | ||
169 | static int | |
170 | perms(m) | |
171 | int m; | |
172 | { | |
173 | char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */ | |
174 | int i; | |
175 | ||
176 | i = 0; | |
177 | if (m & S_IRUSR) | |
178 | ubits[i++] = 'r'; | |
179 | if (m & S_IWUSR) | |
180 | ubits[i++] = 'w'; | |
181 | if (m & S_IXUSR) | |
182 | ubits[i++] = 'x'; | |
183 | ubits[i] = '\0'; | |
184 | ||
185 | i = 0; | |
186 | if (m & S_IRGRP) | |
187 | gbits[i++] = 'r'; | |
188 | if (m & S_IWGRP) | |
189 | gbits[i++] = 'w'; | |
190 | if (m & S_IXGRP) | |
191 | gbits[i++] = 'x'; | |
192 | gbits[i] = '\0'; | |
193 | ||
194 | i = 0; | |
195 | if (m & S_IROTH) | |
196 | obits[i++] = 'r'; | |
197 | if (m & S_IWOTH) | |
198 | obits[i++] = 'w'; | |
199 | if (m & S_IXOTH) | |
200 | obits[i++] = 'x'; | |
201 | obits[i] = '\0'; | |
202 | ||
b72432fd JA |
203 | if (m & S_ISUID) |
204 | ubits[2] = (m & S_IXUSR) ? 's' : 'S'; | |
205 | if (m & S_ISGID) | |
206 | gbits[2] = (m & S_IXGRP) ? 's' : 'S'; | |
207 | if (m & S_ISVTX) | |
208 | obits[2] = (m & S_IXOTH) ? 't' : 'T'; | |
209 | ||
ccc6cda3 JA |
210 | printf ("u=%s,g=%s,o=%s", ubits, gbits, obits); |
211 | } | |
212 | ||
213 | static int | |
214 | printmode(mode) | |
215 | int mode; | |
216 | { | |
217 | if (S_ISBLK(mode)) | |
218 | printf("S_IFBLK "); | |
219 | if (S_ISCHR(mode)) | |
220 | printf("S_IFCHR "); | |
221 | if (S_ISDIR(mode)) | |
222 | printf("S_IFDIR "); | |
223 | if (S_ISREG(mode)) | |
224 | printf("S_IFREG "); | |
225 | if (S_ISFIFO(mode)) | |
226 | printf("S_IFIFO "); | |
227 | if (S_ISLNK(mode)) | |
228 | printf("S_IFLNK "); | |
229 | if (S_ISSOCK(mode)) | |
230 | printf("S_IFSOCK "); | |
b72432fd JA |
231 | #ifdef S_ISWHT |
232 | if (S_ISWHT(mode)) | |
233 | printf("S_ISWHT "); | |
234 | #endif | |
ccc6cda3 JA |
235 | perms(getperm(mode)); |
236 | printf("\n"); | |
237 | } | |
238 | ||
239 | static int | |
240 | printst(st) | |
241 | struct stat *st; | |
242 | { | |
243 | struct passwd *pw; | |
244 | struct group *gr; | |
245 | char *owner; | |
246 | ||
247 | printf("Device (major/minor): %d (%d/%d)\n", (int) (st->st_dev & 0xFF), | |
248 | (int) major (st->st_dev), | |
249 | (int) minor (st->st_dev)); | |
250 | printf("Inode: %d\n", (int) st->st_ino); | |
251 | printf("Mode: (%o) ", (int) st->st_mode); | |
252 | printmode((int) st->st_mode); | |
253 | printf("Link count: %d\n", (int) st->st_nlink); | |
254 | pw = getpwuid(st->st_uid); | |
255 | owner = pw ? pw->pw_name : "unknown"; | |
256 | printf("Uid of owner: %d (%s)\n", (int) st->st_uid, owner); | |
257 | gr = getgrgid(st->st_gid); | |
258 | owner = gr ? gr->gr_name : "unknown"; | |
259 | printf("Gid of owner: %d (%s)\n", (int) st->st_gid, owner); | |
260 | printf("Device type: %d\n", (int) st->st_rdev); | |
261 | printf("File size: %ld\n", (long) st->st_size); | |
262 | printf("File last access time: %s", ctime (&st->st_atime)); | |
263 | printf("File last modify time: %s", ctime (&st->st_mtime)); | |
264 | printf("File last status change time: %s", ctime (&st->st_ctime)); | |
265 | fflush(stdout); | |
266 | return(0); | |
267 | } | |
268 | ||
269 | static int | |
270 | printsome(f, flags) | |
271 | char *f; | |
272 | int flags; | |
273 | { | |
274 | struct stat *st; | |
275 | struct passwd *pw; | |
276 | struct group *gr; | |
277 | int p; | |
278 | char *b; | |
279 | ||
280 | st = getstat(f); | |
281 | if (st == NULL) | |
282 | return (1); | |
283 | ||
284 | /* Print requested info */ | |
285 | if (flags & OPT_ATIME) { | |
286 | if (flags & OPT_ASCII) | |
287 | printf("%s", ctime(&st->st_atime)); | |
288 | else | |
289 | printf("%ld\n", st->st_atime); | |
290 | } else if (flags & OPT_MTIME) { | |
291 | if (flags & OPT_ASCII) | |
292 | printf("%s", ctime(&st->st_mtime)); | |
293 | else | |
294 | printf("%ld\n", st->st_mtime); | |
295 | } else if (flags & OPT_CTIME) { | |
296 | if (flags & OPT_ASCII) | |
297 | printf("%s", ctime(&st->st_ctime)); | |
298 | else | |
299 | printf("%ld\n", st->st_ctime); | |
300 | } else if (flags & OPT_DEV) | |
301 | printf("%d\n", st->st_dev); | |
302 | else if (flags & OPT_INO) | |
303 | printf("%d\n", st->st_ino); | |
304 | else if (flags & OPT_FID) | |
305 | printf("%d:%ld\n", st->st_dev, st->st_ino); | |
306 | else if (flags & OPT_NLINK) | |
307 | printf("%d\n", st->st_nlink); | |
308 | else if (flags & OPT_LNKNAM) { | |
309 | #ifdef S_ISLNK | |
310 | b = xmalloc(4096); | |
311 | p = readlink(f, b, 4096); | |
312 | if (p >= 0 && p < 4096) | |
313 | b[p] = '\0'; | |
314 | else { | |
315 | p = errno; | |
316 | strcpy(b, prog); | |
317 | strcat(b, ": "); | |
318 | strcat(b, strerror(p)); | |
319 | } | |
320 | printf("%s\n", b); | |
321 | free(b); | |
322 | #else | |
323 | printf("%s\n", f); | |
324 | #endif | |
325 | } else if (flags & OPT_PERM) { | |
326 | perms(st->st_mode); | |
327 | printf("\n"); | |
328 | } else if (flags & OPT_OPERM) | |
329 | printf("%o\n", getperm(st->st_mode)); | |
330 | else if (flags & OPT_PMASK) | |
331 | printf("%o\n", getperm(st->st_mode) & pmask); | |
332 | else if (flags & OPT_UID) { | |
333 | pw = getpwuid(st->st_uid); | |
334 | if (flags & OPT_ASCII) | |
335 | printf("%s\n", pw ? pw->pw_name : "unknown"); | |
336 | else | |
337 | printf("%d\n", st->st_uid); | |
338 | } else if (flags & OPT_GID) { | |
339 | gr = getgrgid(st->st_gid); | |
340 | if (flags & OPT_ASCII) | |
341 | printf("%s\n", gr ? gr->gr_name : "unknown"); | |
342 | else | |
343 | printf("%d\n", st->st_gid); | |
344 | } else if (flags & OPT_SIZE) | |
345 | printf("%ld\n", st->st_size); | |
346 | ||
347 | return (0); | |
348 | } | |
349 | ||
350 | #ifndef NOBUILTIN | |
351 | finfo_builtin(list) | |
352 | WORD_LIST *list; | |
353 | { | |
354 | int c, r; | |
355 | char **v; | |
356 | WORD_LIST *l; | |
357 | ||
358 | v = make_builtin_argv (list, &c); | |
359 | r = finfo_main (c, v); | |
360 | free (v); | |
361 | ||
362 | return r; | |
363 | } | |
364 | ||
365 | static char *finfo_doc[] = { | |
366 | "Display information about each FILE. Only single operators should", | |
367 | "be supplied. If no options are supplied, a summary of the info", | |
368 | "available about each FILE is printed. If FILE is of the form", | |
369 | "/dev/fd/XX, file descriptor XX is described. Operators, if supplied,", | |
370 | "have the following meanings:", | |
371 | "", | |
372 | " -a last file access time", | |
373 | " -A last file access time in ctime format", | |
374 | " -c last file status change time", | |
375 | " -C last file status change time in ctime format", | |
376 | " -m last file modification time", | |
377 | " -M last file modification time in ctime format", | |
378 | " -d device", | |
379 | " -i inode", | |
380 | " -f composite file identifier (device:inode)", | |
381 | " -g gid of owner", | |
382 | " -G group name of owner", | |
383 | " -l name of file pointed to by symlink", | |
384 | " -n link count", | |
385 | " -o permissions in octal", | |
386 | " -p permissions in ascii", | |
387 | " -P mask permissions ANDed with MASK (like with umask)", | |
388 | " -s file size in bytes", | |
389 | " -u uid of owner", | |
390 | " -U user name of owner", | |
391 | (char *)0 | |
392 | }; | |
393 | ||
394 | struct builtin finfo_struct = { | |
395 | "finfo", | |
396 | finfo_builtin, | |
397 | BUILTIN_ENABLED, | |
398 | finfo_doc, | |
399 | "finfo [-acdgiflmnopsuACGMPU] file [file...]", | |
400 | 0 | |
401 | }; | |
402 | #endif | |
403 | ||
404 | #ifdef NOBUILTIN | |
405 | #if defined (PREFER_STDARG) | |
406 | # include <stdarg.h> | |
407 | #else | |
408 | # if defined (PREFER_VARARGS) | |
409 | # include <varargs.h> | |
410 | # endif | |
411 | #endif | |
412 | ||
413 | char *this_command_name; | |
414 | ||
415 | main(argc, argv) | |
416 | int argc; | |
417 | char **argv; | |
418 | { | |
419 | this_command_name = argv[0]; | |
420 | exit(finfo_main(argc, argv)); | |
421 | } | |
422 | ||
423 | void | |
424 | builtin_usage() | |
425 | { | |
426 | fprintf(stderr, "%s: usage: %s [-%s] [file ...]\n", prog, OPTIONS); | |
427 | } | |
428 | ||
429 | #ifndef HAVE_STRERROR | |
430 | char * | |
431 | strerror(e) | |
432 | int e; | |
433 | { | |
434 | static char ebuf[40]; | |
435 | extern int sys_nerr; | |
436 | extern char *sys_errlist[]; | |
437 | ||
438 | if (e < 0 || e > sys_nerr) { | |
439 | sprintf(ebuf,"Unknown error code %d", e); | |
440 | return (&ebuf[0]); | |
441 | } | |
442 | return (sys_errlist[e]); | |
443 | } | |
444 | #endif | |
445 | ||
446 | char * | |
447 | xmalloc(s) | |
448 | size_t s; | |
449 | { | |
450 | char *ret; | |
451 | extern char *malloc(); | |
452 | ||
453 | ret = malloc(s); | |
454 | if (ret) | |
455 | return (ret); | |
456 | fprintf(stderr, "%s: cannot malloc %d bytes\n", prog, s); | |
457 | exit(1); | |
458 | } | |
459 | ||
460 | char * | |
461 | base_pathname(p) | |
462 | char *p; | |
463 | { | |
464 | char *t; | |
465 | ||
466 | if (t = strrchr(p, '/')) | |
467 | return(++t); | |
468 | return(p); | |
469 | } | |
470 | ||
471 | int | |
472 | legal_number (string, result) | |
473 | char *string; | |
474 | long *result; | |
475 | { | |
476 | int sign; | |
477 | long value; | |
478 | ||
479 | sign = 1; | |
480 | value = 0; | |
481 | ||
482 | if (result) | |
483 | *result = 0; | |
484 | ||
485 | /* Skip leading whitespace characters. */ | |
486 | while (whitespace (*string)) | |
487 | string++; | |
488 | ||
489 | if (!*string) | |
490 | return (0); | |
491 | ||
492 | /* We allow leading `-' or `+'. */ | |
493 | if (*string == '-' || *string == '+') | |
494 | { | |
495 | if (!digit (string[1])) | |
496 | return (0); | |
497 | ||
498 | if (*string == '-') | |
499 | sign = -1; | |
500 | ||
501 | string++; | |
502 | } | |
503 | ||
504 | while (digit (*string)) | |
505 | { | |
506 | if (result) | |
507 | value = (value * 10) + digit_value (*string); | |
508 | string++; | |
509 | } | |
510 | ||
511 | /* Skip trailing whitespace, if any. */ | |
512 | while (whitespace (*string)) | |
513 | string++; | |
514 | ||
515 | /* Error if not at end of string. */ | |
516 | if (*string) | |
517 | return (0); | |
518 | ||
519 | if (result) | |
520 | *result = value * sign; | |
521 | ||
522 | return (1); | |
523 | } | |
524 | ||
525 | int sh_optind; | |
526 | char *sh_optarg; | |
527 | int sh_opterr; | |
528 | ||
529 | extern int optind; | |
530 | extern char *optarg; | |
531 | ||
532 | int | |
533 | sh_getopt(c, v, o) | |
534 | int c; | |
535 | char **v, *o; | |
536 | { | |
537 | int r; | |
538 | ||
539 | r = getopt(c, v, o); | |
540 | sh_optind = optind; | |
541 | sh_optarg = optarg; | |
542 | return r; | |
543 | } | |
544 | ||
545 | #if defined (USE_VARARGS) | |
546 | void | |
547 | #if defined (PREFER_STDARG) | |
548 | builtin_error (const char *format, ...) | |
549 | #else | |
550 | builtin_error (format, va_alist) | |
551 | const char *format; | |
552 | va_dcl | |
553 | #endif | |
554 | { | |
555 | va_list args; | |
556 | ||
557 | if (this_command_name && *this_command_name) | |
558 | fprintf (stderr, "%s: ", this_command_name); | |
559 | ||
560 | #if defined (PREFER_STDARG) | |
561 | va_start (args, format); | |
562 | #else | |
563 | va_start (args); | |
564 | #endif | |
565 | ||
566 | vfprintf (stderr, format, args); | |
567 | va_end (args); | |
568 | fprintf (stderr, "\n"); | |
569 | } | |
570 | #else | |
571 | void | |
572 | builtin_error (format, arg1, arg2, arg3, arg4, arg5) | |
573 | char *format, *arg1, *arg2, *arg3, *arg4, *arg5; | |
574 | { | |
575 | if (this_command_name && *this_command_name) | |
576 | fprintf (stderr, "%s: ", this_command_name); | |
577 | ||
578 | fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5); | |
579 | fprintf (stderr, "\n"); | |
580 | fflush (stderr); | |
581 | } | |
582 | #endif /* !USE_VARARGS */ | |
583 | ||
584 | #endif |