-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermstat.c
More file actions
84 lines (72 loc) · 2.24 KB
/
permstat.c
File metadata and controls
84 lines (72 loc) · 2.24 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int perms[] = {S_IRUSR, S_IWUSR, S_IXUSR,
S_IRGRP, S_IWGRP, S_IXGRP,
S_IROTH, S_IWOTH, S_IXOTH};
void display_usage(char *progname) {
printf("Usage: %s <filename>\n", progname);
}
/**
* TODO
* Returns a string (pointer to char array) containing the permissions of the
* file referenced in statbuf.
* Allocates enough space on the heap to hold the 10 printable characters.
* The first char is always a - (dash), since all files must be regular files.
* The remaining 9 characters represent the permissions of user (owner), group,
* and others: r, w, x, or -.
*/
char* permission_string(struct stat *statbuf) {
char* perm_string = (char*)malloc(10 * sizeof(char));
strcat(perm_string, "-");
int mode, remainder;
for(int i = 0; i < 9; i++) {
if(statbuf->st_mode & perms[i]) {
remainder = i % 3;
switch(remainder) {
case 0:
strcat(perm_string, "r");
break;
case 1:
strcat(perm_string, "w");
break;
case 2:
strcat(perm_string, "x");
break;
}
}
else {
strcat(perm_string, "-");
}
}
return perm_string;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
display_usage(argv[0]);
return EXIT_FAILURE;
}
struct stat statbuf;
if (stat(argv[1], &statbuf) < 0) {
fprintf(stderr, "Error: Cannot stat '%s'. %s.\n", argv[1],
strerror(errno));
return EXIT_FAILURE;
}
/* TODO
* If the argument supplied to this program is not a regular file,
* print out an error message to standard error and return EXIT_FAILURE.
* For example:
* $ ./permstat iamdir
* Error: 'iamdir' is not a regular file.
*/
if(!S_ISREG(statbuf.st_mode)) {
fprintf(stderr, "Error: \'%s\' is not a regular file.\n", argv[1]);
return EXIT_FAILURE;
}
char *perms = permission_string(&statbuf);
printf("Permissions: %s\n", perms);
free(perms);
return EXIT_SUCCESS;
}