2 * finfo - print file info
20 extern char *strrchr();
21 extern char **make_builtin_argv ();
24 static int printsome();
25 static int printfinfo();
26 static int finfo_main();
29 extern char *sh_optarg;
30 extern char *this_command_name;
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
54 #define OPT_ASCII 0x1000000
56 #define OPTIONS "acdgiflmnopsuACGMP:U"
65 while (*++s >= '0' && *s <= '7')
66 r = (r * 8) + (*s - '0');
71 finfo_main(argc, argv)
78 sh_optind = 0; /* XXX */
79 prog = base_pathname(argv[0]);
85 while ((opt = sh_getopt(argc, argv, OPTIONS)) != EOF) {
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);
118 for (i = 0; i < argc; i++)
119 opt = flags ? printsome (argv[i], flags) : printfinfo(argv[i]);
128 static struct stat st;
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);
142 builtin_error("%s: cannot stat: %s", f, strerror(errno));
143 return ((struct stat *)0);
155 return (st ? printst(st) : 1);
162 return (m & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID));
169 char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
199 printf ("u=%s,g=%s,o=%s", ubits, gbits, obits);
220 perms(getperm(mode));
232 printf("Device (major/minor): %d (%d/%d)\n", (int) (st->st_dev & 0xFF),
233 (int) major (st->st_dev),
234 (int) minor (st->st_dev));
235 printf("Inode: %d\n", (int) st->st_ino);
236 printf("Mode: (%o) ", (int) st->st_mode);
237 printmode((int) st->st_mode);
238 printf("Link count: %d\n", (int) st->st_nlink);
239 pw = getpwuid(st->st_uid);
240 owner = pw ? pw->pw_name : "unknown";
241 printf("Uid of owner: %d (%s)\n", (int) st->st_uid, owner);
242 gr = getgrgid(st->st_gid);
243 owner = gr ? gr->gr_name : "unknown";
244 printf("Gid of owner: %d (%s)\n", (int) st->st_gid, owner);
245 printf("Device type: %d\n", (int) st->st_rdev);
246 printf("File size: %ld\n", (long) st->st_size);
247 printf("File last access time: %s", ctime (&st->st_atime));
248 printf("File last modify time: %s", ctime (&st->st_mtime));
249 printf("File last status change time: %s", ctime (&st->st_ctime));
269 /* Print requested info */
270 if (flags & OPT_ATIME) {
271 if (flags & OPT_ASCII)
272 printf("%s", ctime(&st->st_atime));
274 printf("%ld\n", st->st_atime);
275 } else if (flags & OPT_MTIME) {
276 if (flags & OPT_ASCII)
277 printf("%s", ctime(&st->st_mtime));
279 printf("%ld\n", st->st_mtime);
280 } else if (flags & OPT_CTIME) {
281 if (flags & OPT_ASCII)
282 printf("%s", ctime(&st->st_ctime));
284 printf("%ld\n", st->st_ctime);
285 } else if (flags & OPT_DEV)
286 printf("%d\n", st->st_dev);
287 else if (flags & OPT_INO)
288 printf("%d\n", st->st_ino);
289 else if (flags & OPT_FID)
290 printf("%d:%ld\n", st->st_dev, st->st_ino);
291 else if (flags & OPT_NLINK)
292 printf("%d\n", st->st_nlink);
293 else if (flags & OPT_LNKNAM) {
296 p = readlink(f, b, 4096);
297 if (p >= 0 && p < 4096)
303 strcat(b, strerror(p));
310 } else if (flags & OPT_PERM) {
313 } else if (flags & OPT_OPERM)
314 printf("%o\n", getperm(st->st_mode));
315 else if (flags & OPT_PMASK)
316 printf("%o\n", getperm(st->st_mode) & pmask);
317 else if (flags & OPT_UID) {
318 pw = getpwuid(st->st_uid);
319 if (flags & OPT_ASCII)
320 printf("%s\n", pw ? pw->pw_name : "unknown");
322 printf("%d\n", st->st_uid);
323 } else if (flags & OPT_GID) {
324 gr = getgrgid(st->st_gid);
325 if (flags & OPT_ASCII)
326 printf("%s\n", gr ? gr->gr_name : "unknown");
328 printf("%d\n", st->st_gid);
329 } else if (flags & OPT_SIZE)
330 printf("%ld\n", st->st_size);
343 v = make_builtin_argv (list, &c);
344 r = finfo_main (c, v);
350 static char *finfo_doc[] = {
351 "Display information about each FILE. Only single operators should",
352 "be supplied. If no options are supplied, a summary of the info",
353 "available about each FILE is printed. If FILE is of the form",
354 "/dev/fd/XX, file descriptor XX is described. Operators, if supplied,",
355 "have the following meanings:",
357 " -a last file access time",
358 " -A last file access time in ctime format",
359 " -c last file status change time",
360 " -C last file status change time in ctime format",
361 " -m last file modification time",
362 " -M last file modification time in ctime format",
365 " -f composite file identifier (device:inode)",
367 " -G group name of owner",
368 " -l name of file pointed to by symlink",
370 " -o permissions in octal",
371 " -p permissions in ascii",
372 " -P mask permissions ANDed with MASK (like with umask)",
373 " -s file size in bytes",
375 " -U user name of owner",
379 struct builtin finfo_struct = {
384 "finfo [-acdgiflmnopsuACGMPU] file [file...]",
390 #if defined (PREFER_STDARG)
393 # if defined (PREFER_VARARGS)
394 # include <varargs.h>
398 char *this_command_name;
404 this_command_name = argv[0];
405 exit(finfo_main(argc, argv));
411 fprintf(stderr, "%s: usage: %s [-%s] [file ...]\n", prog, OPTIONS);
414 #ifndef HAVE_STRERROR
419 static char ebuf[40];
421 extern char *sys_errlist[];
423 if (e < 0 || e > sys_nerr) {
424 sprintf(ebuf,"Unknown error code %d", e);
427 return (sys_errlist[e]);
436 extern char *malloc();
441 fprintf(stderr, "%s: cannot malloc %d bytes\n", prog, s);
451 if (t = strrchr(p, '/'))
457 legal_number (string, result)
470 /* Skip leading whitespace characters. */
471 while (whitespace (*string))
477 /* We allow leading `-' or `+'. */
478 if (*string == '-' || *string == '+')
480 if (!digit (string[1]))
489 while (digit (*string))
492 value = (value * 10) + digit_value (*string);
496 /* Skip trailing whitespace, if any. */
497 while (whitespace (*string))
500 /* Error if not at end of string. */
505 *result = value * sign;
530 #if defined (USE_VARARGS)
532 #if defined (PREFER_STDARG)
533 builtin_error (const char *format, ...)
535 builtin_error (format, va_alist)
542 if (this_command_name && *this_command_name)
543 fprintf (stderr, "%s: ", this_command_name);
545 #if defined (PREFER_STDARG)
546 va_start (args, format);
551 vfprintf (stderr, format, args);
553 fprintf (stderr, "\n");
557 builtin_error (format, arg1, arg2, arg3, arg4, arg5)
558 char *format, *arg1, *arg2, *arg3, *arg4, *arg5;
560 if (this_command_name && *this_command_name)
561 fprintf (stderr, "%s: ", this_command_name);
563 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
564 fprintf (stderr, "\n");
567 #endif /* !USE_VARARGS */