-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathper.c
115 lines (98 loc) · 2.54 KB
/
per.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// vim: ts=2 shiftwidth=2 noexpandtab
/*
* per - Simple unix permission viewer and converter
*
* This program is licensed under GPL version 3.
* Copyright is a theft.
* For more info see the LICENSE file or reffer to ``https://www.gnu.org/licenses/gpl-3.0.html''.
*/
#include <errno.h>
#include <getopt.h>
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "per.h"
_Bool specialp = FALSE;
/* Function Definitions */
int
main(int argc, char **argv) {
if (argc == 1) {
usage();
exit(1);
}
/* If only one arg is supplied respawn with `-vns' */
if (argc == 2) {
if (strcmp(argv[1], "-h"))
execl(argv[0], argv[0], "-vns", argv[1], NULL);
else { usage(); exit(0); }
}
/* The same as above but runs when only `-S' is passed */
if (argc == 3 && (strcmp(argv[1], "-S")) == 0)
execl(argv[0], argv[0], "-Svns", argv[2], NULL);
Perm *perm = NULL;
/* Cleaner syntax */
char *target = argv[argc-1];
/* If target is a `-', read target from stdin */
if (strcmp(target, "-") == 0) {
fscanf(stdin, "%s", target);
}
/* Switch options */
int option;
while ((option = getopt(argc, argv, "Snsvh")) != -1) {
switch (option) {
case 'S':
specialp = TRUE;
break;
case 'n':
if (!perm) perm = new_perm_from_value(target);
printf("%0*o\n", specialp ? 4 : 3, perm->numeric);
break;
case 's':
if (!perm) perm = new_perm_from_value(target);
puts(perm->symbolic);
break;
case 'v':
if (!perm) perm = new_perm_from_value(target);
print_verbose(perm->numeric);
break;
case 'h':
usage();
exit(0);
default:
usage();
exit(1);
}
}
return (0);
} /* End of main() */
Perm *
new_perm_from_value(char *target) {
Perm *perm = calloc(1, sizeof(Perm));
char *endptr;
long numeric = strtol(target, &endptr, 8);
struct stat statbuf;
/* Exit if ``NUMERIC'' is negative or is longer than ``BITN'' bits */
if (numeric >> (specialp ? 12 : 9)) {
usage();
ERR(1, EINVAL, "Incorrect numeric notation");
}
/* Is ``TARGET'' a number? */
if (*endptr == '\0') {
perm->numeric = numeric;
perm->symbolic = numeric_to_symbolic(numeric);
}
/* Is ``TARGET'' a valid path? */
else if (stat(target, &statbuf) != -1) {
numeric = statbuf.st_mode & (0777 + (specialp ? 07000 : 0));
perm->numeric = numeric;
perm->symbolic = numeric_to_symbolic(numeric);
}
/* Assuming ``TARGET'' is symbolic notation */
else {
perm->numeric = symbolic_to_numeric(target);
perm->symbolic = target;
}
return perm;
} /* End of new_perm_from_value() */