Patch-ID: bash41-003
[bash.git] / examples / loadables / finfo.c
blobaf7ffb64ce02fcd401ff5234dc0f8197c6e8a55e
1 /*
2 * finfo - print file info
4 * Chet Ramey
5 * chet@po.cwru.edu
6 */
8 #ifdef HAVE_CONFIG_H
9 # include <config.h>
10 #endif
12 #include <sys/types.h>
13 #include "posixstat.h"
14 #include <stdio.h>
15 #include <pwd.h>
16 #include <grp.h>
17 #include <errno.h>
18 #include "posixtime.h"
20 #include "bashansi.h"
21 #include "shell.h"
22 #include "builtins.h"
23 #include "common.h"
25 #ifndef errno
26 extern int errno;
27 #endif
29 extern char **make_builtin_argv ();
31 static int printst();
32 static int printsome();
33 static int printfinfo();
34 static int finfo_main();
36 extern int sh_optind;
37 extern char *sh_optarg;
38 extern char *this_command_name;
40 static char *prog;
41 static int pmask;
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"
66 static int
67 octal(s)
68 char *s;
70 int r;
72 r = *s - '0';
73 while (*++s >= '0' && *s <= '7')
74 r = (r * 8) + (*s - '0');
75 return r;
78 static int
79 finfo_main(argc, argv)
80 int argc;
81 char **argv;
83 register int i;
84 int mode, flags, opt;
86 sh_optind = 0; /* XXX */
87 prog = base_pathname(argv[0]);
88 if (argc == 1) {
89 builtin_usage();
90 return(1);
92 flags = 0;
93 while ((opt = sh_getopt(argc, argv, OPTIONS)) != EOF) {
94 switch(opt) {
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);
118 argc -= sh_optind;
119 argv += sh_optind;
121 if (argc == 0) {
122 builtin_usage();
123 return(1);
126 for (i = 0; i < argc; i++)
127 opt = flags ? printsome (argv[i], flags) : printfinfo(argv[i]);
129 return(opt);
132 static struct stat *
133 getstat(f)
134 char *f;
136 static struct stat st;
137 int fd, r;
138 intmax_t lfd;
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);
145 fd = lfd;
146 r = fstat(fd, &st);
147 } else
148 #ifdef HAVE_LSTAT
149 r = lstat(f, &st);
150 #else
151 r = stat(f, &st);
152 #endif
153 if (r < 0) {
154 builtin_error("%s: cannot stat: %s", f, strerror(errno));
155 return ((struct stat *)0);
157 return (&st);
160 static int
161 printfinfo(f)
162 char *f;
164 struct stat *st;
166 st = getstat(f);
167 return (st ? printst(st) : 1);
170 static int
171 getperm(m)
172 int m;
174 return (m & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID));
177 static int
178 perms(m)
179 int m;
181 char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
182 int i;
184 i = 0;
185 if (m & S_IRUSR)
186 ubits[i++] = 'r';
187 if (m & S_IWUSR)
188 ubits[i++] = 'w';
189 if (m & S_IXUSR)
190 ubits[i++] = 'x';
191 ubits[i] = '\0';
193 i = 0;
194 if (m & S_IRGRP)
195 gbits[i++] = 'r';
196 if (m & S_IWGRP)
197 gbits[i++] = 'w';
198 if (m & S_IXGRP)
199 gbits[i++] = 'x';
200 gbits[i] = '\0';
202 i = 0;
203 if (m & S_IROTH)
204 obits[i++] = 'r';
205 if (m & S_IWOTH)
206 obits[i++] = 'w';
207 if (m & S_IXOTH)
208 obits[i++] = 'x';
209 obits[i] = '\0';
211 if (m & S_ISUID)
212 ubits[2] = (m & S_IXUSR) ? 's' : 'S';
213 if (m & S_ISGID)
214 gbits[2] = (m & S_IXGRP) ? 's' : 'S';
215 if (m & S_ISVTX)
216 obits[2] = (m & S_IXOTH) ? 't' : 'T';
218 printf ("u=%s,g=%s,o=%s", ubits, gbits, obits);
221 static int
222 printmode(mode)
223 int mode;
225 if (S_ISBLK(mode))
226 printf("S_IFBLK ");
227 if (S_ISCHR(mode))
228 printf("S_IFCHR ");
229 if (S_ISDIR(mode))
230 printf("S_IFDIR ");
231 if (S_ISREG(mode))
232 printf("S_IFREG ");
233 if (S_ISFIFO(mode))
234 printf("S_IFIFO ");
235 if (S_ISLNK(mode))
236 printf("S_IFLNK ");
237 if (S_ISSOCK(mode))
238 printf("S_IFSOCK ");
239 #ifdef S_ISWHT
240 if (S_ISWHT(mode))
241 printf("S_ISWHT ");
242 #endif
243 perms(getperm(mode));
244 printf("\n");
247 static int
248 printst(st)
249 struct stat *st;
251 struct passwd *pw;
252 struct group *gr;
253 char *owner;
254 int ma, mi, d;
256 ma = major (st->st_rdev);
257 mi = minor (st->st_rdev);
258 #if defined (makedev)
259 d = makedev (ma, mi);
260 #else
261 d = st->st_rdev & 0xFF;
262 #endif
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));
280 fflush(stdout);
281 return(0);
284 static int
285 printsome(f, flags)
286 char *f;
287 int flags;
289 struct stat *st;
290 struct passwd *pw;
291 struct group *gr;
292 int p;
293 char *b;
295 st = getstat(f);
296 if (st == NULL)
297 return (1);
299 /* Print requested info */
300 if (flags & OPT_ATIME) {
301 if (flags & OPT_ASCII)
302 printf("%s", ctime(&st->st_atime));
303 else
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));
308 else
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));
313 else
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) {
324 #ifdef S_ISLNK
325 b = xmalloc(4096);
326 p = readlink(f, b, 4096);
327 if (p >= 0 && p < 4096)
328 b[p] = '\0';
329 else {
330 p = errno;
331 strcpy(b, prog);
332 strcat(b, ": ");
333 strcat(b, strerror(p));
335 printf("%s\n", b);
336 free(b);
337 #else
338 printf("%s\n", f);
339 #endif
340 } else if (flags & OPT_PERM) {
341 perms(st->st_mode);
342 printf("\n");
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");
351 else
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");
357 else
358 printf("%d\n", st->st_gid);
359 } else if (flags & OPT_SIZE)
360 printf("%ld\n", (long) st->st_size);
362 return (0);
365 #ifndef NOBUILTIN
367 finfo_builtin(list)
368 WORD_LIST *list;
370 int c, r;
371 char **v;
372 WORD_LIST *l;
374 v = make_builtin_argv (list, &c);
375 r = finfo_main (c, v);
376 free (v);
378 return r;
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",
396 " -d device",
397 " -i inode",
398 " -f composite file identifier (device:inode)",
399 " -g gid of owner",
400 " -G group name of owner",
401 " -l name of file pointed to by symlink",
402 " -n link count",
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",
407 " -u uid of owner",
408 " -U user name of owner",
409 (char *)0
412 struct builtin finfo_struct = {
413 "finfo",
414 finfo_builtin,
415 BUILTIN_ENABLED,
416 finfo_doc,
417 "finfo [-acdgiflmnopsuACGMPU] file [file...]",
420 #endif
422 #ifdef NOBUILTIN
423 #if defined (PREFER_STDARG)
424 # include <stdarg.h>
425 #else
426 # if defined (PREFER_VARARGS)
427 # include <varargs.h>
428 # endif
429 #endif
431 char *this_command_name;
433 main(argc, argv)
434 int argc;
435 char **argv;
437 this_command_name = argv[0];
438 exit(finfo_main(argc, argv));
441 void
442 builtin_usage()
444 fprintf(stderr, "%s: usage: %s [-%s] [file ...]\n", prog, OPTIONS);
447 #ifndef HAVE_STRERROR
448 char *
449 strerror(e)
450 int e;
452 static char ebuf[40];
453 extern int sys_nerr;
454 extern char *sys_errlist[];
456 if (e < 0 || e > sys_nerr) {
457 sprintf(ebuf,"Unknown error code %d", e);
458 return (&ebuf[0]);
460 return (sys_errlist[e]);
462 #endif
464 char *
465 xmalloc(s)
466 size_t s;
468 char *ret;
469 extern char *malloc();
471 ret = malloc(s);
472 if (ret)
473 return (ret);
474 fprintf(stderr, "%s: cannot malloc %d bytes\n", prog, s);
475 exit(1);
478 char *
479 base_pathname(p)
480 char *p;
482 char *t;
484 if (t = strrchr(p, '/'))
485 return(++t);
486 return(p);
490 legal_number (string, result)
491 char *string;
492 long *result;
494 int sign;
495 long value;
497 sign = 1;
498 value = 0;
500 if (result)
501 *result = 0;
503 /* Skip leading whitespace characters. */
504 while (whitespace (*string))
505 string++;
507 if (!*string)
508 return (0);
510 /* We allow leading `-' or `+'. */
511 if (*string == '-' || *string == '+')
513 if (!digit (string[1]))
514 return (0);
516 if (*string == '-')
517 sign = -1;
519 string++;
522 while (digit (*string))
524 if (result)
525 value = (value * 10) + digit_value (*string);
526 string++;
529 /* Skip trailing whitespace, if any. */
530 while (whitespace (*string))
531 string++;
533 /* Error if not at end of string. */
534 if (*string)
535 return (0);
537 if (result)
538 *result = value * sign;
540 return (1);
543 int sh_optind;
544 char *sh_optarg;
545 int sh_opterr;
547 extern int optind;
548 extern char *optarg;
551 sh_getopt(c, v, o)
552 int c;
553 char **v, *o;
555 int r;
557 r = getopt(c, v, o);
558 sh_optind = optind;
559 sh_optarg = optarg;
560 return r;
563 #if defined (USE_VARARGS)
564 void
565 #if defined (PREFER_STDARG)
566 builtin_error (const char *format, ...)
567 #else
568 builtin_error (format, va_alist)
569 const char *format;
570 va_dcl
571 #endif
573 va_list args;
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);
580 #else
581 va_start (args);
582 #endif
584 vfprintf (stderr, format, args);
585 va_end (args);
586 fprintf (stderr, "\n");
588 #else
589 void
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");
598 fflush (stderr);
600 #endif /* !USE_VARARGS */
602 #endif