2 * finfo - print file info
9 Copyright (C) 1999-2009 Free Software Foundation, Inc.
11 This file is part of GNU Bash.
12 Bash is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 Bash is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with Bash. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
31 #include "posixstat.h"
36 #include "posixtime.h"
47 extern char **make_builtin_argv ();
50 static int printsome();
51 static int printfinfo();
52 static int finfo_main();
55 extern char *sh_optarg;
56 extern char *this_command_name;
61 #define OPT_UID 0x00001
62 #define OPT_GID 0x00002
63 #define OPT_DEV 0x00004
64 #define OPT_INO 0x00008
65 #define OPT_PERM 0x00010
66 #define OPT_LNKNAM 0x00020
67 #define OPT_FID 0x00040
68 #define OPT_NLINK 0x00080
69 #define OPT_RDEV 0x00100
70 #define OPT_SIZE 0x00200
71 #define OPT_ATIME 0x00400
72 #define OPT_MTIME 0x00800
73 #define OPT_CTIME 0x01000
74 #define OPT_BLKSIZE 0x02000
75 #define OPT_BLKS 0x04000
76 #define OPT_FTYPE 0x08000
77 #define OPT_PMASK 0x10000
78 #define OPT_OPERM 0x20000
80 #define OPT_ASCII 0x1000000
82 #define OPTIONS "acdgiflmnopsuACGMP:U"
91 while (*++s >= '0' && *s <= '7')
92 r = (r * 8) + (*s - '0');
97 finfo_main(argc, argv)
102 int mode, flags, opt;
104 sh_optind = 0; /* XXX */
105 prog = base_pathname(argv[0]);
111 while ((opt = sh_getopt(argc, argv, OPTIONS)) != EOF) {
113 case 'a': flags |= OPT_ATIME; break;
114 case 'A': flags |= OPT_ATIME|OPT_ASCII; break;
115 case 'c': flags |= OPT_CTIME; break;
116 case 'C': flags |= OPT_CTIME|OPT_ASCII; break;
117 case 'd': flags |= OPT_DEV; break;
118 case 'i': flags |= OPT_INO; break;
119 case 'f': flags |= OPT_FID; break;
120 case 'g': flags |= OPT_GID; break;
121 case 'G': flags |= OPT_GID|OPT_ASCII; break;
122 case 'l': flags |= OPT_LNKNAM; break;
123 case 'm': flags |= OPT_MTIME; break;
124 case 'M': flags |= OPT_MTIME|OPT_ASCII; break;
125 case 'n': flags |= OPT_NLINK; break;
126 case 'o': flags |= OPT_OPERM; break;
127 case 'p': flags |= OPT_PERM; break;
128 case 'P': flags |= OPT_PMASK; pmask = octal(sh_optarg); break;
129 case 's': flags |= OPT_SIZE; break;
130 case 'u': flags |= OPT_UID; break;
131 case 'U': flags |= OPT_UID|OPT_ASCII; break;
132 default: builtin_usage (); return(1);
144 for (i = 0; i < argc; i++)
145 opt = flags ? printsome (argv[i], flags) : printfinfo(argv[i]);
154 static struct stat st;
158 if (strncmp(f, "/dev/fd/", 8) == 0) {
159 if ((legal_number(f + 8, &lfd) == 0) || (int)lfd != lfd) {
160 builtin_error("%s: invalid fd", f + 8);
161 return ((struct stat *)0);
172 builtin_error("%s: cannot stat: %s", f, strerror(errno));
173 return ((struct stat *)0);
185 return (st ? printst(st) : 1);
192 return (m & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID));
199 char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
230 ubits[2] = (m & S_IXUSR) ? 's' : 'S';
232 gbits[2] = (m & S_IXGRP) ? 's' : 'S';
234 obits[2] = (m & S_IXOTH) ? 't' : 'T';
236 printf ("u=%s,g=%s,o=%s", ubits, gbits, obits);
261 perms(getperm(mode));
274 ma = major (st->st_rdev);
275 mi = minor (st->st_rdev);
276 #if defined (makedev)
277 d = makedev (ma, mi);
279 d = st->st_rdev & 0xFF;
281 printf("Device (major/minor): %d (%d/%d)\n", d, ma, mi);
283 printf("Inode: %d\n", (int) st->st_ino);
284 printf("Mode: (%o) ", (int) st->st_mode);
285 printmode((int) st->st_mode);
286 printf("Link count: %d\n", (int) st->st_nlink);
287 pw = getpwuid(st->st_uid);
288 owner = pw ? pw->pw_name : "unknown";
289 printf("Uid of owner: %d (%s)\n", (int) st->st_uid, owner);
290 gr = getgrgid(st->st_gid);
291 owner = gr ? gr->gr_name : "unknown";
292 printf("Gid of owner: %d (%s)\n", (int) st->st_gid, owner);
293 printf("Device type: %d\n", (int) st->st_rdev);
294 printf("File size: %ld\n", (long) st->st_size);
295 printf("File last access time: %s", ctime (&st->st_atime));
296 printf("File last modify time: %s", ctime (&st->st_mtime));
297 printf("File last status change time: %s", ctime (&st->st_ctime));
317 /* Print requested info */
318 if (flags & OPT_ATIME) {
319 if (flags & OPT_ASCII)
320 printf("%s", ctime(&st->st_atime));
322 printf("%ld\n", st->st_atime);
323 } else if (flags & OPT_MTIME) {
324 if (flags & OPT_ASCII)
325 printf("%s", ctime(&st->st_mtime));
327 printf("%ld\n", st->st_mtime);
328 } else if (flags & OPT_CTIME) {
329 if (flags & OPT_ASCII)
330 printf("%s", ctime(&st->st_ctime));
332 printf("%ld\n", st->st_ctime);
333 } else if (flags & OPT_DEV)
334 printf("%d\n", st->st_dev);
335 else if (flags & OPT_INO)
336 printf("%d\n", st->st_ino);
337 else if (flags & OPT_FID)
338 printf("%d:%ld\n", st->st_dev, st->st_ino);
339 else if (flags & OPT_NLINK)
340 printf("%d\n", st->st_nlink);
341 else if (flags & OPT_LNKNAM) {
344 p = readlink(f, b, 4096);
345 if (p >= 0 && p < 4096)
351 strcat(b, strerror(p));
358 } else if (flags & OPT_PERM) {
361 } else if (flags & OPT_OPERM)
362 printf("%o\n", getperm(st->st_mode));
363 else if (flags & OPT_PMASK)
364 printf("%o\n", getperm(st->st_mode) & pmask);
365 else if (flags & OPT_UID) {
366 pw = getpwuid(st->st_uid);
367 if (flags & OPT_ASCII)
368 printf("%s\n", pw ? pw->pw_name : "unknown");
370 printf("%d\n", st->st_uid);
371 } else if (flags & OPT_GID) {
372 gr = getgrgid(st->st_gid);
373 if (flags & OPT_ASCII)
374 printf("%s\n", gr ? gr->gr_name : "unknown");
376 printf("%d\n", st->st_gid);
377 } else if (flags & OPT_SIZE)
378 printf("%ld\n", (long) st->st_size);
392 v = make_builtin_argv (list, &c);
393 r = finfo_main (c, v);
399 static char *finfo_doc[] = {
400 "Display information about file attributes.",
402 "Display information about each FILE. Only single operators should",
403 "be supplied. If no options are supplied, a summary of the info",
404 "available about each FILE is printed. If FILE is of the form",
405 "/dev/fd/XX, file descriptor XX is described. Operators, if supplied,",
406 "have the following meanings:",
408 " -a last file access time",
409 " -A last file access time in ctime format",
410 " -c last file status change time",
411 " -C last file status change time in ctime format",
412 " -m last file modification time",
413 " -M last file modification time in ctime format",
416 " -f composite file identifier (device:inode)",
418 " -G group name of owner",
419 " -l name of file pointed to by symlink",
421 " -o permissions in octal",
422 " -p permissions in ascii",
423 " -P mask permissions ANDed with MASK (like with umask)",
424 " -s file size in bytes",
426 " -U user name of owner",
430 struct builtin finfo_struct = {
435 "finfo [-acdgiflmnopsuACGMPU] file [file...]",
441 #if defined (PREFER_STDARG)
444 # if defined (PREFER_VARARGS)
445 # include <varargs.h>
449 char *this_command_name;
455 this_command_name = argv[0];
456 exit(finfo_main(argc, argv));
462 fprintf(stderr, "%s: usage: %s [-%s] [file ...]\n", prog, OPTIONS);
465 #ifndef HAVE_STRERROR
470 static char ebuf[40];
472 extern char *sys_errlist[];
474 if (e < 0 || e > sys_nerr) {
475 sprintf(ebuf,"Unknown error code %d", e);
478 return (sys_errlist[e]);
487 extern char *malloc();
492 fprintf(stderr, "%s: cannot malloc %d bytes\n", prog, s);
502 if (t = strrchr(p, '/'))
508 legal_number (string, result)
521 /* Skip leading whitespace characters. */
522 while (whitespace (*string))
528 /* We allow leading `-' or `+'. */
529 if (*string == '-' || *string == '+')
531 if (!digit (string[1]))
540 while (digit (*string))
543 value = (value * 10) + digit_value (*string);
547 /* Skip trailing whitespace, if any. */
548 while (whitespace (*string))
551 /* Error if not at end of string. */
556 *result = value * sign;
581 #if defined (USE_VARARGS)
583 #if defined (PREFER_STDARG)
584 builtin_error (const char *format, ...)
586 builtin_error (format, va_alist)
593 if (this_command_name && *this_command_name)
594 fprintf (stderr, "%s: ", this_command_name);
596 #if defined (PREFER_STDARG)
597 va_start (args, format);
602 vfprintf (stderr, format, args);
604 fprintf (stderr, "\n");
608 builtin_error (format, arg1, arg2, arg3, arg4, arg5)
609 char *format, *arg1, *arg2, *arg3, *arg4, *arg5;
611 if (this_command_name && *this_command_name)
612 fprintf (stderr, "%s: ", this_command_name);
614 fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
615 fprintf (stderr, "\n");
618 #endif /* !USE_VARARGS */