aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/stat.c
blob: 1da373d6e1623c88aa65ec7004533d495de91b18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* stat.c : display file or file system status
 * anand.sinha85@gmail.com
 * Copyright 2012 <warior.linux@gmail.com>

USE_STAT(NEWTOY(stat, "c:f", TOYFLAG_BIN)) 

config STAT
  bool stat
  default n
  help
    usage: stat [-f] [-c FORMAT] FILE...

    display file or file system status

    -f display file system status instead of file status
    -c use the specified FORMAT instead of the default;
       output a newline after each use of FORMAT

    The valid format sequences for files:
    %a     Access rights in octal
    %A     Access rights in human readable form
    %b     Number of blocks allocated
    %B     The size in bytes of each block
    %d     Device number in decimal
    %D     Device number in hex
    %f     Raw mode in hex
    %F     File type
    %g     Group ID of owner
    %G     Group name of owner
    %h     Number of hard links
    %i     Inode number
    %n     File name
    %N     Quoted file name with dereference if symbolic link
    %o     I/O block size
    %s     Total size, in bytes
    %u     User ID of owner
    %U     User name of owner
    %x     Time of last access
    %X     Time of last access as seconds since Epoch
    %y     Time of last modification
    %Y     Time of last modification as seconds since Epoch
    %z     Time of last change
    %Z     Time of last change as seconds since Epoch

    The valid format sequences for file systems:
    %a     Available blocks for unpriviledges user
    %b     Total number of blocks
    %c     Total number of inodes
    %d     Number of free inodes
    %f     Number of free blocks
    %i     File system ID
    %l     Maximum length of file names
    %n     File name
    %s     Fragment size
    %S     Optimal transfer block size
    %t     File system type
*/

#define FOR_stat
#include "toys.h"

GLOBALS(
	char *fmt;
	void *stat;
	char *file_type;
	struct passwd *user_name;
	struct group *group_name;
	char access_str[11];
)


static char * date_stat_format(time_t time)
{
  static char buf[36];

  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&time));
  return buf;
}

static int print_stat(char type) {
  struct stat *stat = (struct stat*)TT.stat;
  switch (type) {
    case 'a':
      xprintf("%04lo", stat->st_mode & ~S_IFMT);
      break;
    case 'A':
      xprintf("%s", TT.access_str);
      break;
    case 'b':
      xprintf("%llu", stat->st_blocks);
      break;
    case 'B':
      xprintf("%lu", stat->st_blksize);
      break;
    case 'd':
      xprintf("%ldd", stat->st_dev);
      break;
    case 'D':
      xprintf("%llxh", stat->st_dev);
      break;
    case 'f':
      xprintf("%lx", stat->st_mode);
      break;
    case 'F':
      xprintf("%s", TT.file_type);
      break;
    case 'g':
      xprintf("%lu", stat->st_gid);
      break;
    case 'G':
      xprintf("%8s", TT.user_name->pw_name);
      break;
    case 'h':
      xprintf("%lu", stat->st_nlink);
      break;
    case 'i':
      xprintf("%llu", stat->st_ino);
      break;
    case 'N':
      xprintf("`%s'", *toys.optargs);
      break;
    case 'o':
      xprintf("%lu", stat->st_blksize);
      break;
    case 's':
      xprintf("%llu", stat->st_size);
      break;
    case 'u':
      xprintf("%lu", stat->st_uid);
      break;
    case 'U':
      xprintf("%8s", TT.user_name->pw_name);
      break;
    case 'x':
      xprintf("%s", date_stat_format(stat->st_atime));
      break;
    case 'X':
      xprintf("%llu", stat->st_atime);
      break;
    case 'y':
      xprintf("%s", date_stat_format(stat->st_mtime));
      break;
    case 'Y':
      xprintf("%llu", stat->st_mtime);
      break;
    case 'z':
      xprintf("%s", date_stat_format(stat->st_ctime));
      break;
    case 'Z':
      xprintf("%llu", stat->st_ctime);
      break;
    default:
      return 1;
  }
  return 0;
}

