lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | static void print_struct_stat(char *msg, struct stat *s) |
| 9 | { |
| 10 | printf("%s\n", msg); |
| 11 | /* The casts are because glibc thinks it's cool */ |
| 12 | printf("device : 0x%llx\n",(long long)s->st_dev); |
| 13 | printf("inode : %lld\n", (long long)s->st_ino); |
| 14 | printf("mode : 0x%llx\n",(long long)s->st_mode); |
| 15 | printf("nlink : %lld\n", (long long)s->st_nlink); |
| 16 | printf("uid : %lld\n", (long long)s->st_uid); |
| 17 | printf("gid : %lld\n", (long long)s->st_gid); |
| 18 | printf("rdev : 0x%llx\n",(long long)s->st_rdev); |
| 19 | printf("size : %lld\n", (long long)s->st_size); |
| 20 | printf("blksize : %lld\n", (long long)s->st_blksize); |
| 21 | printf("blocks : %lld\n", (long long)s->st_blocks); |
| 22 | printf("atime : %lld\n", (long long)s->st_atime); |
| 23 | printf("mtime : %lld\n", (long long)s->st_mtime); |
| 24 | printf("ctime : %lld\n", (long long)s->st_ctime); |
| 25 | } |
| 26 | |
| 27 | int main(int argc,char **argv) |
| 28 | { |
| 29 | int fd, ret; |
| 30 | char *file; |
| 31 | struct stat s; |
| 32 | |
| 33 | if (argc < 2) { |
| 34 | fprintf(stderr, "Usage: stat FILE\n"); |
| 35 | exit(1); |
| 36 | } |
| 37 | file = argv[1]; |
| 38 | |
| 39 | memset(&s, 0, sizeof(struct stat)); |
| 40 | ret = stat(file, &s); |
| 41 | if(ret<0){ |
| 42 | perror("stat"); |
| 43 | exit(1); |
| 44 | } |
| 45 | print_struct_stat("\nTesting stat:", &s); |
| 46 | |
| 47 | memset(&s, 0, sizeof(struct stat)); |
| 48 | ret = lstat(file, &s); |
| 49 | if(ret<0){ |
| 50 | perror("lstat"); |
| 51 | exit(1); |
| 52 | } |
| 53 | print_struct_stat("\nTesting lstat:", &s); |
| 54 | |
| 55 | |
| 56 | fd = open(file, O_RDONLY); |
| 57 | if(fd<0){ |
| 58 | perror("open"); |
| 59 | exit(1); |
| 60 | } |
| 61 | memset(&s, 0, sizeof(struct stat)); |
| 62 | ret = fstat(fd,&s); |
| 63 | if(ret<0){ |
| 64 | perror("fstat"); |
| 65 | exit(1); |
| 66 | } |
| 67 | print_struct_stat("\nTesting fstat:", &s); |
| 68 | |
| 69 | exit(0); |
| 70 | } |
| 71 | |