Skip to content

Commit a48c434

Browse files
committed
Changed execution of multiple files to multiple execution
1 parent 05d1a69 commit a48c434

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

find.cpp

+32-33
Original file line numberDiff line numberDiff line change
@@ -162,44 +162,43 @@ void find(const usage& arguments, std::vector<std::string>& result) {
162162
}
163163

164164
void execute(std::string& filepath, std::vector<std::string>& result) {
165-
int status;
166-
pid_t pid = fork();
167-
if (pid == -1) {
168-
perror("fork");
169-
exit(EXIT_FAILURE);
170-
}
171-
if (pid == 0) {
172-
std::vector<char *> args;
173-
args.reserve(result.size() + 1);
174-
args.push_back(&filepath[0]);
175-
for (auto& file : result) {
176-
args.push_back(&(file[0]));
177-
}
178-
args.emplace_back(nullptr);
179-
180-
int err = execv(filepath.data(), args.data());
181-
if (err == -1) {
182-
perror("execv");
165+
for (auto& file : result) {
166+
int status;
167+
pid_t pid = fork();
168+
if (pid == -1) {
169+
perror("fork");
183170
exit(EXIT_FAILURE);
184171
}
185-
} else {
186-
do {
187-
pid_t wait_pid = waitpid(pid, &status, WUNTRACED | WCONTINUED);
188-
if (wait_pid == -1) {
189-
perror("waitpid");
172+
if (pid == 0) {
173+
std::vector<char *> args;
174+
args.push_back(&filepath[0]);
175+
args.push_back(&(file[0]));
176+
args.emplace_back(nullptr);
177+
178+
int err = execv(filepath.data(), args.data());
179+
if (err == -1) {
180+
perror("execv");
190181
exit(EXIT_FAILURE);
191182
}
183+
} else {
184+
do {
185+
pid_t wait_pid = waitpid(pid, &status, WUNTRACED | WCONTINUED);
186+
if (wait_pid == -1) {
187+
perror("waitpid");
188+
exit(EXIT_FAILURE);
189+
}
192190

193-
if (WIFEXITED(status)) {
194-
printf("Normal exited, status = %d\n", WEXITSTATUS(status));
195-
} else if (WIFSIGNALED(status)) {
196-
printf("Was killed by signal %d\n", WTERMSIG(status));
197-
} else if (WIFSTOPPED(status)) {
198-
printf("Was stopped by signal %d\n", WSTOPSIG(status));
199-
} else if (WIFCONTINUED(status)) {
200-
printf("Was continued\n");
201-
}
202-
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
191+
if (WIFEXITED(status)) {
192+
printf("Normal exited, status = %d\n", WEXITSTATUS(status));
193+
} else if (WIFSIGNALED(status)) {
194+
printf("Was killed by signal %d\n", WTERMSIG(status));
195+
} else if (WIFSTOPPED(status)) {
196+
printf("Was stopped by signal %d\n", WSTOPSIG(status));
197+
} else if (WIFCONTINUED(status)) {
198+
printf("Was continued\n");
199+
}
200+
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
201+
}
203202
}
204203
}
205204

0 commit comments

Comments
 (0)