forked from chocolatiers/doom-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfuck.c
117 lines (96 loc) · 1.95 KB
/
unfuck.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
115
116
117
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <sys/types.h>
int argc;
char **argv;
int recursive;
int chmod(char *, int);
void usage(void)
{
fprintf(stderr, "\n"
"Usage: unfuck [-r] <file or directory names...>\n"
"\n"
" Unfucks file permissions\n"
" (-r : recursively descends subdirectories)\n"
"\n"
);
exit(-1);
}
int parmloc(char *parm)
{
int argnum;
for (argnum=argc - 1 ; argnum ; argnum--)
if (!strcmp(argv[argnum], parm))
{
argv[argnum] = 0;
break;
}
return argnum;
}
int argloc(void)
{
int argloc = argc - 1;
while (argv[argloc] && argloc)
argloc--;
return argloc+1;
}
void derror(char *str, ...)
{
va_list args;
va_start(args, str);
fprintf(stderr, "error: ");
vfprintf(stderr, str, args);
fprintf(stderr, "\n");
va_end(args);
exit(-1);
}
void unfuckfilename(char *filename, int treatasfile)
{
struct stat st;
lstat(filename, &st);
if ((st.st_mode & S_IFMT) == S_IFDIR && !treatasfile)
{
DIR *dir;
struct direct *ent;
char *subname;
dir = opendir(filename);
if (!dir)
derror("Could not open directory %s\n", filename);
while ((ent = readdir(dir)))
{
subname = alloca(strlen(ent->d_name) + strlen(filename) + 2);
sprintf(subname, "%s/%s", filename, ent->d_name);
if (!strcmp(ent->d_name, "."))
unfuckfilename(subname, 1);
else if (strcmp(ent->d_name, ".."))
unfuckfilename(subname, 0);
}
closedir(dir);
}
else
{
if (st.st_mode & 0100)
st.st_mode |= 0111;
st.st_mode |= 0666;
chmod(filename, st.st_mode);
}
}
int main(int c, char **v)
{
int arglist;
argc = c;
argv = v;
recursive = parmloc("-r");
arglist = argloc();
if (arglist == argc)
usage();
while (arglist < argc)
{
unfuckfilename(argv[arglist], !recursive);
arglist++;
}
return 0;
}