2 * finfo - print file info
12 #include <sys/types.h>
13 #include "posixstat.h"
18 #include "posixtime.h"
29 extern char **make_builtin_argv ();
32 static int printsome();
33 static int printfinfo();
34 static int finfo_main();
37 extern char *sh_optarg
;
38 extern char *this_command_name
;
43 #define OPT_UID 0x00001
44 #define OPT_GID 0x00002
45 #define OPT_DEV 0x00004
46 #define OPT_INO 0x00008
47 #define OPT_PERM 0x00010
48 #define OPT_LNKNAM 0x00020
49 #define OPT_FID 0x00040
50 #define OPT_NLINK 0x00080
51 #define OPT_RDEV 0x00100
52 #define OPT_SIZE 0x00200
53 #define OPT_ATIME 0x00400
54 #define OPT_MTIME 0x00800
55 #define OPT_CTIME 0x01000
56 #define OPT_BLKSIZE 0x02000
57 #define OPT_BLKS 0x04000
58 #define OPT_FTYPE 0x08000
59 #define OPT_PMASK 0x10000
60 #define OPT_OPERM 0x20000
62 #define OPT_ASCII 0x1000000
64 #define OPTIONS "acdgiflmnopsuACGMP:U"
73 while (*++s
>= '0' && *s
<= '7')
74 r
= (r
* 8) + (*s
- '0');
79 finfo_main(argc
, argv
)
86 sh_optind
= 0; /* XXX */
87 prog
= base_pathname(argv
[0]);
93 while ((opt
= sh_getopt(argc
, argv
, OPTIONS
)) != EOF
) {
95 case 'a': flags
|= OPT_ATIME
; break;
96 case 'A': flags
|= OPT_ATIME
|OPT_ASCII
; break;
97 case 'c': flags
|= OPT_CTIME
; break;
98 case 'C': flags
|= OPT_CTIME
|OPT_ASCII
; break;
99 case 'd': flags
|= OPT_DEV
; break;
100 case 'i': flags
|= OPT_INO
; break;
101 case 'f': flags
|= OPT_FID
; break;
102 case 'g': flags
|= OPT_GID
; break;
103 case 'G': flags
|= OPT_GID
|OPT_ASCII
; break;
104 case 'l': flags
|= OPT_LNKNAM
; break;
105 case 'm': flags
|= OPT_MTIME
; break;
106 case 'M': flags
|= OPT_MTIME
|OPT_ASCII
; break;
107 case 'n': flags
|= OPT_NLINK
; break;
108 case 'o': flags
|= OPT_OPERM
; break;
109 case 'p': flags
|= OPT_PERM
; break;
110 case 'P': flags
|= OPT_PMASK
; pmask
= octal(sh_optarg
); break;
111 case 's': flags
|= OPT_SIZE
; break;
112 case 'u': flags
|= OPT_UID
; break;
113 case 'U': flags
|= OPT_UID
|OPT_ASCII
; break;
114 default: builtin_usage (); return(1);
126 for (i
= 0; i
< argc
; i
++)
127 opt
= flags
? printsome (argv
[i
], flags
) : printfinfo(argv
[i
]);
136 static struct stat st
;
140 if (strncmp(f
, "/dev/fd/", 8) == 0) {
141 if ((legal_number(f
+ 8, &lfd
) == 0) || (int)lfd
!= lfd
) {
142 builtin_error("%s: invalid fd", f
+ 8);
143 return ((struct stat
*)0);
154 builtin_error("%s: cannot stat: %s", f
, strerror(errno
));
155 return ((struct stat
*)0);
167 return (st
? printst(st
) : 1);
174 return (m
& (S_IRWXU
|S_IRWXG
|S_IRWXO
|S_ISUID
|S_ISGID
));
181 char ubits
[4], gbits
[4], obits
[4]; /* u=rwx,g=rwx,o=rwx */
212 ubits
[2] = (m
& S_IXUSR
) ? 's' : 'S';
214 gbits
[2] = (m
& S_IXGRP
) ? 's' : 'S';
216 obits
[2] = (m
& S_IXOTH
) ? 't' : 'T';
218 printf ("u=%s,g=%s,o=%s", ubits
, gbits
, obits
);
243 perms(getperm(mode
));
256 ma
= major (st
->st_rdev
);
257 mi
= minor (st
->st_rdev
);
258 #if defined (makedev)
259 d
= makedev (ma
, mi
);
261 d
= st
->st_rdev
& 0xFF;
263 printf("Device (major/minor): %d (%d/%d)\n", d
, ma
, mi
);
265 printf("Inode: %d\n", (int) st
->st_ino
);
266 printf("Mode: (%o) ", (int) st
->st_mode
);
267 printmode((int) st
->st_mode
);
268 printf("Link count: %d\n", (int) st
->st_nlink
);
269 pw
= getpwuid(st
->st_uid
);
270 owner
= pw
? pw
->pw_name
: "unknown";
271 printf("Uid of owner: %d (%s)\n", (int) st
->st_uid
, owner
);
272 gr
= getgrgid(st
->st_gid
);
273 owner
= gr
? gr
->gr_name
: "unknown";
274 printf("Gid of owner: %d (%s)\n", (int) st
->st_gid
, owner
);
275 printf("Device type: %d\n", (int) st
->st_rdev
);
276 printf("File size: %ld\n", (long) st
->st_size
);
277 printf("File last access time: %s", ctime (&st
->st_atime
));
278 printf("File last modify time: %s", ctime (&st
->st_mtime
));
279 printf("File last status change time: %s", ctime (&st
->st_ctime
));
299 /* Print requested info */
300 if (flags
& OPT_ATIME
) {
301 if (flags
& OPT_ASCII
)
302 printf("%s", ctime(&st
->st_atime
));
304 printf("%ld\n", st
->st_atime
);
305 } else if (flags
& OPT_MTIME
) {
306 if (flags
& OPT_ASCII
)
307 printf("%s", ctime(&st
->st_mtime
));
309 printf("%ld\n", st
->st_mtime
);
310 } else if (flags
& OPT_CTIME
) {
311 if (flags
& OPT_ASCII
)
312 printf("%s", ctime(&st
->st_ctime
));
314 printf("%ld\n", st
->st_ctime
);
315 } else if (flags
& OPT_DEV
)
316 printf("%d\n", st
->st_dev
);
317 else if (flags
& OPT_INO
)
318 printf("%d\n", st
->st_ino
);
319 else if (flags
& OPT_FID
)
320 printf("%d:%ld\n", st
->st_dev
, st
->st_ino
);
321 else if (flags
& OPT_NLINK
)
322 printf("%d\n", st
->st_nlink
);
323 else if (flags
& OPT_LNKNAM
) {
326 p
= readlink(f
, b
, 4096);
327 if (p
>= 0 && p
< 4096)
333 strcat(b
, strerror(p
));
340 } else if (flags
& OPT_PERM
) {
343 } else if (flags
& OPT_OPERM
)
344 printf("%o\n", getperm(st
->st_mode
));
345 else if (flags
& OPT_PMASK
)
346 printf("%o\n", getperm(st
->st_mode
) & pmask
);
347 else if (flags
& OPT_UID
) {
348 pw
= getpwuid(st
->st_uid
);
349 if (flags
& OPT_ASCII
)
350 printf("%s\n", pw
? pw
->pw_name
: "unknown");
352 printf("%d\n", st
->st_uid
);
353 } else if (flags
& OPT_GID
) {
354 gr
= getgrgid(st
->st_gid
);
355 if (flags
& OPT_ASCII
)
356 printf("%s\n", gr
? gr
->gr_name
: "unknown");
358 printf("%d\n", st
->st_gid
);
359 } else if (flags
& OPT_SIZE
)
360 printf("%ld\n", st
->st_size
);
374 v
= make_builtin_argv (list
, &c
);
375 r
= finfo_main (c
, v
);
381 static char *finfo_doc
[] = {
382 "Display information about file attributes.",
384 "Display information about each FILE. Only single operators should",
385 "be supplied. If no options are supplied, a summary of the info",
386 "available about each FILE is printed. If FILE is of the form",
387 "/dev/fd/XX, file descriptor XX is described. Operators, if supplied,",
388 "have the following meanings:",
390 " -a last file access time",
391 " -A last file access time in ctime format",
392 " -c last file status change time",
393 " -C last file status change time in ctime format",
394 " -m last file modification time",
395 " -M last file modification time in ctime format",
398 " -f composite file identifier (device:inode)",
400 " -G group name of owner",
401 " -l name of file pointed to by symlink",
403 " -o permissions in octal",
404 " -p permissions in ascii",
405 " -P mask permissions ANDed with MASK (like with umask)",
406 " -s file size in bytes",
408 " -U user name of owner",
412 struct builtin finfo_struct
= {
417 "finfo [-acdgiflmnopsuACGMPU] file [file...]",
423 #if defined (PREFER_STDARG)
426 # if defined (PREFER_VARARGS)
427 # include <varargs.h>
431 char *this_command_name
;
437 this_command_name
= argv
[0];
438 exit(finfo_main(argc
, argv
));
444 fprintf(stderr
, "%s: usage: %s [-%s] [file ...]\n", prog
, OPTIONS
);
447 #ifndef HAVE_STRERROR
452 static char ebuf
[40];
454 extern char *sys_errlist
[];
456 if (e
< 0 || e
> sys_nerr
) {
457 sprintf(ebuf
,"Unknown error code %d", e
);
460 return (sys_errlist
[e
]);
469 extern char *malloc();
474 fprintf(stderr
, "%s: cannot malloc %d bytes\n", prog
, s
);
484 if (t
= strrchr(p
, '/'))
490 legal_number (string
, result
)
503 /* Skip leading whitespace characters. */
504 while (whitespace (*string
))
510 /* We allow leading `-' or `+'. */
511 if (*string
== '-' || *string
== '+')
513 if (!digit (string
[1]))
522 while (digit (*string
))
525 value
= (value
* 10) + digit_value (*string
);
529 /* Skip trailing whitespace, if any. */
530 while (whitespace (*string
))
533 /* Error if not at end of string. */
538 *result
= value
* sign
;
563 #if defined (USE_VARARGS)
565 #if defined (PREFER_STDARG)
566 builtin_error (const char *format
, ...)
568 builtin_error (format
, va_alist
)
575 if (this_command_name
&& *this_command_name
)
576 fprintf (stderr
, "%s: ", this_command_name
);
578 #if defined (PREFER_STDARG)
579 va_start (args
, format
);
584 vfprintf (stderr
, format
, args
);
586 fprintf (stderr
, "\n");
590 builtin_error (format
, arg1
, arg2
, arg3
, arg4
, arg5
)
591 char *format
, *arg1
, *arg2
, *arg3
, *arg4
, *arg5
;
593 if (this_command_name
&& *this_command_name
)
594 fprintf (stderr
, "%s: ", this_command_name
);
596 fprintf (stderr
, format
, arg1
, arg2
, arg3
, arg4
, arg5
);
597 fprintf (stderr
, "\n");
600 #endif /* !USE_VARARGS */