Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice02 / ex3-stat.c
blob: 24a77775998619b50ca68831fd66c72ad45f88d4 (plain) (blame)
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
#include <sys/stat.h> // stat, struct stat, S_ISREG, ...
#include <stdio.h> // printf, perror

int main(int argc, char* argv[]) {
	for (int i = 1; i < argc; i++) {
		struct stat buf;
		if (stat(argv[i], &buf) < 0) {
			perror("stat");
			return 1;
		}

		printf("  File: %s\n", argv[i]);
		printf("  Size: %ld\tBlocks: %ld\tIO Block: %ld\t", buf.st_size, buf.st_blocks, buf.st_blksize);

		     if (S_ISREG(buf.st_mode)) printf("regular file");
		else if (S_ISDIR(buf.st_mode)) printf("directory");
		else if (S_ISCHR(buf.st_mode)) printf("character special file");
		else if (S_ISBLK(buf.st_mode)) printf("block special file");
		else if (S_ISFIFO(buf.st_mode)) printf("fifo");
		else if (S_ISLNK(buf.st_mode)) printf("symbolic link");
		else if (S_ISSOCK(buf.st_mode)) printf("socket");
		printf("\n");

		printf("Device: %ld\tInode: %ld\tLinks: %ld\n", buf.st_dev, buf.st_ino, buf.st_nlink);
		printf("Access: %u\tUid: %u\tGid: %u\n", buf.st_mode, buf.st_uid, buf.st_gid);
		printf("Access: %ld\n", buf.st_atime);
		printf("Modify: %ld\n", buf.st_mtime);
		printf("Change: %ld\n", buf.st_ctime);
	}
}