Skip to content

Commit 05d1a69

Browse files
committed
Fixed execution error
1 parent b1ffe76 commit 05d1a69

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@
1212
- Поддерживать комбинацию аргументов. Например хочется найти все файлы с размером больше 1GB и скормить их утилите /usr/bin/sha1sum.
1313
- Выполнять поиск рекурсивно, в том числе во всех вложенных директориях.
1414
- Сильные духом призываются к выполнению задания с использованием системного вызова getdents(2). Остальные могут использовать readdir и opendir для чтения содержимого директории.
15+
16+
Usage:
17+
path [options] - find files by path with options.
18+
19+
Options:
20+
-inum inum - inode number;
21+
-name name - file name;
22+
-size [-=+]size - file's size(less, equal, more);
23+
-nlinks num - file's hardlinks;
24+
-exec path - file to execute;

find.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ static std::string reason;
4545

4646
usage parse_arguments(int& argc, char* argv[]) {
4747
usage arguments;
48+
if (argc % 2 != 0) {
49+
ok = false;
50+
reason = "Wrong numbers of args";
51+
return arguments;
52+
}
4853
arguments.path = argv[1];
4954
for (int i = 2; i < argc; i += 2) {
5055
std::string option(argv[i]);
@@ -165,7 +170,8 @@ void execute(std::string& filepath, std::vector<std::string>& result) {
165170
}
166171
if (pid == 0) {
167172
std::vector<char *> args;
168-
args.reserve(result.size());
173+
args.reserve(result.size() + 1);
174+
args.push_back(&filepath[0]);
169175
for (auto& file : result) {
170176
args.push_back(&(file[0]));
171177
}
@@ -198,13 +204,14 @@ void execute(std::string& filepath, std::vector<std::string>& result) {
198204
}
199205

200206
int main(int argc, char* argv[]) {
201-
std::cout << help << std::endl;
202207
if (argc <= 1) {
203208
std::cout << "Wrong usage, see help" << std::endl;
209+
std::cout << help << std::endl;
204210
}
205211
usage arguments = parse_arguments(argc, argv);
206212
if (!ok) {
207-
std::cerr << reason << ", see help" << std::endl;
213+
std::cout << reason << ", see help" << std::endl;
214+
std::cout << help << std::endl;
208215
exit(EXIT_FAILURE);
209216
}
210217

0 commit comments

Comments
 (0)