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