]> mj.ucw.cz Git - libucw.git/blob - ucw/exitstatus.c
Updated TODO
[libucw.git] / ucw / exitstatus.c
1 /*
2  *      UCW Library -- Formatting of Process Exit Status
3  *
4  *      (c) 2004 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
13 #include <stdio.h>
14 #include <sys/wait.h>
15 #include <errno.h>
16
17 int
18 format_exit_status(char *msg, int stat)
19 {
20   if (stat < 0)
21     sprintf(msg, "failed to fork (err=%d)", errno);
22   else if (WIFEXITED(stat) && WEXITSTATUS(stat) < 256)
23     {
24       if (WEXITSTATUS(stat))
25         sprintf(msg, "died with exit code %d", WEXITSTATUS(stat));
26       else
27         {
28           msg[0] = 0;
29           return 0;
30         }
31     }
32   else if (WIFSIGNALED(stat))
33     sprintf(msg, "died on signal %d", WTERMSIG(stat));
34   else
35     sprintf(msg, "died with status %x", stat);
36   return 1;
37 }