forked from maxLundin/os-find
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
197 lines (166 loc) · 4.98 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
#define _GNU_SOURCE
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
/* Exit with error message */
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
/* Linux dirent structure */
struct linux_dirent {
long d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[];
};
/* Command line parameters */
size_t PARAM_MASK = 0;
#define __INUM 1 << 0
#define __NAME 1 << 1
#define __SIZE 1 << 2
#define __SIZE_LOW 1 << 3
#define __SIZE_HIGH 1 << 4
#define __NLINKS 1 << 5
#define __EXEC 1 << 6
unsigned long ino;
char *name;
unsigned long size;
unsigned long size_low;
unsigned long size_high;
unsigned long nlinks;
char *exec;
#define BUF_SIZE 1024
/* Recursive search */
void rsearch(char *str);
/* Handle search result */
void handle(struct linux_dirent *d, char *path_to_file);
int main(int argc, char *argv[]) {
int i = 1;
if (argc > 1) {
int p = strlen(argv[1]);
while (argv[1][--p] == '/') {
argv[1][p] = '\0';
}
while (++i < argc) {
if (!strcmp(argv[i], "-inum")) {
if (++i < argc) {
PARAM_MASK |= __INUM;
ino = atoi(argv[i]);
}
} else if (!strcmp(argv[i], "-name")) {
if (++i < argc) {
PARAM_MASK |= __NAME;
name = argv[i];
}
} else if (!strcmp(argv[i], "-size")) {
if (++i < argc) {
switch (argv[i][0]) {
case '-':
PARAM_MASK |= __SIZE_LOW;
size_low = atoi(argv[i] + 1);
break;
case '=':
PARAM_MASK |= __SIZE;
size = atoi(argv[i] + 1);
break;
case '+':
PARAM_MASK |= __SIZE_HIGH;
size_high = atoi(argv[i] + 1);
break;
default:
PARAM_MASK |= __SIZE;
size = atoi(argv[i]);
break;
}
}
} else if (!strcmp(argv[i], "-nlinks")) {
if (++i < argc) {
PARAM_MASK |= __NLINKS;
nlinks = atoi(argv[i]);
}
} else if (!strcmp(argv[i], "-exec")) {
if (++i < argc) {
PARAM_MASK |= __EXEC;
exec = argv[i];
}
}
}
}
rsearch(argc > 1 ? argv[1] : ".");
exit(EXIT_SUCCESS);
}
void rsearch(char *str) {
int fd, nread;
char buf[BUF_SIZE];
char path_to_file[BUF_SIZE];
struct linux_dirent *d;
int bpos;
char d_type;
fd = open(str, O_RDONLY | O_DIRECTORY);
if (fd == -1)
handle_error("open");
while (true) {
nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
if (nread == -1)
handle_error("getdents");
if (nread == 0)
break;
for (bpos = 0; bpos < nread;) {
d = (struct linux_dirent *) (buf + bpos);
d_type = *(buf + bpos + d->d_reclen - 1);
if (strcmp(d->d_name, ".") != 0 &&
strcmp(d->d_name, "..") != 0) {
strcpy(path_to_file, str);
strcat(path_to_file, "/");
strcat(path_to_file, d->d_name);
handle(d, path_to_file);
if (d_type == DT_DIR) {
rsearch(path_to_file);
}
}
bpos += d->d_reclen;
}
}
}
void handle(struct linux_dirent *d, char *path_to_file) {
struct stat sb;
pid_t child_pid;
if (lstat(path_to_file, &sb) == -1) {
handle_error("lstat");
}
if ((PARAM_MASK & __INUM) && !(d->d_ino == ino))
return;
if ((PARAM_MASK & __NAME) && (strcmp(d->d_name, name)))
return;
if ((PARAM_MASK & __SIZE) && !(sb.st_size == size))
return;
if ((PARAM_MASK & __SIZE_LOW) && !(sb.st_size < size_low))
return;
if ((PARAM_MASK & __SIZE_HIGH) && !(sb.st_size > size_high))
return;
if ((PARAM_MASK & __NLINKS) && !(sb.st_nlink == nlinks))
return;
if (PARAM_MASK & __EXEC) {
child_pid = fork();
switch (child_pid) {
case -1:
handle_error("fork");
break;
case 0:
(void)execl(exec, exec, path_to_file, NULL);
handle_error("exec");
break;
default:
signal(SIGCHLD, SIG_IGN);
break;
}
} else {
puts(path_to_file);
}
}