Whamcloud - gitweb
- shut up, shut up, shut up, shut up!
[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 /* trick gcc into being unable to recognize the %c format so it doesn't
294  * issue its inane warning about %c and two-digit year representations. */
295 static size_t strftime_wrapper(char *s, size_t max, const char *fmt,
296                                 struct tm *tm) {
297         return strftime(s, max, fmt, tm);
298 }
299
300 void print_human_time(time_t *t)
301 {
302   char str[40];
303   
304   if (strftime_wrapper(str, 40, "%c", localtime(t)) > 0) 
305           printf(str);
306   else 
307           printf("Cannot calculate human readable time, sorry");
308 }
309
310 /* print statfs info */
311 void print_statfs(char *pformat, char m, char *filename, void *data, SECURITY_ID_T sid)
312 {
313     struct statfs *statfsbuf = (struct statfs*)data;
314 #ifdef FLASK_LINUX
315     char sbuf[256];
316     int rv;
317     unsigned int sbuflen = sizeof(sbuf);
318 #endif
319     
320     switch(m) {
321         case 'n':
322             strcat(pformat, "s");
323             printf(pformat, filename);
324             break;
325 #if !defined(__linux__) && defined (__GNU__)
326         case 'i':
327             strcat(pformat, "Lx");
328             printf(pformat, statfsbuf->f_fsid);
329             break;
330 #else
331         case 'i':
332             strcat(pformat, "x %-8x");
333             printf(pformat, statfsbuf->f_fsid.__val[0], statfsbuf->f_fsid.__val[1]);
334             break;
335 #endif
336
337         case 'l':
338 #ifdef __USE_FILE_OFFSET64
339             strcat(pformat, "lu");
340 #else
341             strcat(pformat, "d");
342 #endif
343             printf(pformat, statfsbuf->f_namelen);
344             break;
345         case 't':
346 #ifdef __USE_FILE_OFFSET64
347             strcat(pformat, "lx");
348 #else
349             strcat(pformat, "x");
350 #endif
351             printf(pformat, statfsbuf->f_type);
352             break;
353         case 'T':
354 /*          print_human_fstype(statfsbuf, pformat);*/
355             print_human_fstype(statfsbuf);
356             break;
357         case 'b':
358 #ifdef __USE_FILE_OFFSET64
359             strcat(pformat, "lld");
360 #else
361 #if !defined(__linux__) && defined (__GNU__)
362             strcat(pformat, "d");
363 #else
364             strcat(pformat, "ld");
365 #endif
366 #endif
367             printf(pformat, statfsbuf->f_blocks);
368             break;
369         case 'f':
370 #ifdef __USE_FILE_OFFSET64
371             strcat(pformat, "lld");
372 #else
373 #if !defined(__linux__) && defined (__GNU__)
374             strcat(pformat, "d");
375 #else
376             strcat(pformat, "ld");
377 #endif
378 #endif
379             printf(pformat, statfsbuf->f_bfree);
380             break;
381         case 'a':
382 #ifdef __USE_FILE_OFFSET64
383             strcat(pformat, "lld");
384 #else
385 #if !defined(__linux__) && defined (__GNU__)
386             strcat(pformat, "d");
387 #else
388             strcat(pformat, "ld");
389 #endif
390 #endif
391             printf(pformat, statfsbuf->f_bavail);
392             break;
393         case 's':
394 #ifdef __USE_FILE_OFFSET64
395             strcat(pformat, "ld");
396 #else
397             strcat(pformat, "d");
398 #endif
399             printf(pformat, statfsbuf->f_bsize);
400             break;
401         case 'c':
402 #ifdef __USE_FILE_OFFSET64
403             strcat(pformat, "lld");
404 #else
405 #if !defined(__linux__) && defined (__GNU__)
406             strcat(pformat, "d");
407 #else
408             strcat(pformat, "ld");
409 #endif
410 #endif
411             printf(pformat, statfsbuf->f_files);
412             break;
413         case 'd':
414 #ifdef __USE_FILE_OFFSET64
415             strcat(pformat, "lld");
416 #else
417 #if !defined(__linux__) && defined (__GNU__)
418             strcat(pformat, "d");
419 #else
420             strcat(pformat, "ld");
421 #endif
422 #endif
423             printf(pformat, statfsbuf->f_ffree);
424             break;
425 #ifdef FLASK_LINUX
426         case 'S':
427             strcat(pformat, "d");
428             printf(pformat, sid);
429             break;
430         case 'C':
431             rv = security_sid_to_context(sid, (security_context_t *) &sbuf, &sbuflen);
432             if ( rv < 0 )
433                 sprintf(sbuf, "<error finding security context %d>", sid);
434             printf(sbuf);
435             break;
436 #endif
437         default:
438             strcat(pformat, "c");
439             printf(pformat, m);
440             break;
441     }
442 }
443
444 /* print stat info */
445 void print_stat(char *pformat, char m, char *filename, void *data, SECURITY_ID_T sid)
446 {
447     char linkname[256];
448     int i;
449     struct stat *statbuf = (struct stat*)data;
450     struct passwd *pw_ent;
451     struct group *gw_ent;
452 #ifdef FLASK_LINUX
453     char sbuf[256];
454     int rv;
455     unsigned int sbuflen = sizeof(sbuf);
456 #endif
457
458     switch(m) {
459         case 'n':
460             strcat(pformat, "s");
461             printf(pformat, filename);
462             break;
463         case 'N':
464             strcat(pformat, "s");
465             if ((statbuf->st_mode & S_IFMT) == S_IFLNK) {
466                 if ((i = readlink(filename, linkname, 256)) == -1) {
467                     perror(filename);
468                     return;
469                 }
470                 linkname[(i >= 256) ? 255 : i] = '\0';
471                 /*printf("\"%s\" -> \"%s\"", filename, linkname);*/
472                 printf("\"");
473                 printf(pformat, filename);
474                 printf("\" -> \"");
475                 printf(pformat, linkname);
476                 printf("\"");
477             } else {
478                 printf("\"");
479                 printf(pformat, filename);
480                 printf("\"");
481             }
482             break;
483         case 'd':
484             strcat(pformat, "d");
485             printf(pformat, (int)statbuf->st_dev);
486             break;
487         case 'D':
488             strcat(pformat, "x");
489             printf(pformat, (int)statbuf->st_dev);
490             break;
491         case 'i':
492             strcat(pformat, "d");
493             printf(pformat, (int)statbuf->st_ino);
494             break;
495         case 'a':
496             strcat(pformat, "o");
497             printf(pformat, statbuf->st_mode & 07777);
498             break;
499         case 'A':
500             print_human_access(statbuf);
501             break;
502         case 'f':
503             strcat(pformat, "x");
504             printf(pformat, statbuf->st_mode);
505             break;
506         case 'F':
507             print_human_type(statbuf->st_mode);
508             break;
509         case 'h':
510             strcat(pformat, "d");
511             printf(pformat, (int)statbuf->st_nlink);
512             break;
513 #ifdef FLASK_LINUX
514         case 'S':
515             strcat(pformat, "d");
516             printf(pformat, sid);
517             break;
518         case 'C':
519             rv = security_sid_to_context(sid, (security_context_t *) &sbuf, &sbuflen);
520             if ( rv < 0 )
521                 sprintf(sbuf, "<error finding security context %d>", sid);
522             printf(sbuf);
523             break;
524 #endif
525         case 'u':
526             strcat(pformat, "d");
527             printf(pformat, statbuf->st_uid);
528             break;
529         case 'U':
530             strcat(pformat, "s");
531             setpwent();
532             pw_ent = getpwuid(statbuf->st_uid);
533             printf(pformat,
534                 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
535             break;
536         case 'g':
537             strcat(pformat, "d");
538             printf(pformat, statbuf->st_gid);
539             break;
540         case 'G':
541             strcat(pformat, "s");
542             setgrent();
543             gw_ent = getgrgid(statbuf->st_gid);
544             printf(pformat,
545                 (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
546             break;
547         case 't':
548             strcat(pformat, "x");
549             printf(pformat, major(statbuf->st_rdev));
550             break;
551         case 'T':
552             strcat(pformat, "x");
553             printf(pformat, minor(statbuf->st_rdev));
554             break;
555         case 's':
556 #ifdef __USE_FILE_OFFSET64
557             strcat(pformat, "llu");
558             printf(pformat, (unsigned long long)statbuf->st_size);
559 #else
560             strcat(pformat, "u");
561             printf(pformat, (unsigned int)statbuf->st_size);
562 #endif
563             break;
564         case 'b':
565             strcat(pformat, "u");
566             printf(pformat, (unsigned int)statbuf->st_blocks);
567             break;
568         case 'o':
569             strcat(pformat, "d");
570             printf(pformat, (int)statbuf->st_blksize);
571             break;
572         case 'x':
573             print_human_time(&(statbuf->st_atime));
574             break;
575         case 'X':
576             strcat(pformat, "d");
577             printf(pformat, (int)statbuf->st_atime);
578             break;
579         case 'y':
580             print_human_time(&(statbuf->st_mtime));
581             break;
582         case 'Y':
583             strcat(pformat, "d");
584             printf(pformat, (int)statbuf->st_mtime);
585             break;
586         case 'z':
587             print_human_time(&(statbuf->st_ctime));
588             break;
589         case 'Z':
590             strcat(pformat, "d");
591             printf(pformat, (int)statbuf->st_ctime);
592             break;
593         default:
594             strcat(pformat, "c");
595             printf(pformat, m);
596             break;
597     }
598 }
599
600 void print_it(char *masterformat, char *filename,
601     void (*print_func)(char*, char, char*, void*, SECURITY_ID_T), void *data, SECURITY_ID_T sid)
602 {
603     char *m, *b, *format;
604     char pformat[65];
605
606     /* create a working copy of the format string */
607     format = strdup(masterformat);
608     if (!format) {
609         perror(filename);
610         return;
611     }
612
613     b = format;
614     while (b)
615     {
616         if ((m = strchr(b, (int)'%')) != NULL)
617         {
618             strcpy (pformat, "%");
619             *m++ = '\0';
620             printf(b);
621
622             /* copy all format specifiers to our format string */
623             while (isdigit(*m) || strchr("#0-+. I", *m))
624             {
625                 char copy[2]="a";
626
627                 *copy = *m;
628                 /* make sure the format specifier is not too long */
629                 if (strlen (pformat) > 63)
630                         fprintf(stderr, "Warning: Format specifier too long, truncating: %s\n", pformat);
631                 else
632                         strcat (pformat, copy);
633                 m++;
634             }
635
636             switch(*m) {
637                 case '\0':
638                 case '%':
639                     printf("%%");
640                     break;
641                 default:
642                     print_func(pformat, *m, filename, data, sid);
643                     break;
644             }
645             b = m + 1;
646         }
647         else
648         {
649             printf(b);
650             b = NULL;
651         }
652     }
653     free(format);
654     printf("\n");
655 }
656
657 /* stat the filesystem and print what we find */
658 void
659 do_statfs (char *filename, int terse, int secure, char *format)
660 {
661   struct statfs statfsbuf;
662   SECURITY_ID_T sid = -1;
663   int i;
664
665 #ifdef FLASK_LINUX
666   if(!is_flask_enabled())
667     secure = 0;
668   if(secure)
669     i = statfs_secure(filename, &statfsbuf, &sid);
670   else
671 #endif
672     i = statfs(filename, &statfsbuf);
673   if(i == -1)
674     {
675       perror (filename);
676       return;
677     }
678
679   if (format == NULL)
680     {
681         if (terse != 0)
682           {
683 #ifdef FLASK_LINUX
684                 if(secure)
685                         format = "%n %i %l %t %b %f %a %s %c %d %S %C";
686                 else
687 #endif
688                         format = "%n %i %l %t %b %f %a %s %c %d";
689           }
690         else
691           {
692 #ifdef FLASK_LINUX
693                 if(secure)
694                     format = "  File: \"%n\"\n"
695                          "    ID: %-8i Namelen: %-7l Type: %T\n"
696                          "Blocks: Total: %-10b Free: %-10f Available: %-10a Size: %s\n"
697                          "Inodes: Total: %-10c Free: %-10d\n"
698                          "   SID: %-14S  S_Context: %C\n";
699                 else
700 #endif
701                     format = "  File: \"%n\"\n"
702                          "    ID: %-8i Namelen: %-7l Type: %T\n"
703                          "Blocks: Total: %-10b Free: %-10f Available: %-10a Size: %s\n"
704                          "Inodes: Total: %-10c Free: %-10d";
705           }
706     }
707
708     print_it(format, filename, print_statfs, &statfsbuf, sid);
709 }
710
711 /* stat the file and print what we find */
712 void
713 do_stat (char *filename, int link, int terse, int secure, char *format)
714 {
715   struct stat statbuf;
716   int i;
717   SECURITY_ID_T sid = -1;
718   
719 #ifdef FLASK_LINUX
720   if(!is_flask_enabled())
721     secure = 0;
722   if(secure)
723     i = (link == 1) ? stat_secure(filename, &statbuf, &sid) : lstat_secure(filename, &statbuf, &sid);
724   else
725 #endif
726   i = (link == 1) ? stat(filename, &statbuf) : lstat(filename, &statbuf);
727
728   if (i == -1)
729     {
730       perror (filename);
731       return;
732     }
733
734    if (format == NULL)
735     {
736        if (terse != 0)
737        {
738 #ifdef FLASK_LINUX
739            if (secure)
740                   format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %S %C";
741            else
742 #endif
743                   format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
744        }
745        else
746        {
747            /* tmp hack to match orignal output until conditional implemented */
748           i = statbuf.st_mode & S_IFMT;
749           if (i == S_IFCHR || i == S_IFBLK) {
750 #ifdef FLASK_LINUX
751                 if (secure)
752                         format =
753                            "  File: %N\n"
754                            "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
755                            "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
756                            " Device type: %t,%T\n"
757                            "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
758                            "   SID: %-14S  S_Context: %C\n"
759                            "Access: %x\n"
760                            "Modify: %y\n"
761                            "Change: %z\n";
762                 else
763 #endif
764                         format =           
765                            "  File: %N\n"
766                            "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
767                            "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
768                            " Device type: %t,%T\n"
769                            "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
770                            "Access: %x\n"
771                            "Modify: %y\n"
772                            "Change: %z\n";
773            }
774            else
775            {
776 #ifdef FLASK_LINUX
777                 if (secure)
778                        format =
779                            "  File: %N\n"
780                            "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
781                            "Device: %Dh/%dd\tInode: %-10i  Links: %-5h\n"
782                            "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
783                            "   SID: %-14S  S_Context: %C\n"
784                            "Access: %x\n"
785                            "Modify: %y\n"
786                            "Change: %z\n";
787                 else
788 #endif
789                       format =
790                            "  File: %N\n"
791                            "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
792                            "Device: %Dh/%dd\tInode: %-10i  Links: %-5h\n"
793                            "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
794                            "Access: %x\n"
795                            "Modify: %y\n"
796                            "Change: %z\n";
797           }
798        }
799     }
800     print_it(format, filename, print_stat, &statbuf, sid);
801 }
802
803 void
804 usage (char *progname)
805 {
806   fprintf (stderr, "Usage: %s [-l] [-f] [-s] [-v] [-h] [-t] [-c format] file1 [file2 ...]\n", progname);
807   exit (1);
808 }
809
810 void verbose_usage(char *progname)
811 {
812     fprintf(stderr, "Usage: %s [-l] [-f] [-s] [-v] [-h] [-t] [-c format] file1 [file2 ...]\n", progname);
813     fprintf(stderr, "\tformat interpreted sequences for file stat are:\n");
814     fprintf(stderr, "\t\t%%n - File name\n");
815     fprintf(stderr, "\t\t%%N - Quoted File name with dereference if symbolic link\n");
816     fprintf(stderr, "\t\t%%d - Device number in decimal\n");
817     fprintf(stderr, "\t\t%%D - Device number in hex\n");
818     fprintf(stderr, "\t\t%%i - Inode number\n");
819     fprintf(stderr, "\t\t%%a - Access rights in octal\n");
820     fprintf(stderr, "\t\t%%A - Access rights in human readable form\n");
821     fprintf(stderr, "\t\t%%f - raw mode in hex\n");
822     fprintf(stderr, "\t\t%%F - File type\n");
823     fprintf(stderr, "\t\t%%h - Number of hard links\n");
824     fprintf(stderr, "\t\t%%u - User Id of owner\n");
825     fprintf(stderr, "\t\t%%U - User name of owner\n");
826     fprintf(stderr, "\t\t%%g - Group Id of owner\n");
827     fprintf(stderr, "\t\t%%G - Group name of owner\n");
828     fprintf(stderr, "\t\t%%t - Major device type in hex\n");
829     fprintf(stderr, "\t\t%%T - Minor device type in hex\n");
830     fprintf(stderr, "\t\t%%s - Total size, in bytes\n");
831     fprintf(stderr, "\t\t%%b - Number of blocks allocated\n");
832     fprintf(stderr, "\t\t%%o - IO block size\n");
833     fprintf(stderr, "\t\t%%x - Time of last access\n");
834     fprintf(stderr, "\t\t%%X - Time of last access as seconds since Epoch\n");
835     fprintf(stderr, "\t\t%%y - Time of last modification\n");
836     fprintf(stderr, "\t\t%%Y - Time of last modification as seconds since Epoch\n");
837     fprintf(stderr, "\t\t%%z - Time of last change\n");
838     fprintf(stderr, "\t\t%%Z - Time of last change as seconds since Epoch\n");
839     fprintf(stderr, "\t\t%%S - Security ID in SE-Linux\n");
840     fprintf(stderr, "\t\t%%C - Security context in SE-Linux\n");
841     fprintf(stderr, "\tformat interpreted sequences for filesystem stat are:\n");
842     fprintf(stderr, "\t\t%%n - File name\n");
843     fprintf(stderr, "\t\t%%i - File System id in hex\n");
844     fprintf(stderr, "\t\t%%l - Maximum length of filenames\n");
845     fprintf(stderr, "\t\t%%t - Type in hex\n");
846     fprintf(stderr, "\t\t%%T - Type in human readable form\n");
847     fprintf(stderr, "\t\t%%b - Total data blocks in file system\n");
848     fprintf(stderr, "\t\t%%f - Free blocks in file system\n");
849     fprintf(stderr, "\t\t%%a - Free blocks available to non-superuser\n");
850     fprintf(stderr, "\t\t%%s - Optimal transfer block size\n");
851     fprintf(stderr, "\t\t%%c - Total file nodes in file system\n");
852     fprintf(stderr, "\t\t%%S - Security ID in SE-Linux\n");
853     fprintf(stderr, "\t\t%%C - Security context in SE-Linux\n");
854     fprintf(stderr, "\t\t%%d - Free file nodes in file system\n");
855     exit(1);
856 }
857   
858
859 int
860 main (int argc, char *argv[])
861 {
862   int c, i, link = 0, fs = 0, terse = 0, secure = 0;
863   char *format = NULL;
864   
865   while ((c = getopt (argc, argv, "lsfvthc:")) != EOF)
866     {
867       switch (c)
868         {
869         case 'l':
870           link = 1;
871           break;
872         case 's':
873           secure = 1;
874           break;
875         case 'f':
876           fs = 1;
877           break;
878         case 't':
879           terse = 1;
880           break;
881         case 'c':
882           format = optarg;
883           break;
884         case 'h':
885           printf ("stat version: 3.0\n");
886           verbose_usage(argv[0]);
887           break;
888         case 'v':
889           printf ("stat version: 3.0\n");
890         default:
891           usage (argv[0]);
892         }
893     }
894   if (argc == optind)
895     usage (argv[0]);
896
897   for (i = optind; i < argc; i++)
898     (fs == 0) ? do_stat (argv[i], link, terse, secure, format) :
899                 do_statfs (argv[i], terse, secure, format);
900
901   return (0);
902 }