-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutility.c
62 lines (55 loc) · 1.65 KB
/
utility.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
#include <stdio.h>
#include <pwd.h>
#include <time.h>
#include <grp.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "utility.h"
void traverse(const char *path, void (*callback)(struct dirent *dir), void (*dirProc)(const char *dir)) {
DIR *pDir = opendir(path);
struct dirent *entry = NULL;
while ((entry = readdir(pDir)) != 0) {
if (entry->d_type == DT_DIR && dirProc)
dirProc(entry->d_name);
else if (callback)
callback(entry);
}
closedir(pDir);
}
char *Month[] = {"Mon", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
char parseType(unsigned char type) {
switch (type) {
case DT_REG:
return '-';
case DT_DIR:
return 'd';
case DT_LNK:
return 'l';
case DT_FIFO:
return 'p';
case DT_CHR:
return 'c';
case DT_SOCK:
return 's';
case DT_BLK:
return 'b';
case DT_UNKNOWN:
default:
return '?';
}
}
char *parseMode(mode_t mode, char st[]) {
int count = 0;
st[count++] = (char) (mode & S_IRUSR ? 'r' : '-');
st[count++] = (char) (mode & S_IWUSR ? 'w' : '-');
st[count++] = (char) (mode & S_IXUSR ? 'x' : '-');
st[count++] = (char) (mode & S_IRGRP ? 'r' : '-');
st[count++] = (char) (mode & S_IWGRP ? 'w' : '-');
st[count++] = (char) (mode & S_IXGRP ? 'x' : '-');
st[count++] = (char) (mode & S_IROTH ? 'r' : '-');
st[count++] = (char) (mode & S_IWOTH ? 'w' : '-');
st[count++] = (char) (mode & S_IXOTH ? 'x' : '-');
return st;
}