Browse Source

Add the -t, --timeout option

develop
Damien Goutte-Gattat 9 years ago
parent
commit
173840f199
  1. 28
      src/wait4.c

28
src/wait4.c

@ -22,6 +22,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <getopt.h>
#include <errno.h>
#include <err.h>
@ -79,23 +80,31 @@ get_integer_or_die(const char *arg)
return val;
}
static void
on_alarm(int sig)
{
exit(EXIT_SUCCESS);
}
/* Main function. */
int
main(int argc, char **argv)
{
int c, ec, len, *pids, i;
int c, ec, len, *pids, i, timeout;
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "timeout", 1, NULL, 't' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
timeout = 0;
while ( (c = getopt_long(argc, argv, "hv", options, NULL)) != -1 ) {
while ( (c = getopt_long(argc, argv, "hvt:", options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
@ -108,12 +117,27 @@ main(int argc, char **argv)
case 'v':
info();
break;
case 't':
timeout = get_integer_or_die(optarg);
break;
}
}
if ( (len = argc - optind) < 1 )
usage(EXIT_FAILURE);
if ( timeout ) {
struct sigaction sa;
sa.sa_handler = on_alarm;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM, &sa, NULL);
alarm(timeout);
}
pids = xmalloc(len * sizeof(int));
for ( i = 0; i < len; i++ )
pids[i] = get_integer_or_die(argv[optind + i]);

Loading…
Cancel
Save