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