Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / examples / loadables / finfo.c
1 /*
2  * finfo - print file info
3  *
4  * Chet Ramey
5  * chet@po.cwru.edu
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #  include <config.h>
10 #endif
11
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"
19
20 #include "bashansi.h"
21 #include "shell.h"
22 #include "builtins.h"
23 #include "common.h"
24
25 #ifndef errno
26 extern int      errno;
27 #endif
28
29 extern char     **make_builtin_argv ();
30
31 static int      printst();
32 static int      printsome();
33 static int      printfinfo();
34 static int      finfo_main();
35
36 extern int      sh_optind;
37 extern char     *sh_optarg;
38 extern char     *this_command_name;
39
40 static char     *prog;
41 static int      pmask;
42
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
61
62 #define OPT_ASCII       0x1000000
63
64 #define OPTIONS         "acdgiflmnopsuACGMP:U"
65
66 static int
67 octal(s)
68 char    *s;
69 {
70         int     r;
71
72         r = *s - '0';
73         while (*++s >= '0' && *s <= '7')
74                 r = (r * 8) + (*s - '0');
75         return r;
76 }
77
78 static int
79 finfo_main(argc, argv)
80 int     argc;
81 char    **argv;
82 {
83         register int    i;
84         int     mode, flags, opt;
85
86         sh_optind = 0;  /* XXX */
87         prog = base_pathname(argv[0]);
88         if (argc == 1) {
89                 builtin_usage();
90                 return(1);
91         }
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);
115                 }
116         }
117
118         argc -= sh_optind;
119         argv += sh_optind;
120
121         if (argc == 0) {
122                 builtin_usage();
123                 return(1);
124         }
125
126         for (i = 0; i < argc; i++)
127                 opt = flags ? printsome (argv[i], flags) : printfinfo(argv[i]);
128
129         return(opt);
130 }
131
132 static struct stat *
133 getstat(f)
134 char    *f;
135 {
136         static struct stat st;
137         int     fd, r;
138         intmax_t lfd;
139
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);
144                 }
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);
156         }
157         return (&st);
158 }
159
160 static int
161 printfinfo(f)
162 char    *f;
163 {
164         struct stat *st;
165
166         st = getstat(f);
167         return (st ? printst(st) : 1);
168 }
169
170 static int
171 getperm(m)
172 int     m;
173 {
174         return (m & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID));
175 }
176
177 static int
178 perms(m)
179 int     m;
180 {
181         char ubits[4], gbits[4], obits[4];      /* u=rwx,g=rwx,o=rwx */
182         int i;
183
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';
192
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';
201
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';
210
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';
217
218         printf ("u=%s,g=%s,o=%s", ubits, gbits, obits);
219 }
220
221 static int
222 printmode(mode)
223 int     mode;
224 {
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");
245 }
246
247 static int      
248 printst(st)
249 struct stat *st;
250 {
251         struct passwd   *pw;
252         struct group    *gr;
253         char    *owner;
254         int     ma, mi, d;
255
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);
264
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);
282 }
283
284 static int
285 printsome(f, flags)
286 char    *f;
287 int     flags;
288 {
289         struct stat *st;
290         struct passwd *pw;
291         struct group *gr;
292         int     p;
293         char    *b;
294
295         st = getstat(f);
296         if (st == NULL)
297                 return (1);
298
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));
334                 }
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", st->st_size);
361
362         return (0);
363 }
364
365 #ifndef NOBUILTIN
366 int
367 finfo_builtin(list)
368      WORD_LIST *list;
369 {
370   int c, r;
371   char **v;
372   WORD_LIST *l;
373
374   v = make_builtin_argv (list, &c);
375   r = finfo_main (c, v);
376   free (v);
377
378   return r;
379 }
380
381 static char *finfo_doc[] = {
382   "Display information about file attributes.",
383   "",
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:",
389   "",
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
410 };
411
412 struct builtin finfo_struct = {
413         "finfo",
414         finfo_builtin,
415         BUILTIN_ENABLED,
416         finfo_doc,
417         "finfo [-acdgiflmnopsuACGMPU] file [file...]",
418         0
419 };
420 #endif
421
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
430
431 char *this_command_name;
432
433 main(argc, argv)
434 int     argc;
435 char    **argv;
436 {
437         this_command_name = argv[0];
438         exit(finfo_main(argc, argv));
439 }
440
441 void
442 builtin_usage()
443 {
444         fprintf(stderr, "%s: usage: %s [-%s] [file ...]\n", prog, OPTIONS);
445 }
446
447 #ifndef HAVE_STRERROR
448 char *
449 strerror(e)
450 int     e;
451 {
452         static char     ebuf[40];
453         extern int      sys_nerr;
454         extern char     *sys_errlist[];
455
456         if (e < 0 || e > sys_nerr) {
457                 sprintf(ebuf,"Unknown error code %d", e);
458                 return (&ebuf[0]);
459         }
460         return (sys_errlist[e]);
461 }
462 #endif
463
464 char *
465 xmalloc(s)
466 size_t  s;
467 {
468         char    *ret;
469         extern char *malloc();
470
471         ret = malloc(s);
472         if (ret)
473                 return (ret);
474         fprintf(stderr, "%s: cannot malloc %d bytes\n", prog, s);
475         exit(1);
476 }
477
478 char *
479 base_pathname(p)
480 char    *p;
481 {
482         char    *t;
483
484         if (t = strrchr(p, '/'))
485                 return(++t);
486         return(p);
487 }
488
489 int
490 legal_number (string, result)
491      char *string;
492      long *result;
493 {
494   int sign;
495   long value;
496
497   sign = 1;
498   value = 0;
499
500   if (result)
501     *result = 0;
502
503   /* Skip leading whitespace characters. */
504   while (whitespace (*string))
505     string++;
506
507   if (!*string)
508     return (0);
509
510   /* We allow leading `-' or `+'. */
511   if (*string == '-' || *string == '+')
512     {
513       if (!digit (string[1]))
514         return (0);
515
516       if (*string == '-')
517         sign = -1;
518
519       string++;
520     }
521
522   while (digit (*string))
523     {
524       if (result)
525         value = (value * 10) + digit_value (*string);
526       string++;
527     }
528
529   /* Skip trailing whitespace, if any. */
530   while (whitespace (*string))
531     string++;
532
533   /* Error if not at end of string. */
534   if (*string)
535     return (0);
536
537   if (result)
538     *result = value * sign;
539
540   return (1);
541 }
542
543 int sh_optind;
544 char *sh_optarg;
545 int sh_opterr;
546
547 extern int optind;
548 extern char *optarg;
549
550 int
551 sh_getopt(c, v, o)
552 int     c;
553 char    **v, *o;
554 {
555         int     r;
556
557         r = getopt(c, v, o);
558         sh_optind = optind;
559         sh_optarg = optarg;
560         return r;
561 }
562
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
572 {
573   va_list args;
574
575   if (this_command_name && *this_command_name)
576     fprintf (stderr, "%s: ", this_command_name);
577
578 #if defined (PREFER_STDARG)
579   va_start (args, format);
580 #else
581   va_start (args);
582 #endif
583
584   vfprintf (stderr, format, args);
585   va_end (args);
586   fprintf (stderr, "\n");
587 }
588 #else
589 void
590 builtin_error (format, arg1, arg2, arg3, arg4, arg5)
591      char *format, *arg1, *arg2, *arg3, *arg4, *arg5;
592 {
593   if (this_command_name && *this_command_name)
594     fprintf (stderr, "%s: ", this_command_name);
595
596   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
597   fprintf (stderr, "\n");
598   fflush (stderr);
599 }
600 #endif /* !USE_VARARGS */
601
602 #endif