-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
79 lines (67 loc) · 1.49 KB
/
util.c
File metadata and controls
79 lines (67 loc) · 1.49 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
#include "util.h"
/**
*/
char *trim(char *str) {
// ltrim
while(isspace(*str)) {
str++;
}
if(*str == 0) {
return str;
}
// rtrim
char * back = str + strlen(str);
while(isspace(*--back));
*(back+1) = '\0';
return str;
}
/**
* Show Help
*/
void showHelp(char * app, char * method) {
printf("\n%s Version %s\n", app, VERSION);
printf("\nLocate File Search using %s cache.\n", method);
printf("\nUsage:\n\n");
printf(" To Scan data (scan file system):\n");
printf(" %s scan <search> <file_or_folder>\n\n", app);
printf(" To Fetch data (fetch data from shared memory):\n");
printf(" %s fetch <engine> <search> <file_or_folder>\n\n", app);
printf(" To Check if a search exists:\n");
printf(" %s exists <search>\n\n", app);
printf(" To Store data:\n");
printf(" %s store <search> <file_or_folder>\n\n", app);
printf(" To Delete data:\n");
printf(" %s delete <search>\n\n", app);
}
/**
*/
void exitProgram(int err, char * msg) {
if(DEBUG) {
printf("ERROR: %s\n", msg);
}
exit(err);
}
/**
*/
unsigned int isFile(char * file) {
struct stat f;
return stat(file, &f) == 0 && S_ISREG(f.st_mode);
}
/**
*/
unsigned int isDir(char * dir) {
struct stat f;
return stat(dir, &f) == 0 && S_ISDIR(f.st_mode);
}
/**
*/
char * getMD5(char * string) {
int i = 0;
unsigned char md5Str[MD5_DIGEST_LENGTH];
char *ret = (char*)malloc(33);
MD5(string, strlen(string), md5Str);
for(; i < 16; ++i) {
sprintf(&ret[i*2], "%02x", (unsigned int)md5Str[i]);
}
return ret;
}