toaruos/apps/stat.c

93 lines
2.4 KiB
C
Raw Normal View History

2018-08-12 11:14:06 +03:00
/* vim: ts=4 sw=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
2018-08-12 11:14:06 +03:00
* Copyright (C) 2013-2018 K. Lange
*
* stat
*
2018-08-12 11:14:06 +03:00
* Display file status.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
2018-08-12 11:14:06 +03:00
#include <unistd.h>
2018-08-12 11:14:06 +03:00
#include <sys/stat.h>
#include <sys/time.h>
2018-08-12 11:14:06 +03:00
static void show_usage(int argc, char * argv[]) {
printf(
"stat - display file status\n"
"\n"
"usage: %s [-Lq] PATH\n"
"\n"
" -L \033[3mdereference symlinks\033[0m\n"
" -q \033[3mdon't print anything, just return 0 if file exists\033[0m\n"
" -? \033[3mshow this help text\033[0m\n"
"\n", argv[0]);
}
2018-08-12 11:14:06 +03:00
int main(int argc, char ** argv) {
int dereference = 0, quiet = 0;
char * file;
int opt;
2018-08-12 11:14:06 +03:00
while ((opt = getopt(argc, argv, "?Lq")) != -1) {
switch (opt) {
case 'L':
dereference = 1;
break;
case 'q':
quiet = 1;
break;
case '?':
show_usage(argc,argv);
return 1;
}
}
2018-08-12 11:14:06 +03:00
if (optind >= argc) {
show_usage(argc, argv);
return 1;
}
file = argv[optind];
struct stat _stat;
if (dereference) {
if (stat(file, &_stat) < 0) return 1;
} else {
if (lstat(file, &_stat) < 0) return 1;
}
2018-08-12 11:14:06 +03:00
if (quiet) return 0;
printf("0x%x bytes\n", (unsigned int)_stat.st_size);
if (S_ISDIR(_stat.st_mode)) {
printf("Is a directory.\n");
} else if (S_ISFIFO(_stat.st_mode)) {
printf("Is a pipe.\n");
} else if (S_ISLNK(_stat.st_mode)) {
printf("Is a symlink.\n");
} else if (_stat.st_mode & 0111) {
printf("Is executable.\n");
}
struct stat * f = &_stat;
printf("st_dev 0x%x %d\n", (unsigned int)f->st_dev , (unsigned int)sizeof(f->st_dev ));
printf("st_ino 0x%x %d\n", (unsigned int)f->st_ino , (unsigned int)sizeof(f->st_ino ));
printf("st_mode 0x%x %d\n", (unsigned int)f->st_mode , (unsigned int)sizeof(f->st_mode ));
printf("st_nlink 0x%x %d\n", (unsigned int)f->st_nlink , (unsigned int)sizeof(f->st_nlink ));
printf("st_uid 0x%x %d\n", (unsigned int)f->st_uid , (unsigned int)sizeof(f->st_uid ));
printf("st_gid 0x%x %d\n", (unsigned int)f->st_gid , (unsigned int)sizeof(f->st_gid ));
printf("st_rdev 0x%x %d\n", (unsigned int)f->st_rdev , (unsigned int)sizeof(f->st_rdev ));
printf("st_size 0x%x %d\n", (unsigned int)f->st_size , (unsigned int)sizeof(f->st_size ));
printf("0x%x\n", (unsigned int)((uint32_t *)f)[0]);
return 0;
}