Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / tests / stat.c
1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <sys/sysmacros.h>
4 #include <pwd.h>
5 #include <grp.h>
6 #include <unistd.h>
7 #include <time.h>
8 #include <sys/vfs.h>
9 #include <string.h>
10 #include <malloc.h>
11 #include <ctype.h>
12 #include <stdlib.h>
13
14 #ifdef FLASK_LINUX
15 #include <selinux/fs_secure.h>
16 #include <linux/flask/security.h>
17 #include <selinux/flask_util.h>           /* for is_flask_enabled() */
18 #define SECURITY_ID_T security_id_t
19 #else
20 #define SECURITY_ID_T int
21 #endif
22
23 #include "stat_fs.h"
24
25 void print_human_type(unsigned short mode)
26 {
27   switch (mode & S_IFMT)
28     {
29     case S_IFDIR:
30       printf ("Directory");
31       break;
32     case S_IFCHR:
33       printf ("Character Device");
34       break;
35     case S_IFBLK:
36       printf ("Block Device");
37       break;
38     case S_IFREG:
39       printf ("Regular File");
40       break;
41     case S_IFLNK:
42       printf ("Symbolic Link");
43       break;
44     case S_IFSOCK:
45       printf ("Socket");
46       break;
47     case S_IFIFO:
48       printf ("Fifo File");
49       break;
50     default:
51       printf ("Unknown");
52     }
53 }
54
55 void print_human_fstype(struct statfs *statfsbuf)
56 {
57   char *type;
58   
59   switch (statfsbuf->f_type)
60 #if defined (__linux__)
61   {
62         case S_MAGIC_AFFS:
63           type = strdup("affs");
64           break;
65         case S_MAGIC_EXT:
66           type = strdup("ext");
67           break;
68         case S_MAGIC_EXT2_OLD:
69           type = strdup("ext2");
70           break;
71         case S_MAGIC_EXT2:
72           type = strdup("ext2/ext3");
73           break;
74         case S_MAGIC_HPFS:
75           type = strdup("hpfs");
76           break;
77         case S_MAGIC_ISOFS:
78           type = strdup("isofs");
79           break;
80         case S_MAGIC_ISOFS_WIN:
81           type = strdup("isofs");
82           break;
83         case S_MAGIC_ISOFS_R_WIN:
84           type = strdup("isofs");
85           break;
86         case S_MAGIC_MINIX:
87           type = strdup("minix");
88         case S_MAGIC_MINIX_30:
89           type = strdup("minix (30 char.)");
90           break;
91         case S_MAGIC_MINIX_V2:
92           type = strdup("minix v2");
93           break;
94         case S_MAGIC_MINIX_V2_30:
95           type = strdup("minix v2 (30 char.)");
96           break;
97         case S_MAGIC_MSDOS:
98           type = strdup("msdos");
99           break;
100         case S_MAGIC_FAT:
101           type = strdup("fat");
102           break;
103         case S_MAGIC_NCP:
104           type = strdup("novell");
105           break;
106         case S_MAGIC_NFS:
107           type = strdup("nfs");
108           break;
109         case S_MAGIC_PROC:
110           type = strdup("proc");
111           break;
112         case S_MAGIC_SMB:
113           type = strdup("smb");
114           break;
115         case S_MAGIC_XENIX:
116           type = strdup("xenix");
117           break;
118         case S_MAGIC_SYSV4:
119           type = strdup("sysv4");
120           break;
121         case S_MAGIC_SYSV2:
122           type = strdup("sysv2");
123           break;
124         case S_MAGIC_COH:
125           type = strdup("coh");
126           break;
127         case S_MAGIC_UFS:
128           type = strdup("ufs");
129           break;
130         case S_MAGIC_XIAFS:
131           type = strdup("xia");
132           break;
133         case S_MAGIC_NTFS:
134           type = strdup("ntfs");
135           break;
136         case S_MAGIC_TMPFS:
137           type = strdup("tmpfs");
138           break;
139         case S_MAGIC_REISERFS:
140           type = strdup("reiserfs");
141           break;
142         case S_MAGIC_CRAMFS:
143           type = strdup("cramfs");
144           break;
145         case S_MAGIC_ROMFS:
146           type = strdup("romfs");
147           break;
148 #elif __GNU__
149         case FSTYPE_UFS:
150           type = strdup("ufs");
151           break;
152         case FSTYPE_NFS:
153           type = strdup("nfs");
154           break;
155         case FSTYPE_GFS:
156           type = strdup("gfs");
157           break;
158         case FSTYPE_LFS:
159           type = strdup("lfs");
160           break;
161         case FSTYPE_SYSV:
162           type = strdup("sysv");
163           break;
164         case FSTYPE_FTP:
165           type = strdup("ftp");
166           break;
167         case FSTYPE_TAR:
168           type = strdup("tar");
169           break;
170         case FSTYPE_AR:
171           type = strdup("ar");
172           break;
173         case FSTYPE_CPIO:
174           type = strdup("cpio");
175           break;
176         case FSTYPE_MSLOSS:
177           type = strdup("msloss");
178           break;
179         case FSTYPE_CPM:
180           type = strdup("cpm");
181           break;
182         case FSTYPE_HFS:
183           type = strdup("hfs");
184           break;
185         case FSTYPE_DTFS:
186           type = strdup("dtfs");
187           break;
188         case FSTYPE_GRFS:
189           type = strdup("grfs");
190           break;
191         case FSTYPE_TERM:
192           type = strdup("term");
193           break;
194         case FSTYPE_DEV:
195           type = strdup("dev");
196           break;
197         case FSTYPE_PROC:
198           type = strdup("proc");
199           break;
200         case FSTYPE_IFSOCK:
201           type = strdup("ifsock");
202           break;
203         case FSTYPE_AFS:
204           type = strdup("afs");
205           break;
206         case FSTYPE_DFS:
207           type = strdup("dfs");
208           break;
209         case FSTYPE_PROC9:
210           type = strdup("proc9");
211           break;
212         case FSTYPE_SOCKET:
213           type = strdup("socket");
214           break;
215         case FSTYPE_MISC:
216           type = strdup("misc");
217           break;
218         case FSTYPE_EXT2FS:
219           type = strdup("ext2/ext3");
220           break;
221         case FSTYPE_HTTP:
222           type = strdup("http");
223           break;
224         case FSTYPE_MEMFS:
225           type = strdup("memfs");
226           break;
227         case FSTYPE_ISO9660:
228           type = strdup("iso9660");
229           break;
230 #endif
231         default:
232           if ((type = (char*)malloc(30 * sizeof(char))) == NULL) {
233                 perror("malloc error");
234                 return;
235           }
236           sprintf (type, "UNKNOWN (0x%x)\n", (int)statfsbuf->f_type);
237   }
238
239   printf("%s", type);
240   free(type);
241 }
242
243 void print_human_access(struct stat *statbuf)
244 {
245   char access[11];
246
247   access[10] = 0;
248   access[9] = (statbuf->st_mode & S_IXOTH) ?
249     ((statbuf->st_mode & S_ISVTX) ? 't' : 'x') :
250     ((statbuf->st_mode & S_ISVTX) ? 'T' : '-');
251   access[8] = (statbuf->st_mode & S_IWOTH) ? 'w' : '-';
252   access[7] = (statbuf->st_mode & S_IROTH) ? 'r' : '-';
253   access[6] = (statbuf->st_mode & S_IXGRP) ?
254     ((statbuf->st_mode & S_ISGID) ? 's' : 'x') :
255     ((statbuf->st_mode & S_ISGID) ? 'S' : '-');
256   access[5] = (statbuf->st_mode & S_IWGRP) ? 'w' : '-';
257   access[4] = (statbuf->st_mode & S_IRGRP) ? 'r' : '-';
258   access[3] = (statbuf->st_mode & S_IXUSR) ?
259     ((statbuf->st_mode & S_ISUID) ? 's' : 'x') :
260     ((statbuf->st_mode & S_ISUID) ? 'S' : '-');
261   access[2] = (statbuf->st_mode & S_IWUSR) ? 'w' : '-';
262   access[1] = (statbuf->st_mode & S_IRUSR) ? 'r' : '-';
263
264   switch (statbuf->st_mode & S_IFMT)
265     {
266     case S_IFDIR:
267       access[0] = 'd';
268       break;
269     case S_IFCHR:
270       access[0] = 'c';
271       break;
272     case S_IFBLK:
273       access[0] = 'b';
274       break;
275     case S_IFREG:
276       access[0] = '-';
277       break;
278     case S_IFLNK:
279       access[0] = 'l';
280       break;
281     case S_IFSOCK:
282       access[0] = 's';
283       break;
284     case S_IFIFO:
285       access[0] = 'p';
286       break;
287     default:
288       access[0] = '?';
289     }
290     printf (access);
291 }
292
293 void print_human_time(time_t *t)
294 {
295   char str[40];
296   
297   if (strftime(str, 40, "%c", localtime(t)) > 0) printf(str);
298   else printf("Cannot calculate human readable time, sorry");
299 }
300
301 /* print statfs info */
302 void print_statfs(char *pformat, char m, char *filename, void *data, SECURITY_ID_T sid)
303 {
304     struct statfs *statfsbuf = (struct statfs*)data;
305 #ifdef FLASK_LINUX
306     char sbuf[256];
307     int rv;
308     unsigned int sbuflen = sizeof(sbuf);
309 #endif
310     
311     switch(m) {
312         case 'n':
313             strcat(pformat, "s");
314             printf(pformat, filename);
315             break;
316 #if !defined(__linux__) && defined (__GNU__)
317         case 'i':
318             strcat(pformat, "Lx");
319             printf(pformat, statfsbuf->f_fsid);
320             break;
321 #else
322         case 'i':
323             strcat(pformat, "x %-8x");
324             printf(pformat, statfsbuf->f_fsid.__val[0], statfsbuf->f_fsid.__val[1]);
325             break;
326 #endif
327
328         case 'l':
329 #ifdef __USE_FILE_OFFSET64
330             strcat(pformat, "lu");
331 #else
332             strcat(pformat, "d");
333 #endif
334             printf(pformat, statfsbuf->f_namelen);
335             break;
336         case 't':
337 #ifdef __USE_FILE_OFFSET64
338             strcat(pformat, "lx");
339 #else
340             strcat(pformat, "x");
341 #endif
342             printf(pformat, statfsbuf->f_type);
343             break;
344         case 'T':
345 /*          print_human_fstype(statfsbuf, pformat);*/
346             print_human_fstype(statfsbuf);
347             break;
348         case 'b':
349 #ifdef __USE_FILE_OFFSET64
350             strcat(pformat, "lld");
351 #else
352 #if !defined(__linux__) && defined (__GNU__)
353             strcat(pformat, "d");
354 #else
355             strcat(pformat, "ld");
356 #endif
357 #endif
358             printf(pformat, statfsbuf->f_blocks);
359             break;
360         case 'f':
361 #ifdef __USE_FILE_OFFSET64
362             strcat(pformat, "lld");
363 #else
364 #if !defined(__linux__) && defined (__GNU__)
365             strcat(pformat, "d");
366 #else
367             strcat(pformat, "ld");
368 #endif
369 #endif
370             printf(pformat, statfsbuf->f_bfree);
371             break;
372         case 'a':
373 #ifdef __USE_FILE_OFFSET64
374             strcat(pformat, "lld");
375 #else
376 #if !defined(__linux__) && defined (__GNU__)
377             strcat(pformat, "d");
378 #else
379             strcat(pformat, "ld");
380 #endif
381 #endif
382             printf(pformat, statfsbuf->f_bavail);
383             break;
384         case 's':
385 #ifdef __USE_FILE_OFFSET64
386             strcat(pformat, "ld");
387 #else
388             strcat(pformat, "d");
389 #endif
390             printf(pformat, statfsbuf->f_bsize);
391             break;
392         case 'c':
393 #ifdef __USE_FILE_OFFSET64
394             strcat(pformat, "lld");
395 #else
396 #if !defined(__linux__) && defined (__GNU__)
397             strcat(pformat, "d");
398 #else
399             strcat(pformat, "ld");
400 #endif
401 #endif
402             printf(pformat, statfsbuf->f_files);
403             break;
404         case 'd':
405 #ifdef __USE_FILE_OFFSET64
406             strcat(pformat, "lld");
407 #else
408 #if !defined(__linux__) && defined (__GNU__)
409             strcat(pformat, "d");
410 #else
411             strcat(pformat, "ld");
412 #endif
413 #endif
414             printf(pformat, statfsbuf->f_ffree);
415             break;
416 #ifdef FLASK_LINUX
417         case 'S':
418             strcat(pformat, "d");
419             printf(pformat, sid);
420             break;
421         case 'C':
422             rv = security_sid_to_context(sid, (security_context_t *) &sbuf, &sbuflen);
423             if ( rv < 0 )
424                 sprintf(sbuf, "<error finding security context %d>", sid);
425             printf(sbuf);
426             break;
427 #endif
428         default:
429             strcat(pformat, "c");
430             printf(pformat, m);
431             break;
432     }
433 }
434
435 /* print stat info */
436 void print_stat(char *pformat, char m, char *filename, void *data, SECURITY_ID_T sid)
437 {
438     char linkname[256];
439     int i;
440     struct stat *statbuf = (struct stat*)data;
441     struct passwd *pw_ent;
442     struct group *gw_ent;
443 #ifdef FLASK_LINUX
444     char sbuf[256];
445     int rv;
446     unsigned int sbuflen = sizeof(sbuf);
447 #endif
448
449     switch(m) {
450         case 'n':
451             strcat(pformat, "s");
452             printf(pformat, filename);
453             break;
454         case 'N':
455             strcat(pformat, "s");
456             if ((statbuf->st_mode & S_IFMT) == S_IFLNK) {
457                 if ((i = readlink(filename, linkname, 256)) == -1) {
458                     perror(filename);
459                     return;
460                 }
461                 linkname[(i >= 256) ? 255 : i] = '\0';
462                 /*printf("\"%s\" -> \"%s\"", filename, linkname);*/
463                 printf("\"");
464                 printf(pformat, filename);
465                 printf("\" -> \"");
466                 printf(pformat, linkname);
467                 printf("\"");
468             } else {
469                 printf("\"");
470                 printf(pformat, filename);
471                 printf("\"");
472             }
473             break;
474         case 'd':
475             strcat(pformat, "d");
476             printf(pformat, (int)statbuf->st_dev);
477             break;
478         case 'D':
479             strcat(pformat, "x");
480             printf(pformat, (int)statbuf->st_dev);
481             break;
482         case 'i':
483             strcat(pformat, "d");
484             printf(pformat, (int)statbuf->st_ino);
485             break;
486         case 'a':
487             strcat(pformat, "o");
488             printf(pformat, statbuf->st_mode & 07777);
489             break;
490         case 'A':
491             print_human_access(statbuf);
492             break;
493         case 'f':
494             strcat(pformat, "x");
495             printf(pformat, statbuf->st_mode);
496             break;
497         case 'F':
498             print_human_type(statbuf->st_mode);
499             break;
500         case 'h':
501             strcat(pformat, "d");
502             printf(pformat, (int)statbuf->st_nlink);
503             break;
504 #ifdef FLASK_LINUX
505         case 'S':
506             strcat(pformat, "d");
507             printf(pformat, sid);
508             break;
509         case 'C':
510             rv = security_sid_to_context(sid, (security_context_t *) &sbuf, &sbuflen);
511             if ( rv < 0 )
512                 sprintf(sbuf, "<error finding security context %d>", sid);
513             printf(sbuf);
514             break;
515 #endif
516         case 'u':
517             strcat(pformat, "d");
518             printf(pformat, statbuf->st_uid);
519             break;
520         case 'U':
521             strcat(pformat, "s");
522             setpwent();
523             pw_ent = getpwuid(statbuf->st_uid);
524             printf(pformat,
525                 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
526             break;
527         case 'g':
528             strcat(pformat, "d");
529             printf(pformat, statbuf->st_gid);
530             break;
531         case 'G':
532             strcat(pformat, "s");
533             setgrent();
534             gw_ent = getgrgid(statbuf->st_gid);
535             printf(pformat,
536                 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
537             break;
538         case 't':
539             strcat(pformat, "x");
540             printf(pformat, major(statbuf->st_rdev));
541             break;
542         case 'T':
543             strcat(pformat, "x");
544             printf(pformat, minor(statbuf->st_rdev));
545             break;
546         case 's':
547 #ifdef __USE_FILE_OFFSET64
548             strcat(pformat, "llu");
549             printf(pformat, (unsigned long long)statbuf->st_size);
550 #else
551             strcat(pformat, "u");
552             printf(pformat, (unsigned int)statbuf->st_size);
553 #endif
554             break;
555         case 'b':
556             strcat(pformat, "u");
557             printf(pformat, (unsigned int)statbuf->st_blocks);
558             break;
559         case 'o':
560             strcat(pformat, "d");
561             printf(pformat, (int)statbuf->st_blksize);
562             break;
563         case 'x':
564             print_human_time(&(statbuf->st_atime));
565             break;
566         case 'X':
567             strcat(pformat, "d");
568             printf(pformat, (int)statbuf->st_atime);
569             break;
570         case 'y':
571             print_human_time(&(statbuf->st_mtime));
572             break;
573         case 'Y':
574             strcat(pformat, "d");
575             printf(pformat, (int)statbuf->st_mtime);
576             break;
577         case 'z':
578             print_human_time(&(statbuf->st_ctime));
579             break;
580         case 'Z':
581             strcat(pformat, "d");
582             printf(pformat, (int)statbuf->st_ctime);
583             break;
584         default:
585             strcat(pformat, "c");
586             printf(pformat, m);
587             break;
588     }
589 }
590
591 void print_it(char *masterformat, char *filename,
592     void (*print_func)(char*, char, char*, void*, SECURITY_ID_T), void *data, SECURITY_ID_T sid)
593 {
594     char *m, *b, *format;
595     char pformat[65];
596
597     /* create a working copy of the format string */
598     format = strdup(masterformat);
599     if (!format) {
600         perror(filename);
601         return;
602     }
603
604     b = format;
605     while (b)
606     {
607         if ((m = strchr(b, (int)'%')) != NULL)
608         {
609             strcpy (pformat, "%");
610             *m++ = '\0';
611             printf(b);
612
613             /* copy all format specifiers to our format string */
614             while (isdigit(*m) || strchr("#0-+. I", *m))
615             {
616                 char copy[2]="a";
617
618                 *copy = *m;
619                 /* make sure the format specifier is not too long */
620                 if (strlen (pformat) > 63)
621                         fprintf(stderr, "Warning: Format specifier too long, truncating: %s\n", pformat);
622                 else
623                         strcat (pformat, copy);
624                 m++;
625             }
626
627             switch(*m) {
628                 case '\0':
629                 case '%':
630                     printf("%%");
631                     break;
632                 default:
633                     print_func(pformat, *m, filename, data, sid);
634                     break;
635             }
636             b = m + 1;
637         }
638         else
639         {
640             printf(b);
641             b = NULL;
642         }
643     }
644     free(format);
645     printf("\n");
646 }
647
648 /* stat the filesystem and print what we find */
649 void
650 do_statfs (char *filename, int terse, int secure, char *format)
651 {
652   struct statfs statfsbuf;
653   SECURITY_ID_T sid = -1;
654   int i;
655
656 #ifdef FLASK_LINUX
657   if(!is_flask_enabled())
658     secure = 0;
659   if(secure)
660     i = statfs_secure(filename, &statfsbuf, &sid);
661   else
662 #endif
663     i = statfs(filename, &statfsbuf);
664   if(i == -1)
665     {
666       perror (filename);
667       return;
668     }
669
670   if (format == NULL)
671     {
672         if (terse != 0)
673           {
674 #ifdef FLASK_LINUX
675                 if(secure)
676                         format = "%n %i %l %t %b %f %a %s %c %d %S %C";
677                 else
678 #endif
679                         format = "%n %i %l %t %b %f %a %s %c %d";
680           }
681         else
682           {
683 #ifdef FLASK_LINUX
684                 if(secure)
685                     format = "  File: \"%n\"\n"
686                          "    ID: %-8i Namelen: %-7l Type: %T\n"
687                          "Blocks: Total: %-10b Free: %-10f Available: %-10a Size: %s\n"
688                          "Inodes: Total: %-10c Free: %-10d\n"
689                          "   SID: %-14S  S_Context: %C\n";
690                 else
691 #endif
692                     format = "  File: \"%n\"\n"
693                          "    ID: %-8i Namelen: %-7l Type: %T\n"
694                          "Blocks: Total: %-10b Free: %-10f Available: %-10a Size: %s\n"
695                          "Inodes: Total: %-10c Free: %-10d";
696           }
697     }
698
699     print_it(format, filename, print_statfs, &statfsbuf, sid);
700 }
701
702 /* stat the file and print what we find */
703 void
704 do_stat (char *filename, int link, int terse, int secure, char *format)
705 {
706   struct stat statbuf;
707   int i;
708   SECURITY_ID_T sid = -1;
709   
710 #ifdef FLASK_LINUX
711   if(!is_flask_enabled())
712     secure = 0;
713   if(secure)
714     i = (link == 1) ? stat_secure(filename, &statbuf, &sid) : lstat_secure(filename, &statbuf, &sid);
715   else
716 #endif
717   i = (link == 1) ? stat(filename, &statbuf) : lstat(filename, &statbuf);
718
719   if (i == -1)
720     {
721       perror (filename);
722       return;
723     }
724
725    if (format == NULL)
726     {
727        if (terse != 0)
728        {
729 #ifdef FLASK_LINUX
730            if (secure)
731                   format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %S %C";
732            else
733 #endif
734                   format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
735        }
736        else
737        {
738            /* tmp hack to match orignal output until conditional implemented */
739           i = statbuf.st_mode & S_IFMT;
740           if (i == S_IFCHR || i == S_IFBLK) {
741 #ifdef FLASK_LINUX
742                 if (secure)
743                         format =
744                            "  File: %N\n"
745                            "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
746                            "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
747                            " Device type: %t,%T\n"
748                            "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
749                            "   SID: %-14S  S_Context: %C\n"
750                            "Access: %x\n"
751                            "Modify: %y\n"
752                            "Change: %z\n";
753                 else
754 #endif
755                         format =           
756                            "  File: %N\n"
757                            "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
758                            "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
759                            " Device type: %t,%T\n"
760                            "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
761                            "Access: %x\n"
762                            "Modify: %y\n"
763                            "Change: %z\n";
764            }
765            else
766            {
767 #ifdef FLASK_LINUX
768                 if (secure)
769                        format =
770                            "  File: %N\n"
771                            "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
772                            "Device: %Dh/%dd\tInode: %-10i  Links: %-5h\n"
773                            "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
774                            "   SID: %-14S  S_Context: %C\n"
775                            "Access: %x\n"
776                            "Modify: %y\n"
777                            "Change: %z\n";
778                 else
779 #endif
780                       format =
781                            "  File: %N\n"
782                            "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
783                            "Device: %Dh/%dd\tInode: %-10i  Links: %-5h\n"
784                            "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
785                            "Access: %x\n"
786                            "Modify: %y\n"
787                            "Change: %z\n";
788           }
789        }
790     }
791     print_it(format, filename, print_stat, &statbuf, sid);
792 }
793
794 void
795 usage (char *progname)
796 {
797   fprintf (stderr, "Usage: %s [-l] [-f] [-s] [-v] [-h] [-t] [-c format] file1 [file2 ...]\n", progname);
798   exit (1);
799 }
800
801 void verbose_usage(char *progname)
802 {
803     fprintf(stderr, "Usage: %s [-l] [-f] [-s] [-v] [-h] [-t] [-c format] file1 [file2 ...]\n", progname);
804     fprintf(stderr, "\tformat interpreted sequences for file stat are:\n");
805     fprintf(stderr, "\t\t%%n - File name\n");
806     fprintf(stderr, "\t\t%%N - Quoted File name with dereference if symbolic link\n");
807     fprintf(stderr, "\t\t%%d - Device number in decimal\n");
808     fprintf(stderr, "\t\t%%D - Device number in hex\n");
809     fprintf(stderr, "\t\t%%i - Inode number\n");
810     fprintf(stderr, "\t\t%%a - Access rights in octal\n");
811     fprintf(stderr, "\t\t%%A - Access rights in human readable form\n");
812     fprintf(stderr, "\t\t%%f - raw mode in hex\n");
813     fprintf(stderr, "\t\t%%F - File type\n");
814     fprintf(stderr, "\t\t%%h - Number of hard links\n");
815     fprintf(stderr, "\t\t%%u - User Id of owner\n");
816     fprintf(stderr, "\t\t%%U - User name of owner\n");
817     fprintf(stderr, "\t\t%%g - Group Id of owner\n");
818     fprintf(stderr, "\t\t%%G - Group name of owner\n");
819     fprintf(stderr, "\t\t%%t - Major device type in hex\n");
820     fprintf(stderr, "\t\t%%T - Minor device type in hex\n");
821     fprintf(stderr, "\t\t%%s - Total size, in bytes\n");
822     fprintf(stderr, "\t\t%%b - Number of blocks allocated\n");
823     fprintf(stderr, "\t\t%%o - IO block size\n");
824     fprintf(stderr, "\t\t%%x - Time of last access\n");
825     fprintf(stderr, "\t\t%%X - Time of last access as seconds since Epoch\n");
826     fprintf(stderr, "\t\t%%y - Time of last modification\n");
827     fprintf(stderr, "\t\t%%Y - Time of last modification as seconds since Epoch\n");
828     fprintf(stderr, "\t\t%%z - Time of last change\n");
829     fprintf(stderr, "\t\t%%Z - Time of last change as seconds since Epoch\n");
830     fprintf(stderr, "\t\t%%S - Security ID in SE-Linux\n");
831     fprintf(stderr, "\t\t%%C - Security context in SE-Linux\n");
832     fprintf(stderr, "\tformat interpreted sequences for filesystem stat are:\n");
833     fprintf(stderr, "\t\t%%n - File name\n");
834     fprintf(stderr, "\t\t%%i - File System id in hex\n");
835     fprintf(stderr, "\t\t%%l - Maximum length of filenames\n");
836     fprintf(stderr, "\t\t%%t - Type in hex\n");
837     fprintf(stderr, "\t\t%%T - Type in human readable form\n");
838     fprintf(stderr, "\t\t%%b - Total data blocks in file system\n");
839     fprintf(stderr, "\t\t%%f - Free blocks in file system\n");
840     fprintf(stderr, "\t\t%%a - Free blocks available to non-superuser\n");
841     fprintf(stderr, "\t\t%%s - Optimal transfer block size\n");
842     fprintf(stderr, "\t\t%%c - Total file nodes in file system\n");
843     fprintf(stderr, "\t\t%%S - Security ID in SE-Linux\n");
844     fprintf(stderr, "\t\t%%C - Security context in SE-Linux\n");
845     fprintf(stderr, "\t\t%%d - Free file nodes in file system\n");
846     exit(1);
847 }
848   
849
850 int
851 main (int argc, char *argv[])
852 {
853   int c, i, link = 0, fs = 0, terse = 0, secure = 0;
854   char *format = NULL;
855   
856   while ((c = getopt (argc, argv, "lsfvthc:")) != EOF)
857     {
858       switch (c)
859         {
860         case 'l':
861           link = 1;
862           break;
863         case 's':
864           secure = 1;
865           break;
866         case 'f':
867           fs = 1;
868           break;
869         case 't':
870           terse = 1;
871           break;
872         case 'c':
873           format = optarg;
874           break;
875         case 'h':
876           printf ("stat version: 3.0\n");
877           verbose_usage(argv[0]);
878           break;
879         case 'v':
880           printf ("stat version: 3.0\n");
881         default:
882           usage (argv[0]);
883         }
884     }
885   if (argc == optind)
886     usage (argv[0]);
887
888   for (i = optind; i < argc; i++)
889     (fs == 0) ? do_stat (argv[i], link, terse, secure, format) :
890                 do_statfs (argv[i], terse, secure, format);
891
892   return (0);
893 }