-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path004_file_type.c
48 lines (43 loc) · 1.03 KB
/
004_file_type.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Print the input file type
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/stat.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: '%s' filename\n", argv[0]);
return 1;
}
char *filename = argv[1];
struct stat file_info;
if (stat(filename, &file_info) == -1) {
perror("stat");
return 1;
}
switch (file_info.st_mode & S_IFMT) {
case S_IFREG:
printf("'%s' is a regular file.\n", filename);
break;
case S_IFDIR:
printf("'%s' is a directory.\n", filename);
break;
case S_IFLNK:
printf("'%s' is a symbolic link.\n", filename);
break;
case S_IFSOCK:
printf("'%s' is a socket.\n", filename);
break;
case S_IFCHR:
printf("'%s' is a character device.\n", filename);
break;
case S_IFBLK:
printf("'%s' is a block device.\n", filename);
break;
default:
printf("'%s' is an unknown file type.\n", filename);
break;
}
return 0;
}