From 0cc35b207df562084f2351a1edd8723acbbc5477 Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Sun, 8 Oct 2023 19:52:25 +0200 Subject: refactoring --- rpt.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/rpt.c b/rpt.c index 4c1fc1d..5d013d1 100644 --- a/rpt.c +++ b/rpt.c @@ -76,28 +76,30 @@ wait_child(pid_t pid, void (*handle_error_f)(int status)) handle_error_f(exit_status); } -/* Invoke the executable *ARGV COUNT times, passing it the rest of ARG as - arguments. Call HANDLE_ERROR_F between each process. */ void -repeat_cmd(char **argv, long count, void (*handle_error_f)(int status)) +invoke_cmd(char **argv, void (*handle_error_f)(int status)) { - pid_t pid; + pid_t pid = fork(); - for(long i = 0; i < count; ++i) + if (pid < 0) { - pid = fork(); + perror("rpt"); + exit(4); + } - if (pid < 0) - { - perror("rpt"); - exit(4); - } + if (pid == 0) + exec_child(argv); + else + wait_child(pid, handle_error_f); +} - if (pid == 0) - exec_child(argv); - else - wait_child(pid, handle_error_f); - } +/* Invoke the executable *ARGV COUNT times, passing it the rest of ARG as + arguments. Call HANDLE_ERROR_F between each process. */ +void +repeat_cmd(char **argv, long count, void (*handle_error_f)(int status)) +{ + for(long i = 0; i < count; ++i) + invoke_cmd(argv, handle_error_f); } int -- cgit v1.2.3