comparison toys/pending/stat.c @ 810:874d2e646f2d

Fix whitespace in submitted stat command.
author Rob Landley <rob@landley.net>
date Tue, 05 Mar 2013 02:48:12 -0600
parents 68d6c1ce7bba
children 7a983e09efad
comparison
equal deleted inserted replaced
809:f753e5192cbb 810:874d2e646f2d
1 /* vi: set sw=4 ts=4 1 /* stat.c : display file or file system status
2 * 2 * anand.sinha85@gmail.com
3 * stat.c : display file or file system status 3 * Copyright 2012 <warior.linux@gmail.com>
4 * anand.sinha85@gmail.com 4 *
5 * Copyright 2012 <warior.linux@gmail.com> 5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/stat.html
6 * 6
7 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/stat.html
8 USE_STAT(NEWTOY(stat, "LZfc", TOYFLAG_BIN)) 7 USE_STAT(NEWTOY(stat, "LZfc", TOYFLAG_BIN))
8
9 config STAT 9 config STAT
10 bool st 10 bool st
11 default n 11 default n
12 help 12 help
13 Usage: stat [OPTION] FILE... 13 Usage: stat [OPTION] FILE...
47 %Z Time of last change as seconds since Epoch 47 %Z Time of last change as seconds since Epoch
48 */ 48 */
49 49
50 #define FOR_stat 50 #define FOR_stat
51 #include "toys.h" 51 #include "toys.h"
52
52 #define SIZE_DATE_TIME_STAT 36 53 #define SIZE_DATE_TIME_STAT 36
53 #define access_string(x, s, i) if((x&7) & 1) \ 54 #define access_string(x, s, i) if((x&7) & 1) \
54 s[9 - i * 3] = 'x'; \ 55 s[9 - i * 3] = 'x'; \
55 else \ 56 else \
56 s[9 - i * 3] = '-'; \ 57 s[9 - i * 3] = '-'; \
78 struct statfs * toystatfs; 79 struct statfs * toystatfs;
79 int toy_obj_file_arg; 80 int toy_obj_file_arg;
80 ) 81 )
81 82
82 83
83 static int do_stat(const char * file_name){ 84 static int do_stat(const char * file_name)
85 {
84 TT.toystat = (struct stat*)malloc(sizeof(struct stat)); 86 TT.toystat = (struct stat*)malloc(sizeof(struct stat));
85 if(stat(file_name, TT.toystat) < 0){ 87 if(stat(file_name, TT.toystat) < 0){
86 perror_msg("Error: unable to get information about the file, stat\n", file_name); 88 perror_msg("Error: unable to get information about the file, stat\n", file_name);
87 toys.exitval = EXIT_FAILURE; 89 toys.exitval = EXIT_FAILURE;
88 } 90 }
89 return 0; 91 return 0;
90 } 92 }
91 93
92 static int do_statfs(const char * file_name){ 94 static int do_statfs(const char * file_name)
95 {
93 TT.toystatfs = (struct statfs *)malloc(sizeof(struct statfs)); 96 TT.toystatfs = (struct statfs *)malloc(sizeof(struct statfs));
94 if(statfs(file_name, TT.toystatfs) < 0){ 97 if (statfs(file_name, TT.toystatfs) < 0) {
95 perror_msg("Error: unable to get information about the file, statfs\n", file_name); 98 perror_msg("Error: unable to get information about the file, statfs\n", file_name);
96 toys.exitval = EXIT_FAILURE; 99 toys.exitval = EXIT_FAILURE;
97 } 100 }
98 return 0; 101 return 0;
99 } 102 }
100 103
101 static char * check_type_file(mode_t mode, size_t size){ 104 static char * check_type_file(mode_t mode, size_t size)
105 {
102 if(S_ISREG(mode)){ 106 if(S_ISREG(mode)){
103 if(size) 107 if (size) return "regular file";
104 return "regular file"; 108 return "regular empty file";
105 return "regular empty file"; 109 }
106 } 110 if(S_ISDIR(mode)) return "directory";
107 if(S_ISDIR(mode)) 111 if(S_ISCHR(mode)) return "character device";
108 return "directory"; 112 if(S_ISBLK(mode)) return "block device";
109 if(S_ISCHR(mode)) 113 if(S_ISFIFO(mode)) return "FIFO (named pipe)";
110 return "character device"; 114 if(S_ISLNK(mode)) return "symbolic link";
111 if(S_ISBLK(mode)) 115 if(S_ISSOCK(mode)) return "socket";
112 return "block device"; 116 }
113 if(S_ISFIFO(mode)) 117
114 return "FIFO (named pipe)"; 118 static char * get_access_str(unsigned long pernission, mode_t mode)
115 if(S_ISLNK(mode)) 119 {
116 return "symbolic link";
117 if(S_ISSOCK(mode))
118 return "socket";
119 }
120
121 static char * get_access_str(unsigned long pernission, mode_t mode){
122 static char access_string[10]; 120 static char access_string[10];
123 int i; 121 int i;
124 if(S_ISDIR(mode)) 122
125 access_string[0] = 'd'; 123 if (S_ISDIR(mode)) access_string[0] = 'd';
126 else 124 else access_string[0] = '-';
127 access_string[0] = '-'; 125 for(i = 0; i < 3; i++)
128 for(i = 0; i < 3; i++){
129 access_string(pernission >> (i * 3) & 7, access_string, i); 126 access_string(pernission >> (i * 3) & 7, access_string, i);
130 } 127
131 access_string[10] = '\0'; 128 access_string[10] = '\0';
132 return access_string; 129 return access_string;
133 } 130 }
134 131
135 static char * date_stat_format(time_t time){ 132 static char * date_stat_format(time_t time)
133 {
136 static char buf[SIZE_DATE_TIME_STAT]; 134 static char buf[SIZE_DATE_TIME_STAT];
137 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&time)); 135 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&time));
138 return buf; 136 return buf;
139 } 137 }
140 138
141 inline void print_stat_format(char *format, int flag){ 139 inline void print_stat_format(char *format, int flag)
140 {
142 format++; 141 format++;
143 switch(*format){ 142 switch(*format) {
144 case 'a': 143 case 'a':
145 if(flag) 144 if(flag)
146 xprintf("%lu\n", TT.toystatfs->f_bavail); 145 xprintf("%lu\n", TT.toystatfs->f_bavail);
147 else 146 else
148 xprintf("%04lo\n",TT.toystat->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)); 147 xprintf("%04lo\n",TT.toystat->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO));
258 break; 257 break;
259 } 258 }
260 exit(0); 259 exit(0);
261 } 260 }
262 261
263 void stat_main(void){ 262 void stat_main(void)
263 {
264 int stat_flag_Z = 0, stat_flag_f = 0, stat_flag_c = 0, stat_format = 0; 264 int stat_flag_Z = 0, stat_flag_f = 0, stat_flag_c = 0, stat_format = 0;
265 if(toys.optargs){ 265
266 if(toys.optflags & 1){ 266 if (toys.optargs) {
267 if (toys.optflags & 1) {
267 stat_flag_c = 1; 268 stat_flag_c = 1;
268 TT.toy_obj_file_arg = 1; 269 TT.toy_obj_file_arg = 1;
269 stat_format = 1; 270 stat_format = 1;
270 } 271 }
271 if(toys.optflags & (1 << 1)){ 272 if(toys.optflags & (1 << 1)) {
272 stat_flag_f = 1; 273 stat_flag_f = 1;
273 if(do_statfs(toys.optargs[TT.toy_obj_file_arg]) != 0) 274 if (do_statfs(toys.optargs[TT.toy_obj_file_arg]) != 0)
274 xprintf("Error STATFS\n"); 275 xprintf("Error STATFS\n");
275 }else 276 } else if (do_stat(toys.optargs[TT.toy_obj_file_arg]) != 0)
276 if(do_stat(toys.optargs[TT.toy_obj_file_arg]) != 0)
277 xprintf("Error STAT\n"); 277 xprintf("Error STAT\n");
278 if(toys.optflags & (1 << 2)){ 278 if (toys.optflags & (1 << 2)) {
279 stat_flag_Z = 1; 279 stat_flag_Z = 1;
280 xprintf("SELinux feature has not been implemented so far..\n"); 280 xprintf("SELinux feature has not been implemented so far..\n");
281 } 281 }
282 } 282 }
283 // function to check the type/mode of file 283 // function to check the type/mode of file
284 if(!stat_flag_f){ 284 if(!stat_flag_f) {
285 TT.file_type = check_type_file(TT.toystat->st_mode, TT.toystat->st_size); 285 TT.file_type = check_type_file(TT.toystat->st_mode, TT.toystat->st_size);
286 // check user and group name 286 // check user and group name
287 TT.user_name = getpwuid(TT.toystat->st_uid); 287 TT.user_name = getpwuid(TT.toystat->st_uid);
288 TT.group_name = getgrgid(TT.toystat->st_gid); 288 TT.group_name = getgrgid(TT.toystat->st_gid);
289 // function to get access in human readable format 289 // function to get access in human readable format
290 TT.access_str = get_access_str((TT.toystat->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)), TT.toystat->st_mode); 290 TT.access_str = get_access_str((TT.toystat->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)), TT.toystat->st_mode);
291 TT.time_toy = gmtime(&(TT.toystat->st_atime)); 291 TT.time_toy = gmtime(&(TT.toystat->st_atime));
292 } 292 }
293 if(!(stat_flag_f |stat_flag_Z)){ 293 if (!(stat_flag_f |stat_flag_Z)) {
294 if(stat_format) 294 if(stat_format) print_stat_format(toys.optargs[0], stat_flag_f);
295 print_stat_format(toys.optargs[0], stat_flag_f);
296 xprintf(" File: `%s'\n", toys.optargs[TT.toy_obj_file_arg]); 295 xprintf(" File: `%s'\n", toys.optargs[TT.toy_obj_file_arg]);
297 xprintf(" Size: %llu\t Blocks: %llu\t IO Blocks: %lu\t", TT.toystat->st_size, TT.toystat->st_blocks, TT.toystat->st_blksize); 296 xprintf(" Size: %llu\t Blocks: %llu\t IO Blocks: %lu\t", TT.toystat->st_size, TT.toystat->st_blocks, TT.toystat->st_blksize);
298 xprintf("%s\n", TT.file_type); 297 xprintf("%s\n", TT.file_type);
299 xprintf("Device: %llxh\t Inode: %llu\t Links: %lu\n", TT.toystat->st_dev, TT.toystat->st_ino, TT.toystat->st_nlink); 298 xprintf("Device: %llxh\t Inode: %llu\t Links: %lu\n", TT.toystat->st_dev, TT.toystat->st_ino, TT.toystat->st_nlink);
300 xprintf("Access: (%04lo/%s)\tUid: (%lu/%8s)\tGid: (%lu/%8s)\n", (TT.toystat->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)), TT.access_str, TT.toystat->st_uid, TT.user_name->pw_name, TT.toystat->st_gid, TT.group_name->gr_name); 299 xprintf("Access: (%04lo/%s)\tUid: (%lu/%8s)\tGid: (%lu/%8s)\n", (TT.toystat->st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)), TT.access_str, TT.toystat->st_uid, TT.user_name->pw_name, TT.toystat->st_gid, TT.group_name->gr_name);
301 xprintf("Access: %s\nModify: %s\nChange: %s\n", date_stat_format(TT.toystat->st_atime), date_stat_format(TT.toystat->st_mtime), date_stat_format(TT.toystat->st_ctime)); 300 xprintf("Access: %s\nModify: %s\nChange: %s\n", date_stat_format(TT.toystat->st_atime), date_stat_format(TT.toystat->st_mtime), date_stat_format(TT.toystat->st_ctime));
302 }else if(stat_flag_f){ 301 } else if (stat_flag_f) {
303 // implementation of statfs -f, file system 302 // implementation of statfs -f, file system
304 if(stat_format) 303 if (stat_format) print_stat_format(toys.optargs[0], stat_flag_f);
305 print_stat_format(toys.optargs[0], stat_flag_f);
306 xprintf(" File: \"%s\"\n", toys.optargs[TT.toy_obj_file_arg]); 304 xprintf(" File: \"%s\"\n", toys.optargs[TT.toy_obj_file_arg]);
307 xprintf(" ID: %d%d Namelen: %ld Type: %lx\n", TT.toystatfs->f_fsid.__val[0], TT.toystatfs->f_fsid.__val[1], TT.toystatfs->f_namelen, TT.toystatfs->f_type); 305 xprintf(" ID: %d%d Namelen: %ld Type: %lx\n", TT.toystatfs->f_fsid.__val[0], TT.toystatfs->f_fsid.__val[1], TT.toystatfs->f_namelen, TT.toystatfs->f_type);
308 xprintf("Block Size: %d Fundamental block size: %d\n", TT.toystatfs->f_bsize, TT.toystatfs->f_frsize); 306 xprintf("Block Size: %d Fundamental block size: %d\n", TT.toystatfs->f_bsize, TT.toystatfs->f_frsize);
309 xprintf("Blocks: Total: %lu\t", TT.toystatfs->f_blocks); 307 xprintf("Blocks: Total: %lu\t", TT.toystatfs->f_blocks);
310 xprintf("Free: %lu\t", TT.toystatfs->f_bfree); 308 xprintf("Free: %lu\t", TT.toystatfs->f_bfree);