static int print_statfs(char type) {
  struct statfs *statfs = (struct statfs*)TT.stat;
  switch (type) {
    case 'a':
      xprintf("%lu", statfs->f_bavail);
      break;
    case 'b':
      xprintf("%lu", statfs->f_blocks);
      break;
    case 'c':
      xprintf("%lu", statfs->f_files);
      break;
    case 'd':
      xprintf("%lu", statfs->f_ffree);
      break;
    case 'f':
      xprintf("%lu", statfs->f_bfree);
      break;
    case 'i':
      xprintf("%x%x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
      break;
    case 'l':
      xprintf("%ld", statfs->f_namelen);
      break;
    case 's':
      xprintf("%d", statfs->f_frsize);
      break;
    case 'S':
      xprintf("%d", statfs->f_bsize);
      break;
    case 't':
      xprintf("%lx", statfs->f_type);
      break;
    default:
      return 1;
  }
  return 0;
}

static int do_stat(char *path)
{
  struct stat *statf = (struct stat*)TT.stat;
  size_t i;
  struct {
    mode_t mode;
    char *str;
  } types[] = {
    {S_IFDIR, "directory"},
    {S_IFCHR, "character device"},
    {S_IFBLK, "block device"},
    {S_IFREG, "regular file"},
    {S_IFIFO, "FIFO (named pipe)"},
    {S_IFLNK, "symbolic link"},
    {S_IFSOCK, "socket"}
  };

  if (stat(path, statf) < 0) return 1;

  for (i = 0; i < sizeof(types)/sizeof(*types); i++)
    if ((statf->st_mode & S_IFMT) == types[i].mode) TT.file_type = types[i].str;
  if (!statf->st_size && (statf->st_mode & S_IFMT) == S_IFREG)
    TT.file_type = "regular empty file";

  // check user and group name
  TT.user_name = getpwuid(statf->st_uid);
  TT.group_name = getgrgid(statf->st_gid);
  // function to get access in human readable format
  format_mode(&TT.access_str, statf->st_mode);
  return 0;
}

static int do_statfs(char *path)
{
  return statfs(path, TT.stat) < 0;
}

void stat_main(void)
{
  struct {
    char *fmt;
    int (*do_it)(char*);
    int (*print_it)(char);
    size_t size;
  } d, ds[2] = {
    {"  File: %N\n"
     "  Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
     "Device: %D\t Inode: %i\t Links: %h\n"
     "Access: (%a/%A)\tUid: (%u/%U)\tGid: (%g/%G)\n"
     "Access: %x\nModify: %y\nChange: %z",
     do_stat, print_stat, sizeof(struct stat)},
    {"  File: \"%n\"\n"
     "    ID: %i Namelen: %l    Type: %t\n"
     "Block Size: %s    Fundamental block size: %S\n"
     "Blocks: Total: %b\tFree: %f\tAvailable: %a\n"
     "Inodes: Total: %c\tFree: %d",
     do_statfs, print_statfs, sizeof(struct statfs)}
  };

  d = ds[toys.optflags & FLAG_f];
  TT.stat = xmalloc(d.size);
  if (toys.optflags & FLAG_c) d.fmt = TT.fmt;

  for (; *toys.optargs; toys.optargs++) {
    char *format = d.fmt;
    if (d.do_it(*toys.optargs)) {
      perror_msg("'%s'", *toys.optargs);
      continue;
    }
    for (; *format; format++) {
      if (*format != '%') {
        xputc(*format);
        continue;
      }
      format++;
      if (*format == 'n') xprintf("%s", *toys.optargs);
      else if (d.print_it(*format)) xputc(*format);
    }
    xputc('\n');
  }

  if(CFG_TOYBOX_FREE) free(TT.stat);
}