@@ -162,44 +162,43 @@ void find(const usage& arguments, std::vector<std::string>& result) {
162
162
}
163
163
164
164
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" );
183
170
exit (EXIT_FAILURE);
184
171
}
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" );
190
181
exit (EXIT_FAILURE);
191
182
}
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
+ }
192
190
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
+ }
203
202
}
204
203
}
205
204
0 commit comments