Imported from ../bash-2.03.tar.gz.
[platform/upstream/bash.git] / examples / loadables / finfo.c
1 /*
2  * finfo - print file info
3  */
4
5 #include <sys/types.h>
6 #include "posixstat.h"
7 #include <stdio.h>
8 #include <pwd.h>
9 #include <grp.h>
10 #include <errno.h>
11
12 #include "shell.h"
13 #include "builtins.h"
14 #include "common.h"
15
16 #ifndef errno
17 extern int      errno;
18 #endif
19
20 extern char     *strrchr();
21 extern char     **make_builtin_argv ();
22
23 static int      printst();
24 static int      printsome();
25 static int      printfinfo();
26 static int      finfo_main();
27
28 extern int      sh_optind;
29 extern char     *sh_optarg;
30 extern char     *this_command_name;
31
32 static char     *prog;
33 static int      pmask;
34
35 #define OPT_UID         0x00001
36 #define OPT_GID         0x00002
37 #define OPT_DEV         0x00004
38 #define OPT_INO         0x00008
39 #define OPT_PERM        0x00010
40 #define OPT_LNKNAM      0x00020
41 #define OPT_FID         0x00040
42 #define OPT_NLINK       0x00080
43 #define OPT_RDEV        0x00100
44 #define OPT_SIZE        0x00200
45 #define OPT_ATIME       0x00400
46 #define OPT_MTIME       0x00800
47 #define OPT_CTIME       0x01000
48 #define OPT_BLKSIZE     0x02000
49 #define OPT_BLKS        0x04000
50 #define OPT_FTYPE       0x08000
51 #define OPT_PMASK       0x10000
52 #define OPT_OPERM       0x20000
53
54 #define OPT_ASCII       0x1000000
55
56 #define OPTIONS         "acdgiflmnopsuACGMP:U"
57
58 static int
59 octal(s)
60 char    *s;
61 {
62         int     r;
63
64         r = *s - '0';
65         while (*++s >= '0' && *s <= '7')
66                 r = (r * 8) + (*s - '0');
67         return r;
68 }
69
70 static int
71 finfo_main(argc, argv)
72 int     argc;
73 char    **argv;
74 {
75         register int    i;
76         int     mode, flags, opt;
77
78         sh_optind = 0;  /* XXX */
79         prog = base_pathname(argv[0]);
80         if (argc == 1) {
81                 builtin_usage();
82                 return(1);
83         }
84         flags = 0;
85         while ((opt = sh_getopt(argc, argv, OPTIONS)) != EOF) {
86                 switch(opt) {
87                 case 'a': flags |= OPT_ATIME; break;
88                 case 'A': flags |= OPT_ATIME|OPT_ASCII; break;
89                 case 'c': flags |= OPT_CTIME; break;
90                 case 'C': flags |= OPT_CTIME|OPT_ASCII; break;
91                 case 'd': flags |= OPT_DEV; break;
92                 case 'i': flags |= OPT_INO; break;
93                 case 'f': flags |= OPT_FID; break;
94                 case 'g': flags |= OPT_GID; break;
95                 case 'G': flags |= OPT_GID|OPT_ASCII; break;
96                 case 'l': flags |= OPT_LNKNAM; break;
97                 case 'm': flags |= OPT_MTIME; break;
98                 case 'M': flags |= OPT_MTIME|OPT_ASCII; break;
99                 case 'n': flags |= OPT_NLINK; break;
100                 case 'o': flags |= OPT_OPERM; break;
101                 case 'p': flags |= OPT_PERM; break;
102                 case 'P': flags |= OPT_PMASK; pmask = octal(sh_optarg); break;
103                 case 's': flags |= OPT_SIZE; break;
104                 case 'u': flags |= OPT_UID; break;
105                 case 'U': flags |= OPT_UID|OPT_ASCII; break;
106                 default: builtin_usage (); return(1);
107                 }
108         }
109
110         argc -= sh_optind;
111         argv += sh_optind;
112
113         if (argc == 0) {
114                 builtin_usage();
115                 return(1);
116         }
117
118         for (i = 0; i < argc; i++)
119                 opt = flags ? printsome (argv[i], flags) : printfinfo(argv[i]);
120
121         return(opt);
122 }
123
124 static struct stat *
125 getstat(f)
126 char    *f;
127 {
128         static struct stat st;
129         int     fd, r;
130         long    lfd;
131
132         if (strncmp(f, "/dev/fd/", 8) == 0) {
133                 if (legal_number(f + 8, &lfd) == 0) {
134                         builtin_error("%s: invalid fd", f + 8);
135                         return ((struct stat *)0);
136                 }
137                 fd = lfd;
138                 r = fstat(fd, &st);
139         } else
140                 r = stat(f, &st);
141         if (r < 0) {
142                 builtin_error("%s: cannot stat: %s", f, strerror(errno));
143                 return ((struct stat *)0);
144         }
145         return (&st);
146 }
147
148 static int
149 printfinfo(f)
150 char    *f;
151 {
152         struct stat *st;
153
154         st = getstat(f);
155         return (st ? printst(st) : 1);
156 }
157
158 static int
159 getperm(m)
160 int     m;
161 {
162         return (m & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID));
163 }
164
165 static int
166 perms(m)
167 int     m;
168 {
169         char ubits[4], gbits[4], obits[4];      /* u=rwx,g=rwx,o=rwx */
170         int i;
171
172         i = 0;
173         if (m & S_IRUSR)
174                 ubits[i++] = 'r';
175         if (m & S_IWUSR)
176                 ubits[i++] = 'w';
177         if (m & S_IXUSR)
178                 ubits[i++] = 'x';
179         ubits[i] = '\0';
180
181         i = 0;
182         if (m & S_IRGRP)
183                 gbits[i++] = 'r';
184         if (m & S_IWGRP)
185                 gbits[i++] = 'w';
186         if (m & S_IXGRP)
187                 gbits[i++] = 'x';
188         gbits[i] = '\0';
189
190         i = 0;
191         if (m & S_IROTH)
192                 obits[i++] = 'r';
193         if (m & S_IWOTH)
194                 obits[i++] = 'w';
195         if (m & S_IXOTH)
196                 obits[i++] = 'x';
197         obits[i] = '\0';
198
199         if (m & S_ISUID)
200                 ubits[2] = (m & S_IXUSR) ? 's' : 'S';
201         if (m & S_ISGID)
202                 gbits[2] = (m & S_IXGRP) ? 's' : 'S';
203         if (m & S_ISVTX)
204                 obits[2] = (m & S_IXOTH) ? 't' : 'T';
205
206         printf ("u=%s,g=%s,o=%s", ubits, gbits, obits);
207 }
208
209 static int
210 printmode(mode)
211 int     mode;
212 {
213         if (S_ISBLK(mode))
214                 printf("S_IFBLK ");
215         if (S_ISCHR(mode))
216                 printf("S_IFCHR ");
217         if (S_ISDIR(mode))
218                 printf("S_IFDIR ");
219         if (S_ISREG(mode))
220                 printf("S_IFREG ");
221         if (S_ISFIFO(mode))
222                 printf("S_IFIFO ");
223         if (S_ISLNK(mode))
224                 printf("S_IFLNK ");
225         if (S_ISSOCK(mode))
226                 printf("S_IFSOCK ");
227 #ifdef S_ISWHT
228         if (S_ISWHT(mode))
229                 printf("S_ISWHT ");
230 #endif
231         perms(getperm(mode));
232         printf("\n");
233 }
234
235 static int      
236 printst(st)
237 struct stat *st;
238 {
239         struct passwd   *pw;
240         struct group    *gr;
241         char    *owner;
242
243         printf("Device (major/minor): %d (%d/%d)\n", (int) (st->st_dev & 0xFF),
244                                                      (int) major (st->st_dev),
245                                                      (int) minor (st->st_dev));
246         printf("Inode: %d\n", (int) st->st_ino);
247         printf("Mode: (%o) ", (int) st->st_mode);
248         printmode((int) st->st_mode);
249         printf("Link count: %d\n", (int) st->st_nlink);
250         pw = getpwuid(st->st_uid);
251         owner = pw ? pw->pw_name : "unknown";
252         printf("Uid of owner: %d (%s)\n", (int) st->st_uid, owner);
253         gr = getgrgid(st->st_gid);
254         owner = gr ? gr->gr_name : "unknown";
255         printf("Gid of owner: %d (%s)\n", (int) st->st_gid, owner);
256         printf("Device type: %d\n", (int) st->st_rdev);
257         printf("File size: %ld\n", (long) st->st_size);
258         printf("File last access time: %s", ctime (&st->st_atime));
259         printf("File last modify time: %s", ctime (&st->st_mtime));
260         printf("File last status change time: %s", ctime (&st->st_ctime));
261         fflush(stdout);
262         return(0);
263 }
264
265 static int
266 printsome(f, flags)
267 char    *f;
268 int     flags;
269 {
270         struct stat *st;
271         struct passwd *pw;
272         struct group *gr;
273         int     p;
274         char    *b;
275
276         st = getstat(f);
277         if (st == NULL)
278                 return (1);
279
280         /* Print requested info */
281         if (flags & OPT_ATIME) {
282                 if (flags & OPT_ASCII)
283                         printf("%s", ctime(&st->st_atime));
284                 else
285                         printf("%ld\n", st->st_atime);
286         } else if (flags & OPT_MTIME) {
287                 if (flags & OPT_ASCII)
288                         printf("%s", ctime(&st->st_mtime));
289                 else
290                         printf("%ld\n", st->st_mtime);
291         } else if (flags & OPT_CTIME) {
292                 if (flags & OPT_ASCII)
293                         printf("%s", ctime(&st->st_ctime));
294                 else
295                         printf("%ld\n", st->st_ctime);
296         } else if (flags & OPT_DEV)
297                 printf("%d\n", st->st_dev);
298         else if (flags & OPT_INO)
299                 printf("%d\n", st->st_ino);
300         else if (flags & OPT_FID)
301                 printf("%d:%ld\n", st->st_dev, st->st_ino);
302         else if (flags & OPT_NLINK)
303                 printf("%d\n", st->st_nlink);
304         else if (flags & OPT_LNKNAM) {
305 #ifdef S_ISLNK
306                 b = xmalloc(4096);
307                 p = readlink(f, b, 4096);
308                 if (p >= 0 && p < 4096)
309                         b[p] = '\0';
310                 else {
311                         p = errno;
312                         strcpy(b, prog);
313                         strcat(b, ": ");
314                         strcat(b, strerror(p));
315                 }
316                 printf("%s\n", b);
317                 free(b);
318 #else
319                 printf("%s\n", f);
320 #endif
321         } else if (flags & OPT_PERM) {
322                 perms(st->st_mode);
323                 printf("\n");
324         } else if (flags & OPT_OPERM)
325                 printf("%o\n", getperm(st->st_mode));
326         else if (flags & OPT_PMASK)
327                 printf("%o\n", getperm(st->st_mode) & pmask);
328         else if (flags & OPT_UID) {
329                 pw = getpwuid(st->st_uid);
330                 if (flags & OPT_ASCII)
331                         printf("%s\n", pw ? pw->pw_name : "unknown");
332                 else
333                         printf("%d\n", st->st_uid);
334         } else if (flags & OPT_GID) {
335                 gr = getgrgid(st->st_gid);
336                 if (flags & OPT_ASCII)
337                         printf("%s\n", gr ? gr->gr_name : "unknown");
338                 else
339                         printf("%d\n", st->st_gid);
340         } else if (flags & OPT_SIZE)
341                 printf("%ld\n", st->st_size);
342
343         return (0);
344 }
345
346 #ifndef NOBUILTIN
347 finfo_builtin(list)
348      WORD_LIST *list;
349 {
350   int c, r;
351   char **v;
352   WORD_LIST *l;
353
354   v = make_builtin_argv (list, &c);
355   r = finfo_main (c, v);
356   free (v);
357
358   return r;
359 }
360
361 static char *finfo_doc[] = {
362   "Display information about each FILE.  Only single operators should",
363   "be supplied.  If no options are supplied, a summary of the info",
364   "available about each FILE is printed.  If FILE is of the form",
365   "/dev/fd/XX, file descriptor XX is described.  Operators, if supplied,",
366   "have the following meanings:",
367   "",
368   "     -a      last file access time",
369   "     -A      last file access time in ctime format",
370   "     -c      last file status change time",
371   "     -C      last file status change time in ctime format",
372   "     -m      last file modification time",
373   "     -M      last file modification time in ctime format",
374   "     -d      device",
375   "     -i      inode",
376   "     -f      composite file identifier (device:inode)",
377   "     -g      gid of owner",
378   "     -G      group name of owner",
379   "     -l      name of file pointed to by symlink",
380   "     -n      link count",
381   "     -o      permissions in octal",
382   "     -p      permissions in ascii",
383   "     -P mask permissions ANDed with MASK (like with umask)",
384   "     -s      file size in bytes",
385   "     -u      uid of owner",
386   "     -U      user name of owner",
387   (char *)0
388 };
389
390 struct builtin finfo_struct = {
391         "finfo",
392         finfo_builtin,
393         BUILTIN_ENABLED,
394         finfo_doc,
395         "finfo [-acdgiflmnopsuACGMPU] file [file...]",
396         0
397 };
398 #endif
399
400 #ifdef NOBUILTIN
401 #if defined (PREFER_STDARG)
402 #  include <stdarg.h>
403 #else
404 #  if defined (PREFER_VARARGS)
405 #    include <varargs.h>
406 #  endif
407 #endif
408
409 char *this_command_name;
410
411 main(argc, argv)
412 int     argc;
413 char    **argv;
414 {
415         this_command_name = argv[0];
416         exit(finfo_main(argc, argv));
417 }
418
419 void
420 builtin_usage()
421 {
422         fprintf(stderr, "%s: usage: %s [-%s] [file ...]\n", prog, OPTIONS);
423 }
424
425 #ifndef HAVE_STRERROR
426 char *
427 strerror(e)
428 int     e;
429 {
430         static char     ebuf[40];
431         extern int      sys_nerr;
432         extern char     *sys_errlist[];
433
434         if (e < 0 || e > sys_nerr) {
435                 sprintf(ebuf,"Unknown error code %d", e);
436                 return (&ebuf[0]);
437         }
438         return (sys_errlist[e]);
439 }
440 #endif
441
442 char *
443 xmalloc(s)
444 size_t  s;
445 {
446         char    *ret;
447         extern char *malloc();
448
449         ret = malloc(s);
450         if (ret)
451                 return (ret);
452         fprintf(stderr, "%s: cannot malloc %d bytes\n", prog, s);
453         exit(1);
454 }
455
456 char *
457 base_pathname(p)
458 char    *p;
459 {
460         char    *t;
461
462         if (t = strrchr(p, '/'))
463                 return(++t);
464         return(p);
465 }
466
467 int
468 legal_number (string, result)
469      char *string;
470      long *result;
471 {
472   int sign;
473   long value;
474
475   sign = 1;
476   value = 0;
477
478   if (result)
479     *result = 0;
480
481   /* Skip leading whitespace characters. */
482   while (whitespace (*string))
483     string++;
484
485   if (!*string)
486     return (0);
487
488   /* We allow leading `-' or `+'. */
489   if (*string == '-' || *string == '+')
490     {
491       if (!digit (string[1]))
492         return (0);
493
494       if (*string == '-')
495         sign = -1;
496
497       string++;
498     }
499
500   while (digit (*string))
501     {
502       if (result)
503         value = (value * 10) + digit_value (*string);
504       string++;
505     }
506
507   /* Skip trailing whitespace, if any. */
508   while (whitespace (*string))
509     string++;
510
511   /* Error if not at end of string. */
512   if (*string)
513     return (0);
514
515   if (result)
516     *result = value * sign;
517
518   return (1);
519 }
520
521 int sh_optind;
522 char *sh_optarg;
523 int sh_opterr;
524
525 extern int optind;
526 extern char *optarg;
527
528 int
529 sh_getopt(c, v, o)
530 int     c;
531 char    **v, *o;
532 {
533         int     r;
534
535         r = getopt(c, v, o);
536         sh_optind = optind;
537         sh_optarg = optarg;
538         return r;
539 }
540
541 #if defined (USE_VARARGS)
542 void
543 #if defined (PREFER_STDARG)
544 builtin_error (const char *format, ...)
545 #else
546 builtin_error (format, va_alist)
547      const char *format;
548      va_dcl
549 #endif
550 {
551   va_list args;
552
553   if (this_command_name && *this_command_name)
554     fprintf (stderr, "%s: ", this_command_name);
555
556 #if defined (PREFER_STDARG)
557   va_start (args, format);
558 #else
559   va_start (args);
560 #endif
561
562   vfprintf (stderr, format, args);
563   va_end (args);
564   fprintf (stderr, "\n");
565 }
566 #else
567 void
568 builtin_error (format, arg1, arg2, arg3, arg4, arg5)
569      char *format, *arg1, *arg2, *arg3, *arg4, *arg5;
570 {
571   if (this_command_name && *this_command_name)
572     fprintf (stderr, "%s: ", this_command_name);
573
574   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
575   fprintf (stderr, "\n");
576   fflush (stderr);
577 }
578 #endif /* !USE_VARARGS */
579
580 #endif