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
));
252 ma
= major (st
->st_rdev
);
253 mi
= minor (st
->st_rdev
);
254 #if defined (makedev)
255 d
= makedev (ma
, mi
);
257 d
= st
->st_rdev
& 0xFF;
259 printf("Device (major/minor): %d (%d/%d)\n", d
, ma
, mi
);
261 printf("Inode: %d\n", (int) st
->st_ino
);
262 printf("Mode: (%o) ", (int) st
->st_mode
);
263 printmode((int) st
->st_mode
);
264 printf("Link count: %d\n", (int) st
->st_nlink
);
265 pw
= getpwuid(st
->st_uid
);
266 owner
= pw
? pw
->pw_name
: "unknown";
267 printf("Uid of owner: %d (%s)\n", (int) st
->st_uid
, owner
);
268 gr
= getgrgid(st
->st_gid
);
269 owner
= gr
? gr
->gr_name
: "unknown";
270 printf("Gid of owner: %d (%s)\n", (int) st
->st_gid
, owner
);
271 printf("Device type: %d\n", (int) st
->st_rdev
);
272 printf("File size: %ld\n", (long) st
->st_size
);
273 printf("File last access time: %s", ctime (&st
->st_atime
));
274 printf("File last modify time: %s", ctime (&st
->st_mtime
));
275 printf("File last status change time: %s", ctime (&st
->st_ctime
));
295 /* Print requested info */
296 if (flags
& OPT_ATIME
) {
297 if (flags
& OPT_ASCII
)
298 printf("%s", ctime(&st
->st_atime
));
300 printf("%ld\n", st
->st_atime
);
301 } else if (flags
& OPT_MTIME
) {
302 if (flags
& OPT_ASCII
)
303 printf("%s", ctime(&st
->st_mtime
));
305 printf("%ld\n", st
->st_mtime
);
306 } else if (flags
& OPT_CTIME
) {
307 if (flags
& OPT_ASCII
)
308 printf("%s", ctime(&st
->st_ctime
));
310 printf("%ld\n", st
->st_ctime
);
311 } else if (flags
& OPT_DEV
)
312 printf("%d\n", st
->st_dev
);
313 else if (flags
& OPT_INO
)
314 printf("%d\n", st
->st_ino
);
315 else if (flags
& OPT_FID
)
316 printf("%d:%ld\n", st
->st_dev
, st
->st_ino
);
317 else if (flags
& OPT_NLINK
)
318 printf("%d\n", st
->st_nlink
);
319 else if (flags
& OPT_LNKNAM
) {
322 p
= readlink(f
, b
, 4096);
323 if (p
>= 0 && p
< 4096)
329 strcat(b
, strerror(p
));
336 } else if (flags
& OPT_PERM
) {
339 } else if (flags
& OPT_OPERM
)
340 printf("%o\n", getperm(st
->st_mode
));
341 else if (flags
& OPT_PMASK
)
342 printf("%o\n", getperm(st
->st_mode
) & pmask
);
343 else if (flags
& OPT_UID
) {
344 pw
= getpwuid(st
->st_uid
);
345 if (flags
& OPT_ASCII
)
346 printf("%s\n", pw
? pw
->pw_name
: "unknown");
348 printf("%d\n", st
->st_uid
);
349 } else if (flags
& OPT_GID
) {
350 gr
= getgrgid(st
->st_gid
);
351 if (flags
& OPT_ASCII
)
352 printf("%s\n", gr
? gr
->gr_name
: "unknown");
354 printf("%d\n", st
->st_gid
);
355 } else if (flags
& OPT_SIZE
)
356 printf("%ld\n", st
->st_size
);
370 v
= make_builtin_argv (list
, &c
);
371 r
= finfo_main (c
, v
);
377 static char *finfo_doc
[] = {
378 "Display information about each FILE. Only single operators should",
379 "be supplied. If no options are supplied, a summary of the info",
380 "available about each FILE is printed. If FILE is of the form",
381 "/dev/fd/XX, file descriptor XX is described. Operators, if supplied,",
382 "have the following meanings:",
384 " -a last file access time",
385 " -A last file access time in ctime format",
386 " -c last file status change time",
387 " -C last file status change time in ctime format",
388 " -m last file modification time",
389 " -M last file modification time in ctime format",
392 " -f composite file identifier (device:inode)",
394 " -G group name of owner",
395 " -l name of file pointed to by symlink",
397 " -o permissions in octal",
398 " -p permissions in ascii",
399 " -P mask permissions ANDed with MASK (like with umask)",
400 " -s file size in bytes",
402 " -U user name of owner",
406 struct builtin finfo_struct
= {
411 "finfo [-acdgiflmnopsuACGMPU] file [file...]",
417 #if defined (PREFER_STDARG)
420 # if defined (PREFER_VARARGS)
421 # include <varargs.h>
425 char *this_command_name
;
431 this_command_name
= argv
[0];
432 exit(finfo_main(argc
, argv
));
438 fprintf(stderr
, "%s: usage: %s [-%s] [file ...]\n", prog
, OPTIONS
);
441 #ifndef HAVE_STRERROR
446 static char ebuf
[40];
448 extern char *sys_errlist
[];
450 if (e
< 0 || e
> sys_nerr
) {
451 sprintf(ebuf
,"Unknown error code %d", e
);
454 return (sys_errlist
[e
]);
463 extern char *malloc();
468 fprintf(stderr
, "%s: cannot malloc %d bytes\n", prog
, s
);
478 if (t
= strrchr(p
, '/'))
484 legal_number (string
, result
)
497 /* Skip leading whitespace characters. */
498 while (whitespace (*string
))
504 /* We allow leading `-' or `+'. */
505 if (*string
== '-' || *string
== '+')
507 if (!digit (string
[1]))
516 while (digit (*string
))
519 value
= (value
* 10) + digit_value (*string
);
523 /* Skip trailing whitespace, if any. */
524 while (whitespace (*string
))
527 /* Error if not at end of string. */
532 *result
= value
* sign
;
557 #if defined (USE_VARARGS)
559 #if defined (PREFER_STDARG)
560 builtin_error (const char *format
, ...)
562 builtin_error (format
, va_alist
)
569 if (this_command_name
&& *this_command_name
)
570 fprintf (stderr
, "%s: ", this_command_name
);
572 #if defined (PREFER_STDARG)
573 va_start (args
, format
);
578 vfprintf (stderr
, format
, args
);
580 fprintf (stderr
, "\n");
584 builtin_error (format
, arg1
, arg2
, arg3
, arg4
, arg5
)
585 char *format
, *arg1
, *arg2
, *arg3
, *arg4
, *arg5
;
587 if (this_command_name
&& *this_command_name
)
588 fprintf (stderr
, "%s: ", this_command_name
);
590 fprintf (stderr
, format
, arg1
, arg2
, arg3
, arg4
, arg5
);
591 fprintf (stderr
, "\n");
594 #endif /* !USE_VARARGS */