| [9] | 1 | /* stat.c Feb 1987 - main, printit, statit
|
|---|
| 2 | *
|
|---|
| 3 | * stat - a program to perform what the stat(2) call does.
|
|---|
| 4 | *
|
|---|
| 5 | * usage: stat [-] [-all] -<field> [-<field> ...] [file1 file2 file3 ...]
|
|---|
| 6 | *
|
|---|
| 7 | * where <field> is one of the struct stat fields without the leading "st_".
|
|---|
| 8 | * The three times can be printed out as human times by requesting
|
|---|
| 9 | * -Ctime instead of -ctime (upper case 1st letter).
|
|---|
| 10 | * - means take the file names from stdin.
|
|---|
| 11 | * -0.. means fd0..
|
|---|
| 12 | * no files means all fds.
|
|---|
| 13 | *
|
|---|
| 14 | * output: if only one field is specified, that fields' contents are printed.
|
|---|
| 15 | * if more than one field is specified, the output is
|
|---|
| 16 | * file filed1: f1val, field2: f2val, etc
|
|---|
| 17 | *
|
|---|
| 18 | * written: Larry McVoy, (mcvoy@rsch.wisc.edu)
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | # define ALLDEF /* Make -all default. (kjb) */
|
|---|
| 22 |
|
|---|
| 23 | # include <sys/types.h>
|
|---|
| 24 | # include <errno.h>
|
|---|
| 25 | # include <limits.h>
|
|---|
| 26 | # include <stdio.h>
|
|---|
| 27 | # include <stdlib.h>
|
|---|
| 28 | # include <unistd.h>
|
|---|
| 29 | # include <string.h>
|
|---|
| 30 | # include <time.h>
|
|---|
| 31 | # include <sys/stat.h>
|
|---|
| 32 | # define addr(x) ((void*) &sbuf.x)
|
|---|
| 33 | # define size(x) sizeof(sbuf.x)
|
|---|
| 34 | # define equal(s, t) (strcmp(s, t) == 0)
|
|---|
| 35 | # ifndef PATH_MAX
|
|---|
| 36 | # define PATH_MAX 1024
|
|---|
| 37 | # endif
|
|---|
| 38 | # undef LS_ADDS_SPACE /* AT&T Unix PC, ls prints "file[* /]" */
|
|---|
| 39 | /* This makes stat fail. */
|
|---|
| 40 |
|
|---|
| 41 | # ifndef _MINIX /* All but Minix have u_* and st_blocks */
|
|---|
| 42 | # define BSD
|
|---|
| 43 | # endif
|
|---|
| 44 |
|
|---|
| 45 | # ifndef S_IREAD
|
|---|
| 46 | # define S_IREAD S_IRUSR
|
|---|
| 47 | # define S_IWRITE S_IWUSR
|
|---|
| 48 | # define S_IEXEC S_IXUSR
|
|---|
| 49 | # endif
|
|---|
| 50 |
|
|---|
| 51 | char * arg0;
|
|---|
| 52 | struct stat sbuf;
|
|---|
| 53 | extern int errno;
|
|---|
| 54 | int first_file= 1;
|
|---|
| 55 | #ifndef S_IFLNK
|
|---|
| 56 | #define lstat stat
|
|---|
| 57 | #endif
|
|---|
| 58 |
|
|---|
| 59 | struct field {
|
|---|
| 60 | char* f_name; /* field name in stat */
|
|---|
| 61 | u_char* f_addr; /* address of the field in sbuf */
|
|---|
| 62 | u_short f_size; /* size of the object, needed for pointer arith */
|
|---|
| 63 | u_short f_print; /* show this field? */
|
|---|
| 64 | } fields[] = {
|
|---|
| 65 | { "dev", addr(st_dev), size(st_dev), 0 },
|
|---|
| 66 | { "ino", addr(st_ino), size(st_ino), 0 },
|
|---|
| 67 | { "mode", addr(st_mode), size(st_mode), 0 },
|
|---|
| 68 | { "nlink", addr(st_nlink), size(st_nlink), 0 },
|
|---|
| 69 | { "uid", addr(st_uid), size(st_uid), 0 },
|
|---|
| 70 | { "gid", addr(st_gid), size(st_gid), 0 },
|
|---|
| 71 | { "rdev", addr(st_rdev), size(st_rdev), 0 },
|
|---|
| 72 | { "size", addr(st_size), size(st_size), 0 },
|
|---|
| 73 | { "Atime", addr(st_atime), size(st_atime), 0 },
|
|---|
| 74 | { "atime", addr(st_atime), size(st_atime), 0 },
|
|---|
| 75 | { "Mtime", addr(st_mtime), size(st_mtime), 0 },
|
|---|
| 76 | { "mtime", addr(st_mtime), size(st_mtime), 0 },
|
|---|
| 77 | { "Ctime", addr(st_ctime), size(st_ctime), 0 },
|
|---|
| 78 | { "ctime", addr(st_ctime), size(st_ctime), 0 },
|
|---|
| 79 | # ifdef BSD
|
|---|
| 80 | { "blksize", addr(st_blksize), size(st_blksize), 0 },
|
|---|
| 81 | { "blocks", addr(st_blocks), size(st_blocks), 0 },
|
|---|
| 82 | # endif
|
|---|
| 83 | { NULL, 0, 0, 0 },
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | void printstat(struct stat *sbuf, int nprint);
|
|---|
| 87 | void printit(struct stat* sb, struct field* f, int n);
|
|---|
| 88 | void rwx(mode_t mode, char *bit);
|
|---|
| 89 | void usage(void);
|
|---|
| 90 |
|
|---|
| 91 | int do_readlink=0;
|
|---|
| 92 |
|
|---|
| 93 | int main(int ac, char** av)
|
|---|
| 94 | {
|
|---|
| 95 | int i, j, nprint = 0, files = 0;
|
|---|
| 96 | char buf[PATH_MAX], *check;
|
|---|
| 97 | int sym=0, ret=0, from_stdin = 0;
|
|---|
| 98 | int err;
|
|---|
| 99 | u_long fd;
|
|---|
| 100 |
|
|---|
| 101 | if ((arg0 = strrchr(av[0], '/')) == NULL) arg0 = av[0]; else arg0++;
|
|---|
| 102 | #ifdef S_IFLNK
|
|---|
| 103 | if (equal(arg0, "lstat")) sym = 1;
|
|---|
| 104 | if (equal(arg0, "readlink")) do_readlink = 1;
|
|---|
| 105 | #endif
|
|---|
| 106 |
|
|---|
| 107 | if (ac > 1 && equal(av[i = 1], "-"))
|
|---|
| 108 | i++, from_stdin++;
|
|---|
| 109 |
|
|---|
| 110 | for (i= 1; i<ac; i++) {
|
|---|
| 111 | if (av[i][0] == '-') {
|
|---|
| 112 | if (equal(av[i], "-")) {
|
|---|
| 113 | from_stdin= 1;
|
|---|
| 114 | files++;
|
|---|
| 115 | continue;
|
|---|
| 116 | }
|
|---|
| 117 | if (equal("-all", av[i])) {
|
|---|
| 118 | for (j=0; fields[j].f_name; j++)
|
|---|
| 119 | nprint++, fields[j].f_print++;
|
|---|
| 120 | continue;
|
|---|
| 121 | }
|
|---|
| 122 | if (equal("-s", av[i])) {
|
|---|
| 123 | #ifdef S_IFLNK
|
|---|
| 124 | sym=1;
|
|---|
| 125 | #endif
|
|---|
| 126 | continue;
|
|---|
| 127 | }
|
|---|
| 128 | fd = strtoul(av[i]+1, &check, 0);
|
|---|
| 129 | if (check != av[i]+1 && *check == '\0')
|
|---|
| 130 | {
|
|---|
| 131 | files++;
|
|---|
| 132 | continue;
|
|---|
| 133 | }
|
|---|
| 134 | for (j=0; fields[j].f_name; j++)
|
|---|
| 135 | if (equal(fields[j].f_name, &av[i][1])) {
|
|---|
| 136 | nprint++, fields[j].f_print++;
|
|---|
| 137 | break;
|
|---|
| 138 | }
|
|---|
| 139 | if (!fields[j].f_name) {
|
|---|
| 140 | if (!equal("-?", av[i])) {
|
|---|
| 141 | fprintf(stderr, "stat: %s: bad field\n", av[i]);
|
|---|
| 142 | }
|
|---|
| 143 | usage();
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | else
|
|---|
| 147 | files++;
|
|---|
| 148 | }
|
|---|
| 149 | if (!nprint) {
|
|---|
| 150 | # ifndef ALLDEF
|
|---|
| 151 | usage();
|
|---|
| 152 | # else
|
|---|
| 153 | for (j=0; fields[j].f_name; j++)
|
|---|
| 154 | nprint++, fields[j].f_print++;
|
|---|
| 155 | # endif
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | if (from_stdin)
|
|---|
| 159 | files++; /* We don't know how many files come from stdin. */
|
|---|
| 160 |
|
|---|
| 161 | if (files == 0) { /* Stat all file descriptors. */
|
|---|
| 162 | if(do_readlink) return 0;
|
|---|
| 163 | for (i= 0; i<OPEN_MAX; i++) {
|
|---|
| 164 | err= fstat(i, &sbuf);
|
|---|
| 165 | if (err == -1 && errno == EBADF)
|
|---|
| 166 | continue;
|
|---|
| 167 | if (err == 0) {
|
|---|
| 168 | if (!first_file) fputc('\n', stdout);
|
|---|
| 169 | printf("fd %d:\n", i);
|
|---|
| 170 | printstat(&sbuf, nprint);
|
|---|
| 171 | }
|
|---|
| 172 | else {
|
|---|
| 173 | fprintf(stderr, "%s: fd %d: %s\n", arg0, i, strerror(errno));
|
|---|
| 174 | ret= 1;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | exit(ret);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | for (i=1; i<ac; i++) {
|
|---|
| 181 | if (equal(av[i], "-")) {
|
|---|
| 182 | while (fgets(buf, sizeof(buf), stdin)) {
|
|---|
| 183 | char *p= strchr(buf, '\n');
|
|---|
| 184 | if (p) *p= 0;
|
|---|
| 185 | #ifdef S_IFLNK
|
|---|
| 186 | if(do_readlink) {
|
|---|
| 187 | char sbuf[300];
|
|---|
| 188 | int n;
|
|---|
| 189 | if((n=readlink(buf, sbuf, sizeof(sbuf)-1)) < 0) {
|
|---|
| 190 | perror(buf);
|
|---|
| 191 | continue;
|
|---|
| 192 | }
|
|---|
| 193 | sbuf[n] = '\0';
|
|---|
| 194 | printf("%s: %s\n", buf, sbuf);
|
|---|
| 195 | continue;
|
|---|
| 196 | }
|
|---|
| 197 | #endif
|
|---|
| 198 | if (!sym) err= stat(av[i], &sbuf);
|
|---|
| 199 | if (sym || (err != 0 && errno == ENOENT)) {
|
|---|
| 200 | err= lstat(av[i], &sbuf);
|
|---|
| 201 | }
|
|---|
| 202 | if (err == -1) {
|
|---|
| 203 | fprintf(stderr, "%s: %s: %s\n",
|
|---|
| 204 | arg0, av[i], strerror(errno));
|
|---|
| 205 | ret= 1;
|
|---|
| 206 | }
|
|---|
| 207 | else {
|
|---|
| 208 | if (!first_file) fputc('\n', stdout);
|
|---|
| 209 | printf("%s:\n", buf);
|
|---|
| 210 | printstat(&sbuf, nprint);
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 | continue;
|
|---|
| 214 | }
|
|---|
| 215 | if (av[i][0] == '-') {
|
|---|
| 216 | fd= strtoul(av[i]+1, &check, 10);
|
|---|
| 217 | if (check == av[i]+1 || *check != '\0') continue;
|
|---|
| 218 | if (fd >= INT_MAX) {
|
|---|
| 219 | err= -1;
|
|---|
| 220 | errno= EBADF;
|
|---|
| 221 | }
|
|---|
| 222 | else {
|
|---|
| 223 | err= fstat((int) fd, &sbuf);
|
|---|
| 224 | }
|
|---|
| 225 | if (err != -1) {
|
|---|
| 226 | if (!first_file) fputc('\n', stdout);
|
|---|
| 227 | if (files != 1) printf("fd %lu:\n", fd);
|
|---|
| 228 | printstat(&sbuf, nprint);
|
|---|
| 229 | }
|
|---|
| 230 | else {
|
|---|
| 231 | fprintf(stderr, "fd %lu: %s\n", fd, strerror(errno));
|
|---|
| 232 | ret= 1;
|
|---|
| 233 | }
|
|---|
| 234 | continue;
|
|---|
| 235 | }
|
|---|
| 236 | if(do_readlink) {
|
|---|
| 237 | char sbuf[300];
|
|---|
| 238 | int n;
|
|---|
| 239 | if((n=err=readlink(av[i], sbuf, sizeof(sbuf)-1)) < 0) {
|
|---|
| 240 | perror(av[i]);
|
|---|
| 241 | continue;
|
|---|
| 242 | }
|
|---|
| 243 | sbuf[n] = '\0';
|
|---|
| 244 | printf("%s: %s\n", av[i], sbuf);
|
|---|
| 245 | continue;
|
|---|
| 246 | }
|
|---|
| 247 | if (!sym) err= stat(av[i], &sbuf);
|
|---|
| 248 | if (sym || (err != 0 && errno == ENOENT)) err= lstat(av[i], &sbuf);
|
|---|
| 249 | if (err != -1) {
|
|---|
| 250 | if (!first_file) fputc('\n', stdout);
|
|---|
| 251 | if (files != 1) printf("%s:\n", av[i]);
|
|---|
| 252 | printstat(&sbuf, nprint);
|
|---|
| 253 | }
|
|---|
| 254 | else {
|
|---|
| 255 | fprintf(stderr, "%s: %s: %s\n", arg0, av[i], strerror(errno));
|
|---|
| 256 | ret= 1;
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | exit(ret);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /*------------------------------------------------------------------30/Jan/87-*
|
|---|
| 263 | * printstat(file, nprint) - do the work
|
|---|
| 264 | *----------------------------------------------------------------larry mcvoy-*/
|
|---|
| 265 | void printstat(struct stat *sbuf, int nprint)
|
|---|
| 266 | {
|
|---|
| 267 | int j;
|
|---|
| 268 | int first_field= 1;
|
|---|
| 269 |
|
|---|
| 270 | for (j=0; fields[j].f_name; j++) {
|
|---|
| 271 | if (fields[j].f_print) {
|
|---|
| 272 | if (!first_field) fputc('\n', stdout);
|
|---|
| 273 | printit(sbuf, &fields[j], nprint);
|
|---|
| 274 | first_field= 0;
|
|---|
| 275 | }
|
|---|
| 276 | }
|
|---|
| 277 | fputc('\n', stdout);
|
|---|
| 278 | first_file= 0;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /*------------------------------------------------------------------30/Jan/87-*
|
|---|
| 282 | * printit(sb, f, n) - print the field
|
|---|
| 283 | *
|
|---|
| 284 | * Inputs -> (struct stat*), (struct field*), (int)
|
|---|
| 285 | *
|
|---|
| 286 | * Results -> Displays the field, with special handling of weird fields like
|
|---|
| 287 | * mode and mtime. The mode field is dumped in octal, followed
|
|---|
| 288 | * by one or more of the S_IF<X> and/or S_I<X> values.
|
|---|
| 289 | *----------------------------------------------------------------larry mcvoy-*/
|
|---|
| 290 | void printit(struct stat* sb, struct field* f, int n)
|
|---|
| 291 | {
|
|---|
| 292 | if (n > 1)
|
|---|
| 293 | printf("%s: ", f->f_name);
|
|---|
| 294 | if (equal(f->f_name, "mode")) {
|
|---|
| 295 | /* This lot changed to my personal liking. (kjb) */
|
|---|
| 296 | char bit[11];
|
|---|
| 297 |
|
|---|
| 298 | printf("%07lo, ", (u_long) sb->st_mode);
|
|---|
| 299 |
|
|---|
| 300 | strcpy(bit, "----------");
|
|---|
| 301 |
|
|---|
| 302 | switch (sb->st_mode&S_IFMT) {
|
|---|
| 303 | case S_IFDIR: bit[0]='d'; break;
|
|---|
| 304 | # ifdef S_IFFIFO
|
|---|
| 305 | case S_IFFIFO: bit[0]='p'; break;
|
|---|
| 306 | # endif
|
|---|
| 307 | case S_IFCHR: bit[0]='c'; break;
|
|---|
| 308 | case S_IFBLK: bit[0]='b'; break;
|
|---|
| 309 | # ifdef S_IFSOCK
|
|---|
| 310 | case S_IFSOCK: bit[0]='S'; break;
|
|---|
| 311 | # endif
|
|---|
| 312 | # ifdef S_IFMPC
|
|---|
| 313 | case S_IFMPC: bit[0]='C'; break;
|
|---|
| 314 | # endif
|
|---|
| 315 | # ifdef S_IFMPB
|
|---|
| 316 | case S_IFMPB: bit[0]='B'; break;
|
|---|
| 317 | # endif
|
|---|
| 318 | # ifdef S_IFLNK
|
|---|
| 319 | case S_IFLNK: bit[0]='l'; break;
|
|---|
| 320 | # endif
|
|---|
| 321 | }
|
|---|
| 322 | rwx(sb->st_mode, bit+1);
|
|---|
| 323 | rwx(sb->st_mode<<3, bit+4);
|
|---|
| 324 | rwx(sb->st_mode<<6, bit+7);
|
|---|
| 325 | if (sb->st_mode&S_ISUID) bit[3]='s';
|
|---|
| 326 | if (sb->st_mode&S_ISGID) bit[6]='s';
|
|---|
| 327 | if (sb->st_mode&S_ISVTX) bit[9]='t';
|
|---|
| 328 | printf("\"%s\"", bit);
|
|---|
| 329 | }
|
|---|
| 330 | /* times in human form, uppercase first letter */
|
|---|
| 331 | else if (equal("Ctime", f->f_name)) {
|
|---|
| 332 | printf("%.24s (%lu)", ctime(&sb->st_ctime), (u_long) sb->st_ctime);
|
|---|
| 333 | f[1].f_print= 0;
|
|---|
| 334 | }
|
|---|
| 335 | else if (equal("Mtime", f->f_name)) {
|
|---|
| 336 | printf("%.24s (%lu)", ctime(&sb->st_mtime), (u_long) sb->st_mtime);
|
|---|
| 337 | f[1].f_print= 0;
|
|---|
| 338 | }
|
|---|
| 339 | else if (equal("Atime", f->f_name)) {
|
|---|
| 340 | printf("%.24s (%lu)", ctime(&sb->st_atime), (u_long) sb->st_atime);
|
|---|
| 341 | f[1].f_print= 0;
|
|---|
| 342 | }
|
|---|
| 343 | else if (equal("ctime", f->f_name)) {
|
|---|
| 344 | printf("%lu", (u_long) sb->st_ctime);
|
|---|
| 345 | }
|
|---|
| 346 | else if (equal("mtime", f->f_name)) {
|
|---|
| 347 | printf("%lu", (u_long) sb->st_mtime);
|
|---|
| 348 | }
|
|---|
| 349 | else if (equal("atime", f->f_name)) {
|
|---|
| 350 | printf("%lu", (u_long) sb->st_atime);
|
|---|
| 351 | }
|
|---|
| 352 | else {
|
|---|
| 353 | switch (f->f_size) {
|
|---|
| 354 | case sizeof(char):
|
|---|
| 355 | printf("%d", * (u_char *) f->f_addr);
|
|---|
| 356 | break;
|
|---|
| 357 | case sizeof(short):
|
|---|
| 358 | printf("%u", (u_int) * (u_short *) f->f_addr);
|
|---|
| 359 | break;
|
|---|
| 360 | #if INT_MAX != SHRT_MAX
|
|---|
| 361 | case sizeof(int):
|
|---|
| 362 | printf("%u", * (u_int *) f->f_addr);
|
|---|
| 363 | break;
|
|---|
| 364 | #endif
|
|---|
| 365 | #if LONG_MAX != INT_MAX && LONG_MAX != SHRT_MAX
|
|---|
| 366 | case sizeof(long):
|
|---|
| 367 | printf("%lu", * (u_long *) f->f_addr);
|
|---|
| 368 | break;
|
|---|
| 369 | #endif
|
|---|
| 370 | default:
|
|---|
| 371 | fprintf(stderr, "\nProgram error: bad '%s' field size %d\n",
|
|---|
| 372 | f->f_name, f->f_size);
|
|---|
| 373 | break;
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | void rwx(mode_t mode, char *bit)
|
|---|
| 379 | {
|
|---|
| 380 | if (mode&S_IREAD) bit[0]='r';
|
|---|
| 381 | if (mode&S_IWRITE) bit[1]='w';
|
|---|
| 382 | if (mode&S_IEXEC) bit[2]='x';
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | void usage(void)
|
|---|
| 386 | {
|
|---|
| 387 | fprintf(stderr,
|
|---|
| 388 | "Usage: %s [-] [-fd] [-all] [-s] [-field ...] [file1 ...]\n",
|
|---|
| 389 | arg0);
|
|---|
| 390 | exit(1);
|
|---|
| 391 | }
|
|---|