repeat~ repeat a command n times |
| git clone https://github.com/djnnvx/repeat.git |
| log | files | refs | archive | README |
lib.c (678B)
1 2 #include "repeat.h" 3 4 #include <string.h> 5 6 int parse_positive_int(char const *s) { 7 int n = -1; 8 9 if (!s) 10 return n; 11 n = atoi(s); 12 return (n > 0) ? n : -1; 13 } 14 15 int execute_command(char **cmd_argv) { 16 pid_t pid = fork(); 17 if (pid == -1) { 18 perror("repeat: fork failed"); 19 return -1; 20 } else if (pid == 0) { 21 execvp(cmd_argv[0], cmd_argv); 22 perror("repeat: execvp failed"); 23 _exit(127); 24 } 25 int status; 26 waitpid(pid, &status, 0); 27 if (WIFEXITED(status)) 28 return WEXITSTATUS(status); 29 if (WIFSIGNALED(status)) 30 return 128 + WTERMSIG(status); 31 return -1; /* should never be reached */ 32 }