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