]> mj.ucw.cz Git - libucw.git/blob - ucw/exitstatus.c
tableprinter: code cleanup
[libucw.git] / ucw / exitstatus.c
1 /*
2  *      UCW Library -- Formatting of Process Exit Status
3  *
4  *      (c) 2004--2012 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/process.h>
12 #include <ucw/signames.h>
13
14 #include <stdio.h>
15 #include <sys/wait.h>
16 #include <errno.h>
17
18 int
19 format_exit_status(char *msg, int stat)
20 {
21   if (stat < 0)
22     sprintf(msg, "failed to fork (err=%d)", errno);
23   else if (WIFEXITED(stat) && WEXITSTATUS(stat) < 256)
24     {
25       if (WEXITSTATUS(stat))
26         sprintf(msg, "died with exit code %d", WEXITSTATUS(stat));
27       else
28         {
29           msg[0] = 0;
30           return 0;
31         }
32     }
33   else if (WIFSIGNALED(stat))
34     {
35       int sig = WTERMSIG(stat);
36       const char *sn = sig_number_to_name(sig);
37       sprintf(msg, "died on signal %d (%s)", sig, (sn ? : "unknown"));
38     }
39   else
40     sprintf(msg, "died with status %x", stat);
41   return 1;
42 }