annotate toys/pending/stat.c @ 872:793972c94560

stat cleanup
author Felix Janda <felix.janda@posteo.de>
date Sat, 20 Apr 2013 00:18:17 +0200
parents 8aa07b575cd6
children beb32d780164
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
1 /* stat.c : display file or file system status
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
2 * anand.sinha85@gmail.com
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
3 * Copyright 2012 <warior.linux@gmail.com>
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
4
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
5 USE_STAT(NEWTOY(stat, "LZc:f", TOYFLAG_BIN))
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
6
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 config STAT
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
8 bool stat
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
9 default n
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
10 help
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
11 Usage: stat [OPTION] FILE...
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
12 display file or file system status
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
13 -Z, --context
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 print the security context information if available
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
15 -f, --file-system
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
16 display file system status instead of file status
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
17 -c --format=FORMAT
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
18 use the specified FORMAT instead of the default; output a newline after each use of FORMAT
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
19 --help display this help and exit
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
20 The valid format sequences for files (without --file-system):
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
21 %a Access rights in octal
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
22 %A Access rights in human readable form
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
23 %b Number of blocks allocated (see
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
24 %B The size in bytes of each block reported by
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
25 %d Device number in decimal
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
26 %D Device number in hex
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
27 %f Raw mode in hex
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
28 %F File type
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
29 %G Group name of owner
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
30 %h Number of hard links
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
31 %i Inode number
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
32 %n File name
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
33 %N Quoted file name with dereference if symbolic link
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
34 %o I/O block size
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
35 %s Total size, in bytes
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
36 %t Major device type in hex
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
37 %T Minor device type in hex
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
38 %u User ID of owner
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
39 %U User name of owner
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
40 %x Time of last access
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
41 %X Time of last access as seconds since Epoch
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
42 %y Time of last modification
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
43 %Y Time of last modification as seconds since Epoch
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
44 %z Time of last change
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
45 %Z Time of last change as seconds since Epoch
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 */
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
47
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 #define FOR_stat
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 #include "toys.h"
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
50
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 GLOBALS(
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
52 char *fmt;
811
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
53 char *access_str;
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
54 char *file_type;
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
55 struct passwd *user_name;
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
56 struct group *group_name;
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
57 struct stat *toystat;
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
58 struct statfs *toystatfs;
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 )
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
60
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
61
811
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
62 static void do_stat(const char * file_name)
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
63 {
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
64 TT.toystat = xmalloc(sizeof(struct stat));
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
65 if (stat(file_name, TT.toystat) < 0) perror_exit("stat: '%s'", file_name);
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
67
811
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
68 static void do_statfs(const char * file_name)
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
69 {
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
70 TT.toystatfs = xmalloc(sizeof(struct statfs));
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
71 if (statfs(file_name, TT.toystatfs) < 0)
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
72 perror_exit("statfs: '%s'", file_name);
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
74
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
75 static char * check_type_file(mode_t mode, size_t size)
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
76 {
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
77 if (S_ISREG(mode)) {
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
78 if (size) return "regular file";
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
79 return "regular empty file";
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
80 }
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
81 if (S_ISDIR(mode)) return "directory";
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
82 if (S_ISCHR(mode)) return "character device";
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
83 if (S_ISBLK(mode)) return "block device";
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
84 if (S_ISFIFO(mode)) return "FIFO (named pipe)";
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
85 if (S_ISLNK(mode)) return "symbolic link";
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
86 if (S_ISSOCK(mode)) return "socket";
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
88
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
89 static char * get_access_str(unsigned long permission, mode_t mode)
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
90 {
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
91 static char access_string[11];
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
92 char *s = access_string;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
93 char *rwx[] = {"---", "--x", "-w-", "-wx",
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
94 "r--", "r-x", "rw-", "rwx"};
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
95
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
96 if (S_ISDIR(mode)) *s = 'd';
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
97 else *s = '-';
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
98
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
99 for (s += 7; s > access_string; s-=3) {
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
100 memcpy(s, rwx[permission & 7], 3);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
101 permission >>= 3;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
102 }
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
103
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
104 access_string[10] = '\0';
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
105 return access_string;
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
107
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
108 static char * date_stat_format(time_t time)
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
109 {
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
110 static char buf[36];
811
7a983e09efad Call stat "stat" instead of "st" in menuconfig. Use xmalloc() instead of malloc.
Rob Landley <rob@landley.net>
parents: 810
diff changeset
111
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
112 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&time));
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
113 return buf;
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
115
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
116 static void print_stat_format(char *format, int flag)
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
117 {
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
118 for (; *format; format++) {
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
119 if (*format != '%') {
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
120 xprintf("%c", *format);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
121 continue;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
122 }
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
123 format++;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
124 switch (*format) {
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
125 case 'a':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
126 if (flag) xprintf("%lu", TT.toystatfs->f_bavail);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
127 else xprintf("%04lo",TT.toystat->st_mode & ~S_IFMT);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
128 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
129 case 'A':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
130 xprintf("%s",TT.access_str);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
131 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
132 case 'b':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
133 if (flag) xprintf("%lu", TT.toystatfs->f_blocks);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
134 else xprintf("%llu", TT.toystat->st_blocks);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
135 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
136 case 'B':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
137 xprintf("%lu", TT.toystat->st_blksize);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
138 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
139 case 'c':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
140 if (flag) xprintf("%lu", TT.toystatfs->f_files);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
141 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
142 case 'C':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
143 xprintf("Currently feature is not supported");
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
144 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
145 case 'd':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
146 if (flag) xprintf("%lu", TT.toystatfs->f_ffree);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
147 else xprintf("%ldd", TT.toystat->st_dev);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
148 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
149 case 'D':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
150 xprintf("%llxh", TT.toystat->st_dev);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
151 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
152 case 'f':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
153 if (flag) xprintf("%lu", TT.toystatfs->f_bfree);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
154 else xprintf("%lx", TT.toystat->st_mode);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
155 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
156 case 'F':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
157 xprintf("%s", TT.file_type);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
158 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
159 case 'g':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
160 xprintf("%lu", TT.toystat->st_uid);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
161 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
162 case 'G':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
163 xprintf("%8s", TT.user_name->pw_name);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
164 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
165 case 'h':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
166 xprintf("%lu", TT.toystat->st_nlink);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
167 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
168 case 'i':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
169 if (flag)
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
170 xprintf("%d%d", TT.toystatfs->f_fsid.__val[0], TT.toystatfs->f_fsid.__val[1]);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
171 else xprintf("%llu", TT.toystat->st_ino);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
172 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
173 case 'l':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
174 if (flag) xprintf("%ld", TT.toystatfs->f_namelen);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
175 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
176 case 'n':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
177 xprintf("%s", *toys.optargs);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
178 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
179 case 'N':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
180 xprintf("`%s'", *toys.optargs);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
181 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
182 case 'o':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
183 xprintf("%lu", TT.toystat->st_blksize);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
184 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
185 case 's':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
186 if (flag) xprintf("%d", TT.toystatfs->f_frsize);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
187 else xprintf("%llu", TT.toystat->st_size);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
188 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
189 case 'S':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
190 if (flag) xprintf("%d", TT.toystatfs->f_bsize);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
191 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
192 case 't':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
193 if (flag) xprintf("%lx", TT.toystatfs->f_type);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
194 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
195 case 'T':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
196 if (flag) xprintf("Needs to be implemented");
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
197 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
198 case 'u':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
199 xprintf("%lu", TT.toystat->st_uid);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
200 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
201 case 'U':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
202 xprintf("%8s", TT.user_name->pw_name);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
203 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
204 case 'x':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
205 xprintf("%s", date_stat_format(TT.toystat->st_atime));
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
206 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
207 case 'X':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
208 xprintf("%llu", TT.toystat->st_atime);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
209 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
210 case 'y':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
211 xprintf("%s", date_stat_format(TT.toystat->st_mtime));
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
212 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
213 case 'Y':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
214 xprintf("%llu", TT.toystat->st_mtime);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
215 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
216 case 'z':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
217 xprintf("%s", date_stat_format(TT.toystat->st_ctime));
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
218 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
219 case 'Z':
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
220 xprintf("%llu", TT.toystat->st_ctime);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
221 default:
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
222 xprintf("%c", *format);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
223 break;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
224 }
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
225 }
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
226 xprintf("\n");
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 }
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
228
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
229 void stat_main(void)
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
230 {
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
231 int flag_f = toys.optflags & FLAG_f, flag_c = toys.optflags & FLAG_c;
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
232 char *fmts[] = {
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
233 " File: %N\n"
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
234 " Size: %s\t Blocks: %S\t IO Blocks: %B\t%F\n"
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
235 "Device: %D\t Inode: %i\t Links: %h\n"
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
236 "Access: (%a/%A)\tUid: (%u/%U)\tGid: (%g/%G)\n"
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
237 "Access: %x\nModify: %y\nChange: %z",
810
874d2e646f2d Fix whitespace in submitted stat command.
Rob Landley <rob@landley.net>
parents: 747
diff changeset
238
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
239 " File: \"%n\"\n"
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
240 " ID: %i Namelen: %l Type: %t\n"
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
241 "Block Size: %s Fundamental block size: %S\n"
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
242 "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
243 "Inodes: Total: %c\tFree: %d",
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
244 TT.fmt};
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
245
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
246 if (toys.optflags & FLAG_Z) error_exit("SELinux feature has not been implemented so far..");
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
247 if (!flag_f) {
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
248 do_stat(*toys.optargs);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
249 // function to check the type/mode of file
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
250 TT.file_type = check_type_file(TT.toystat->st_mode, TT.toystat->st_size);
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
251 // check user and group name
871
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
252 TT.user_name = getpwuid(TT.toystat->st_uid);
8aa07b575cd6 stat: Reindent from 4 to 2 spaces
Felix Janda <felix.janda@posteo.de>
parents: 811
diff changeset
253 TT.group_name = getgrgid(TT.toystat->st_gid);
872
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
254 // function to get access in human readable format
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
255 TT.access_str = get_access_str(TT.toystat->st_mode & ~S_IFMT, TT.toystat->st_mode);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
256 } else do_statfs(*toys.optargs);
793972c94560 stat cleanup
Felix Janda <felix.janda@posteo.de>
parents: 871
diff changeset
257 print_stat_format(fmts[!flag_c*flag_f+flag_c], flag_f);
747
68d6c1ce7bba Add stat submission to new "pending" directory, along with infrastructure to support pending.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 }