summaryrefslogtreecommitdiffabout
path: root/tests/to.c
Unidiff
Diffstat (limited to 'tests/to.c') (more/less context) (ignore whitespace changes)
-rw-r--r--tests/to.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/to.c b/tests/to.c
new file mode 100644
index 0000000..6511a54
--- a/dev/null
+++ b/tests/to.c
@@ -0,0 +1,74 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <errno.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/wait.h>
7
8int
9main (int argc, char **argv)
10{
11 char *progname = argv[0];
12 unsigned long n;
13 char *p;
14 pid_t pid, ret;
15 int status;
16
17 if (argc < 3)
18 {
19 fprintf (stderr, "usage: %s TIMEOUT COMMAND ...\n", progname);
20 exit (1);
21 }
22 errno = 0;
23 n = strtoul (argv[1], &p, 10);
24 if (errno || *p || n == 0)
25 {
26 fprintf (stderr, "%s: %s is not a valid timeout\n", progname, argv[1]);
27 exit (1);
28 }
29
30 argc -= 2;
31 argv += 2;
32
33 pid = fork ();
34 if (pid == -1)
35 {
36 perror ("fork");
37 exit (127);
38 }
39
40 if (pid == 0)
41 {
42 execvp (argv[0], argv);
43 perror (argv[0]);
44 exit (127);
45 }
46
47 alarm (n);
48 ret = wait (&status);
49 alarm (0);
50
51 if (ret != pid)
52 {
53 perror ("wait");
54 exit (127);
55 }
56
57 if (WIFEXITED (status))
58 return WEXITSTATUS (status);
59
60 if (WIFSIGNALED (status))
61 fprintf (stderr, "%s: %s terminated on signal %d\n", progname, argv[0],
62 WTERMSIG (status));
63 else if (WIFSTOPPED (status))
64 fprintf (stderr, "%s: %s stopped\n", progname, argv[0]);
65 else
66 fprintf (stderr, "%s: %s exited with unrecognized status %d\n",
67 progname, argv[0], status);
68 return 127;
69}
70
71
72
73
74

Return to:

Send suggestions and report system problems to the System administrator.