2 * finfo - print file info
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);
146 builtin_error("%s: cannot stat: %s", f, strerror(errno));
147 return ((struct stat *)0);
159 return (st ? printst(st) : 1);
166 return (m & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID));
173 char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
204 ubits[2] = (m & S_IXUSR) ? 's' : 'S';
206 gbits[2] = (m & S_IXGRP) ? 's' : 'S';
208 obits[2] = (m & S_IXOTH) ? 't' : 'T';
210 printf ("u=%s,g=%s,o=%s", ubits, gbits, obits);
235 perms(getperm(mode));
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));
284 /* Print requested info */
285 if (flags & OPT_ATIME) {
286 if (flags & OPT_ASCII)
287 printf("%s", ctime(&st->st_atime));
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));
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));
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) {
311 p = readlink(f, b, 4096);
312 if (p >= 0 && p < 4096)
318 strcat(b, strerror(p));
325 } else if (flags & OPT_PERM) {
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");
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");
343 printf("%d\n", st->st_gid);
344 } else if (flags & OPT_SIZE)
345 printf("%ld\n", st->st_size);
358 v = make_builtin_argv (list, &c);
359 r = finfo_main (c, v);
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:",
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",
380 " -f composite file identifier (device:inode)",
382 " -G group name of owner",
383 " -l name of file pointed to by symlink",
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",
390 " -U user name of owner",
394 struct builtin finfo_struct = {
399 "finfo [-acdgiflmnopsuACGMPU] file [file...]",
405 #if defined (PREFER_STDARG)
408 # if defined (PREFER_VARARGS)
409 # include <varargs.h>
413 char *this_command_name;
419 this_command_name = argv[0];
420 exit(finfo_main(argc, argv));
426 fprintf(stderr, "%s: usage: %s [-%s] [file ...]\n", prog, OPTIONS);
429 #ifndef HAVE_STRERROR
434 static char ebuf[40];
436 extern char *sys_errlist[];
438 if (e < 0 || e > sys_nerr) {
439 sprintf(ebuf,"Unknown error code %d", e);
442 return (sys_errlist[e]);
451 extern char *malloc();
456 fprintf(stderr, "%s: cannot malloc %d bytes\n", prog, s);
466 if (t = strrchr(p, '/'))
472 legal_number (string, result)
485 /* Skip leading whitespace characters. */
486 while (whitespace (*string))
492 /* We allow leading `-' or `+'. */
493 if (*string == '-' || *string == '+')
495 if (!digit (string[1]))
504 while (digit (*string))
507 value = (value * 10) + digit_value (*string);
511 /* Skip trailing whitespace, if any. */
512 while (whitespace (*string))
515 /* Error if not at end of string. */
520 *result = value * sign;
545 #if defined (USE_VARARGS)
547 #if defined (PREFER_STDARG)
548 builtin_error (const char *format, ...)
550 builtin_error (format, va_alist)
557 if (this_command_name && *this_command_name)
558 fprintf (stderr, "%s: ", this_command_name);
560 #if defined (PREFER_STDARG)
561 va_start (args, format);
566 vfprintf (stderr, format, args);
568 fprintf (stderr, "\n");
572 builtin_error (format, arg1, arg2, arg3, arg4, arg5)
573 char *format, *arg1, *arg2, *arg3, *arg4, *arg5;
575 if (this_command_name && *this_command_name)
576 fprintf (stderr, "%s: ", this_command_name);
578 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
579 fprintf (stderr, "\n");
582 #endif /* !USE_VARARGS */