2 * finfo - print file info
10 #include "posixstat.h"
25 extern char **make_builtin_argv ();
28 static int printsome();
29 static int printfinfo();
30 static int finfo_main();
33 extern char *sh_optarg;
34 extern char *this_command_name;
39 #define OPT_UID 0x00001
40 #define OPT_GID 0x00002
41 #define OPT_DEV 0x00004
42 #define OPT_INO 0x00008
43 #define OPT_PERM 0x00010
44 #define OPT_LNKNAM 0x00020
45 #define OPT_FID 0x00040
46 #define OPT_NLINK 0x00080
47 #define OPT_RDEV 0x00100
48 #define OPT_SIZE 0x00200
49 #define OPT_ATIME 0x00400
50 #define OPT_MTIME 0x00800
51 #define OPT_CTIME 0x01000
52 #define OPT_BLKSIZE 0x02000
53 #define OPT_BLKS 0x04000
54 #define OPT_FTYPE 0x08000
55 #define OPT_PMASK 0x10000
56 #define OPT_OPERM 0x20000
58 #define OPT_ASCII 0x1000000
60 #define OPTIONS "acdgiflmnopsuACGMP:U"
69 while (*++s >= '0' && *s <= '7')
70 r = (r * 8) + (*s - '0');
75 finfo_main(argc, argv)
82 sh_optind = 0; /* XXX */
83 prog = base_pathname(argv[0]);
89 while ((opt = sh_getopt(argc, argv, OPTIONS)) != EOF) {
91 case 'a': flags |= OPT_ATIME; break;
92 case 'A': flags |= OPT_ATIME|OPT_ASCII; break;
93 case 'c': flags |= OPT_CTIME; break;
94 case 'C': flags |= OPT_CTIME|OPT_ASCII; break;
95 case 'd': flags |= OPT_DEV; break;
96 case 'i': flags |= OPT_INO; break;
97 case 'f': flags |= OPT_FID; break;
98 case 'g': flags |= OPT_GID; break;
99 case 'G': flags |= OPT_GID|OPT_ASCII; break;
100 case 'l': flags |= OPT_LNKNAM; break;
101 case 'm': flags |= OPT_MTIME; break;
102 case 'M': flags |= OPT_MTIME|OPT_ASCII; break;
103 case 'n': flags |= OPT_NLINK; break;
104 case 'o': flags |= OPT_OPERM; break;
105 case 'p': flags |= OPT_PERM; break;
106 case 'P': flags |= OPT_PMASK; pmask = octal(sh_optarg); break;
107 case 's': flags |= OPT_SIZE; break;
108 case 'u': flags |= OPT_UID; break;
109 case 'U': flags |= OPT_UID|OPT_ASCII; break;
110 default: builtin_usage (); return(1);
122 for (i = 0; i < argc; i++)
123 opt = flags ? printsome (argv[i], flags) : printfinfo(argv[i]);
132 static struct stat st;
136 if (strncmp(f, "/dev/fd/", 8) == 0) {
137 if ((legal_number(f + 8, &lfd) == 0) || (int)lfd != lfd) {
138 builtin_error("%s: invalid fd", f + 8);
139 return ((struct stat *)0);
150 builtin_error("%s: cannot stat: %s", f, strerror(errno));
151 return ((struct stat *)0);
163 return (st ? printst(st) : 1);
170 return (m & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID));
177 char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
208 ubits[2] = (m & S_IXUSR) ? 's' : 'S';
210 gbits[2] = (m & S_IXGRP) ? 's' : 'S';
212 obits[2] = (m & S_IXOTH) ? 't' : 'T';
214 printf ("u=%s,g=%s,o=%s", ubits, gbits, obits);
239 perms(getperm(mode));
251 printf("Device (major/minor): %d (%d/%d)\n", (int) (st->st_dev & 0xFF),
252 (int) major (st->st_dev),
253 (int) minor (st->st_dev));
254 printf("Inode: %d\n", (int) st->st_ino);
255 printf("Mode: (%o) ", (int) st->st_mode);
256 printmode((int) st->st_mode);
257 printf("Link count: %d\n", (int) st->st_nlink);
258 pw = getpwuid(st->st_uid);
259 owner = pw ? pw->pw_name : "unknown";
260 printf("Uid of owner: %d (%s)\n", (int) st->st_uid, owner);
261 gr = getgrgid(st->st_gid);
262 owner = gr ? gr->gr_name : "unknown";
263 printf("Gid of owner: %d (%s)\n", (int) st->st_gid, owner);
264 printf("Device type: %d\n", (int) st->st_rdev);
265 printf("File size: %ld\n", (long) st->st_size);
266 printf("File last access time: %s", ctime (&st->st_atime));
267 printf("File last modify time: %s", ctime (&st->st_mtime));
268 printf("File last status change time: %s", ctime (&st->st_ctime));
288 /* Print requested info */
289 if (flags & OPT_ATIME) {
290 if (flags & OPT_ASCII)
291 printf("%s", ctime(&st->st_atime));
293 printf("%ld\n", st->st_atime);
294 } else if (flags & OPT_MTIME) {
295 if (flags & OPT_ASCII)
296 printf("%s", ctime(&st->st_mtime));
298 printf("%ld\n", st->st_mtime);
299 } else if (flags & OPT_CTIME) {
300 if (flags & OPT_ASCII)
301 printf("%s", ctime(&st->st_ctime));
303 printf("%ld\n", st->st_ctime);
304 } else if (flags & OPT_DEV)
305 printf("%d\n", st->st_dev);
306 else if (flags & OPT_INO)
307 printf("%d\n", st->st_ino);
308 else if (flags & OPT_FID)
309 printf("%d:%ld\n", st->st_dev, st->st_ino);
310 else if (flags & OPT_NLINK)
311 printf("%d\n", st->st_nlink);
312 else if (flags & OPT_LNKNAM) {
315 p = readlink(f, b, 4096);
316 if (p >= 0 && p < 4096)
322 strcat(b, strerror(p));
329 } else if (flags & OPT_PERM) {
332 } else if (flags & OPT_OPERM)
333 printf("%o\n", getperm(st->st_mode));
334 else if (flags & OPT_PMASK)
335 printf("%o\n", getperm(st->st_mode) & pmask);
336 else if (flags & OPT_UID) {
337 pw = getpwuid(st->st_uid);
338 if (flags & OPT_ASCII)
339 printf("%s\n", pw ? pw->pw_name : "unknown");
341 printf("%d\n", st->st_uid);
342 } else if (flags & OPT_GID) {
343 gr = getgrgid(st->st_gid);
344 if (flags & OPT_ASCII)
345 printf("%s\n", gr ? gr->gr_name : "unknown");
347 printf("%d\n", st->st_gid);
348 } else if (flags & OPT_SIZE)
349 printf("%ld\n", st->st_size);
363 v = make_builtin_argv (list, &c);
364 r = finfo_main (c, v);
370 static char *finfo_doc[] = {
371 "Display information about each FILE. Only single operators should",
372 "be supplied. If no options are supplied, a summary of the info",
373 "available about each FILE is printed. If FILE is of the form",
374 "/dev/fd/XX, file descriptor XX is described. Operators, if supplied,",
375 "have the following meanings:",
377 " -a last file access time",
378 " -A last file access time in ctime format",
379 " -c last file status change time",
380 " -C last file status change time in ctime format",
381 " -m last file modification time",
382 " -M last file modification time in ctime format",
385 " -f composite file identifier (device:inode)",
387 " -G group name of owner",
388 " -l name of file pointed to by symlink",
390 " -o permissions in octal",
391 " -p permissions in ascii",
392 " -P mask permissions ANDed with MASK (like with umask)",
393 " -s file size in bytes",
395 " -U user name of owner",
399 struct builtin finfo_struct = {
404 "finfo [-acdgiflmnopsuACGMPU] file [file...]",
410 #if defined (PREFER_STDARG)
413 # if defined (PREFER_VARARGS)
414 # include <varargs.h>
418 char *this_command_name;
424 this_command_name = argv[0];
425 exit(finfo_main(argc, argv));
431 fprintf(stderr, "%s: usage: %s [-%s] [file ...]\n", prog, OPTIONS);
434 #ifndef HAVE_STRERROR
439 static char ebuf[40];
441 extern char *sys_errlist[];
443 if (e < 0 || e > sys_nerr) {
444 sprintf(ebuf,"Unknown error code %d", e);
447 return (sys_errlist[e]);
456 extern char *malloc();
461 fprintf(stderr, "%s: cannot malloc %d bytes\n", prog, s);
471 if (t = strrchr(p, '/'))
477 legal_number (string, result)
490 /* Skip leading whitespace characters. */
491 while (whitespace (*string))
497 /* We allow leading `-' or `+'. */
498 if (*string == '-' || *string == '+')
500 if (!digit (string[1]))
509 while (digit (*string))
512 value = (value * 10) + digit_value (*string);
516 /* Skip trailing whitespace, if any. */
517 while (whitespace (*string))
520 /* Error if not at end of string. */
525 *result = value * sign;
550 #if defined (USE_VARARGS)
552 #if defined (PREFER_STDARG)
553 builtin_error (const char *format, ...)
555 builtin_error (format, va_alist)
562 if (this_command_name && *this_command_name)
563 fprintf (stderr, "%s: ", this_command_name);
565 #if defined (PREFER_STDARG)
566 va_start (args, format);
571 vfprintf (stderr, format, args);
573 fprintf (stderr, "\n");
577 builtin_error (format, arg1, arg2, arg3, arg4, arg5)
578 char *format, *arg1, *arg2, *arg3, *arg4, *arg5;
580 if (this_command_name && *this_command_name)
581 fprintf (stderr, "%s: ", this_command_name);
583 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
584 fprintf (stderr, "\n");
587 #endif /* !USE_VARARGS */