/
main.c
230 lines (164 loc) · 4.12 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <malloc.h>
#include <alloca.h>
#include <openssl/md5.h>
int is_hex(const char input) {
//30 - 39 0-9
for (char i = 0x30; i < 0x40; i++)
if (input == i) return 1;
//61 - 66 a-f
for (char i = 0x61; i < 0x67; i++)
if (input == i) return 1;
//41 - 46 A-F
for (char i = 0x41; i < 0x47; i++)
if (input == i) return 1;
return 0;
}
int is_md5(const char *input) {
int i = 0;
for (; input[i]; i++) {
//Check that this digit is [0-9a-fA-F]
if (is_hex(input[i]))
continue;
//Exclude the dot extension from the equation.
if (input[i] == '.')
break;
//This character was not a hex digit or a dot, not an md5.
return 0;
}
//The individual string characters are legit but are there the right number of them?
return i == 32;
}
//Return value is on the heap.
char* md5(const char *filename) {
//Open the input file.
FILE *in_file = fopen(filename, "rb");
if (!in_file) {
printf ("%s can't be opened.\n", filename);
return NULL;
}
//Initialise the md5 function.
MD5_CTX md_context;
MD5_Init (&md_context);
//Read in the data from the file and feed it to the md5 function.
int bytes;
unsigned char data[1024];
while ((bytes = fread(data, 1, 1024, in_file)) != 0) {
MD5_Update(&md_context, data, bytes);
}
//Recieve the final md5 value inside c.
unsigned char c[MD5_DIGEST_LENGTH];
MD5_Final(c,&md_context);
//Allocate memory for the return value.
char tmp[2];
char *hash = malloc((MD5_DIGEST_LENGTH * 2) + 1);
//Format the md5 digits as chars.
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
sprintf(tmp, "%02x", c[i]);
memcpy(hash+(i*2), tmp, 2);
}
hash[MD5_DIGEST_LENGTH*2] = '\0';
fclose (in_file);
return hash;
}
void process_file(const char *filepath, int s) {
const char *filename = strrchr(filepath, '/');
int path_len;
if (!filename) {
//No forward slashes in this string, the path is the name.
filename = filepath;
path_len = 0;
} else {
filename++;
path_len = filename - filepath;
}
if (!s && is_md5(filename)) {
printf("Skipping %s (Use -s)\n", filename);
return;
}
char *hash = md5(filepath);
if (!hash)
//Bad things.
return;
char *ext = strrchr(filename, '.');
int ext_len = ext? strlen(ext): 0;
int size = (path_len + 32 + ext_len + 1);
char *new_filepath = alloca(size);
if (path_len)
memcpy(new_filepath, filepath, path_len);
memcpy(new_filepath + path_len, hash, 32);
if (ext)
memcpy(new_filepath + path_len + 32, ext, ext_len);
new_filepath[size-1] = '\0';
rename(filepath, new_filepath);
printf("Renaming %s to %s\n", filepath, new_filepath);
free(hash);
}
void file_loop(const char *filename, int r, int s) {
if (filename[0] == '-' || filename[0] == '.') {
return;
}
struct stat buf;
stat(filename, &buf);
if (r && S_ISDIR(buf.st_mode)) {
DIR *dir;
struct dirent *ent;
int path_size = strlen(filename) + 2;
int size = path_size + 20;
char *path = malloc(size);
//Already checked length of filename.
strcpy(path, filename);
path[path_size - 2] = '/';
path[path_size - 1] = '\0';
if ((dir = opendir(path))) {
//For each file ent.
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] == '.')
continue;
if (strlen(ent->d_name) + path_size >= size) {
size = strlen(ent->d_name) + path_size;
path = realloc(path, size);
}
//Already checked length of ent->d_name.
memcpy(path+path_size - 1, ent->d_name, strlen(ent->d_name) + 1);
file_loop(path, r, s);
}
} else {
printf("Could not open dir %s\n", filename);
}
free(path);
closedir(dir);
} else if(S_ISREG(buf.st_mode)) {
process_file(filename, s);
}
}
void main(int argc, char **argv) {
if (argc < 2)
return;
//Not enough arguments.
// -r recusive
// -l lazy
// -s strict
int r = 0, s = 0;
for (int i = 1; i < argc; i++) {
if (strlen(argv[i]) == 2 && argv[i][0] == '-') {
switch (argv[i][1]) {
case 'r':
r = 1;
break;
case 'l':
s = 0;
break;
case 's':
s = 1;
}
}
}
for (int i = 1; i < argc; i++) {
file_loop(argv[i], r, s);
}
}