int main(int argc, char **argv) { struct stat sb; if(argc < 2) { fprintf(stderr, "Error: %s <PATH>\n", argv[0]); exit(-2); } if(lstat(argv[1], &sb) == 0) { if(sb.st_mode & S_IFLNK) { char tgt[MAXPATHLEN]; if(readlink(argv[1], tgt, sizeof(tgt)) > 0) { printf("link\ts\t%s\n", tgt); } } } time_t now = time(NULL); if(stat(argv[1], &sb) == 0) { printf("exists\tL\t1\n"); #if defined(STAT_TIME_T) #define PTIME(TYPE) do { \ printf(#TYPE "time\tL\t%ld\n", sb.st_##TYPE##time); \ printf(#TYPE "age\tL\t%ld\n", now - sb.st_##TYPE##time); \ } while(0) #elif defined(STAT_TIMESPEC) #define PTIME(TYPE) do { \ printf(#TYPE "time\tL\t%ld\n", sb.st_##TYPE##timespec.tv_sec); \ printf(#TYPE "age\tL\t%ld\n", now - sb.st_##TYPE##timespec.tv_sec); \ } while(0) #else #define PTIME(TYPE) do { \ printf(#TYPE "time\tL\t%ld\n", sb.st_##TYPE##time.tv_sec); \ printf(#TYPE "age\tL\t%ld\n", now - sb.st_##TYPE##time.tv_sec); \ } while(0) #endif PTIME(m); PTIME(a); PTIME(c); printf("hardlinks\tL\t%lu\n", (unsigned long)sb.st_nlink); printf("size\tL\t%llu\n", (unsigned long long)sb.st_size); printf("permissions\ts\t%04o\n", 0xfff & sb.st_mode); printf("type\ts\t%c\n", S_ISREG(sb.st_mode) ? 'f' : S_ISDIR(sb.st_mode) ? 'd' : S_ISLNK(sb.st_mode) ? 'l' : S_ISBLK(sb.st_mode) ? 'b' : S_ISCHR(sb.st_mode) ? 'c' : S_ISFIFO(sb.st_mode) ? 'p' : S_ISSOCK(sb.st_mode) ? 's' : '?'); print_user(sb.st_uid); print_group(sb.st_gid); } else { printf("exists\tL\t1\n"); } }
/* Returns the pair (T,L). */ static Pair pair_mk(Time T, Capacitance L) { Pair p; PTIME(p) = T; PLOAD(p) = L; return p; }
static void pair_show(FILE *fp, Pair p) { fprintf(fp, "(%.2f, %.2f)", PTIME(p), PLOAD(p)); }