aboutsummaryrefslogtreecommitdiff
path: root/toys/other/stat.c
blob: 22b1b8333fd4e739dc888b778b185db3b29577f7 (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
/* stat.c : display file or file system status
 * Copyright 2012 <warior.linux@gmail.com>
 * Copyright 2013 <anand.sinha85@gmail.com>

USE_STAT(NEWTOY(stat, "<1c:(format)fLt", TOYFLAG_BIN))

config STAT
  bool stat
  default y
  help
    usage: stat [-tfL] [-c FORMAT] FILE...

    Display status of files or filesystems.

    -c	Output specified FORMAT string instead of default
    -f	Display filesystem status instead of file status
    -L	Follow symlinks
    -t	terse (-c "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o")
    	      (with -f = -c "%n %i %l %t %s %S %b %f %a %c %d")

    The valid format escape sequences for files:
    %a  Access bits (octal) |%A  Access bits (flags)|%b  Size/512
    %B  Bytes per %b (512)  |%d  Device ID (dec)    |%D  Device ID (hex)
    %f  All mode bits (hex) |%F  File type          |%g  Group ID
    %G  Group name          |%h  Hard links         |%i  Inode
    %m  Mount point         |%n  Filename           |%N  Long filename
    %o  I/O block size      |%s  Size (bytes)       |%t  Devtype major (hex)
    %T  Devtype minor (hex) |%u  User ID            |%U  User name
    %x  Access time         |%X  Access unix time   |%y  Modification time
    %Y  Mod unix time       |%z  Creation time      |%Z  Creation unix time

    The valid format escape sequences for filesystems:
    %a  Available blocks    |%b  Total blocks       |%c  Total inodes
    %d  Free inodes         |%f  Free blocks        |%i  File system ID
    %l  Max filename length |%n  File name          |%s  Fragment size
    %S  Best transfer size  |%t  FS type (hex)      |%T  FS type (driver name)
*/

#define FOR_stat
#include "toys.h"

GLOBALS(
  char *c;

  union {
    struct stat st;
    struct statfs sf;
  } stat;
  char *file, *pattern;
  int patlen;
)

// Force numeric output to long long instead of manually typecasting everything
// and safely parse length prefix
static void out(char c, long long val)
{
  sprintf(toybuf, "%.*sll%c", TT.patlen, TT.pattern, c);
  printf(toybuf, val);
}

// Output string with parsed length prefix
static void strout(char *val)
{
  sprintf(toybuf, "%.*ss", TT.patlen, TT.pattern);
  printf(toybuf, val);
}

static void date_stat_format(struct timespec *ts)
{
  strout(format_iso_time(toybuf+128, sizeof(toybuf)-128, ts));
}

static void print_stat(char type)
{
  struct stat *stat = (struct stat *)&TT.stat;

  if (type == 'a') out('o', stat->st_mode&~S_IFMT);
  else if (type == 'A') {
    char str[11];

    mode_to_string(stat->st_mode, str);
    strout(str);
  } else if (type == 'b') out('u', stat->st_blocks);
  else if (type == 'B') out('d', 512);
  else if (type == 'd') out('d', stat->st_dev);
  else if (type == 'D') out('x', stat->st_dev);
  else if (type == 'f') out('x', stat->st_mode);
  else if (type == 'F') {
    char *t = "character device\0directory\0block device\0" \
              "regular file\0symbolic link\0socket\0FIFO (named pipe)";
    int i, filetype = stat->st_mode & S_IFMT;

    for (i = 1; filetype != (i*8192) && i < 7; i++) t += strlen(t)+1;
    if (!stat->st_size && filetype == S_IFREG) t = "regular empty file";
    strout(t);
  } else if (type == 'g') out('u', stat->st_gid);
  else if (type == 'G') strout(getgroupname(stat->st_gid));
  else if (type == 'h') out('u', stat->st_nlink);
  else if (type == 'i') out('u', stat->st_ino);
  else if (type == 'm') {
    struct mtab_list *mt = xgetmountlist(0);
    dev_t dev = stat->st_rdev ? stat->st_rdev : stat->st_dev;

    // This mount point could exist multiple times, so show oldest.
    for (dlist_terminate(mt); mt; mt = mt->next) if (mt->stat.st_dev == dev) {
      strout(mt->dir);
      break;
    }
    llist_traverse(mt, free);
  } else if (type == 'N') {
    xprintf("%s", TT.file);
    if (S_ISLNK(stat->st_mode))
      if (readlink0(TT.file, toybuf, sizeof(toybuf)))
        xprintf(" -> `%s'", toybuf);
  } else if (type == 'o') out('u', stat->st_blksize);
  else if (type == 's') out('u', stat->st_size);
  else if (type == 't') out('x', dev_major(stat->st_rdev));
  else if (type == 'T') out('x', dev_minor(stat->st_rdev));
  else if (type == 'u') out('u', stat->st_uid);
  else if (type == 'U') strout(getusername(stat->st_uid));
  else if (type == 'x') date_stat_format(&stat->st_atim);
  else if (type == 'X') out('u', stat->st_atime);
  else if (type == 'y') date_stat_format(&stat->st_mtim);
  else if (type == 'Y') out('u', stat->st_mtime);
  else if (type == 'z') date_stat_format(&stat->st_ctim);
  else if (type == 'Z') out('u', stat->st_ctime);
  else xprintf("?");
}

static void print_statfs(char type) {
  struct statfs *statfs = (struct statfs *)&TT.stat;

  if (type == 'a') out('u', statfs->f_bavail);
  else if (type == 'b') out('u', statfs->f_blocks);
  else if (type == 'c') out('u', statfs->f_files);
  else if (type == 'd') out('u', statfs->f_ffree);
  else if (type == 'f') out('u', statfs->f_bfree);
  else if (type == 'l') out('d', statfs->f_namelen);
  else if (type == 't') out('x', statfs->f_type);
  else if (type == 'T') {
    char *s = "unknown";
    struct {unsigned num; char *name;} nn[] = {
      {0xADFF, "affs"}, {0x5346544e, "ntfs"}, {0x1Cd1, "devpts"},
      {0x137D, "ext"}, {0xEF51, "ext2"}, {0xEF53, "ext3"},
      {0x1BADFACE, "bfs"}, {0x9123683E, "btrfs"}, {0x28cd3d45, "cramfs"},
      {0x3153464a, "jfs"}, {0x7275, "romfs"}, {0x01021994, "tmpfs"},
      {0x3434, "nilfs"}, {0x6969, "nfs"}, {0x9fa0, "proc"},
      {0x534F434B, "sockfs"}, {0x62656572, "sysfs"}, {0x517B, "smb"},
      {0x4d44, "msdos"}, {0x4006, "fat"}, {0x43415d53, "smackfs"},
      {0x73717368, "squashfs"}
    };
    int i;

    for (i=0; i<ARRAY_LEN(nn); i++)
      if (nn[i].num == statfs->f_type) s = nn[i].name;
    strout(s);
  } else if (type == 'i') {
    char buf[32];

    sprintf(buf, "%08x%08x", statfs->f_fsid.__val[0], statfs->f_fsid.__val[1]);
    strout(buf);
  } else if (type == 's') out('d', statfs->f_frsize);
  else if (type == 'S') out('d', statfs->f_bsize);
  else strout("?");
}

void stat_main(void)
{
  int flagf = toys.optflags & FLAG_f, i;
  char *format, *f;

  if (toys.optflags&FLAG_t) {
    format = flagf ? "%n %i %l %t %s %S %b %f %a %c %d" :
                     "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
  } else format = flagf
    ? "  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"
    : "  File: %N\n  Size: %s\t Blocks: %b\t IO Blocks: %B\t%F\n"
      "Device: %Dh/%dd\t Inode: %i\t Links: %h\n"
      "Access: (0%a/%A)\tUid: (%5u/%8U)\tGid: (%5g/%8G)\n"
      "Access: %x\nModify: %y\nChange: %z";

  if (toys.optflags & FLAG_c) format = TT.c;

  for (i = 0; toys.optargs[i]; i++) {
    int L = toys.optflags & FLAG_L;

    TT.file = toys.optargs[i];
    if (flagf && !statfs(TT.file, (void *)&TT.stat));
    else if (flagf || (L ? stat : lstat)(TT.file, (void *)&TT.stat)) {
      perror_msg("'%s'", TT.file);
      continue;
    }

    for (f = format; *f; f++) {
      if (*f != '%') putchar(*f);
      else {
        f = next_printf(f, &TT.pattern);
        if (!f) error_exit("bad -c \"%s\"", format);
        TT.patlen = f-TT.pattern;
        if (TT.patlen>99) error_exit("bad %s", TT.pattern);
        if (*f == 'n') strout(TT.file);
        else if (flagf) print_statfs(*f);
        else print_stat(*f);
      }
    }
    xputc('\n');
  }
}