signame.c (3169B)
1 #include "signame.h" 2 3 #include "util.h" 4 5 #include <signal.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 #define SIGNUM_NAME(name) \ 10 { SIG##name, #name } 11 12 static struct { 13 int num; 14 const char* name; 15 } signals[] = { 16 /* Signals required by POSIX 1003.1-2001 base, listed in 17 traditional numeric order where possible. */ 18 #ifdef SIGHUP 19 SIGNUM_NAME(HUP), 20 #endif 21 #ifdef SIGINT 22 SIGNUM_NAME(INT), 23 #endif 24 #ifdef SIGQUIT 25 SIGNUM_NAME(QUIT), 26 #endif 27 #ifdef SIGILL 28 SIGNUM_NAME(ILL), 29 #endif 30 #ifdef SIGTRAP 31 SIGNUM_NAME(TRAP), 32 #endif 33 #ifdef SIGABRT 34 SIGNUM_NAME(ABRT), 35 #endif 36 #ifdef SIGFPE 37 SIGNUM_NAME(FPE), 38 #endif 39 #ifdef SIGKILL 40 SIGNUM_NAME(KILL), 41 #endif 42 #ifdef SIGSEGV 43 SIGNUM_NAME(SEGV), 44 #endif 45 /* On Haiku, SIGSEGV == SIGBUS, but we prefer SIGSEGV to match 46 strsignal.c output, so SIGBUS must be listed second. */ 47 #ifdef SIGBUS 48 SIGNUM_NAME(BUS), 49 #endif 50 #ifdef SIGPIPE 51 SIGNUM_NAME(PIPE), 52 #endif 53 #ifdef SIGALRM 54 SIGNUM_NAME(ALRM), 55 #endif 56 #ifdef SIGTERM 57 SIGNUM_NAME(TERM), 58 #endif 59 #ifdef SIGUSR1 60 SIGNUM_NAME(USR1), 61 #endif 62 #ifdef SIGUSR2 63 SIGNUM_NAME(USR2), 64 #endif 65 #ifdef SIGCHLD 66 SIGNUM_NAME(CHLD), 67 #endif 68 #ifdef SIGURG 69 SIGNUM_NAME(URG), 70 #endif 71 #ifdef SIGSTOP 72 SIGNUM_NAME(STOP), 73 #endif 74 #ifdef SIGTSTP 75 SIGNUM_NAME(TSTP), 76 #endif 77 #ifdef SIGCONT 78 SIGNUM_NAME(CONT), 79 #endif 80 #ifdef SIGTTIN 81 SIGNUM_NAME(TTIN), 82 #endif 83 #ifdef SIGTTOU 84 SIGNUM_NAME(TTOU), 85 #endif 86 87 /* Signals required by POSIX 1003.1-2001 with the XSI extension. */ 88 #ifdef SIGSYS 89 SIGNUM_NAME(SYS), 90 #endif 91 #ifdef SIGPOLL 92 SIGNUM_NAME(POLL), 93 #endif 94 #ifdef SIGVTALRM 95 SIGNUM_NAME(VTALRM), 96 #endif 97 #ifdef SIGPROF 98 SIGNUM_NAME(PROF), 99 #endif 100 #ifdef SIGXCPU 101 SIGNUM_NAME(XCPU), 102 #endif 103 #ifdef SIGXFSZ 104 SIGNUM_NAME(XFSZ), 105 #endif 106 107 /* Unix Version 7. */ 108 #ifdef SIGIOT 109 SIGNUM_NAME(IOT), /* Older name for ABRT. */ 110 #endif 111 #ifdef SIGEMT 112 SIGNUM_NAME(EMT), 113 #endif 114 115 /* USG Unix. */ 116 #ifdef SIGPHONE 117 SIGNUM_NAME(PHONE), 118 #endif 119 #ifdef SIGWIND 120 SIGNUM_NAME(WIND), 121 #endif 122 123 /* Unix System V. */ 124 #ifdef SIGCLD 125 SIGNUM_NAME(CLD), 126 #endif 127 #ifdef SIGPWR 128 SIGNUM_NAME(PWR), 129 #endif 130 131 /* GNU/Linux 2.2 and Solaris 8. */ 132 #ifdef SIGCANCEL 133 SIGNUM_NAME(CANCEL), 134 #endif 135 #ifdef SIGLWP 136 SIGNUM_NAME(LWP), 137 #endif 138 #ifdef SIGWAITING 139 SIGNUM_NAME(WAITING), 140 #endif 141 #ifdef SIGFREEZE 142 SIGNUM_NAME(FREEZE), 143 #endif 144 #ifdef SIGTHAW 145 SIGNUM_NAME(THAW), 146 #endif 147 #ifdef SIGLOST 148 SIGNUM_NAME(LOST), 149 #endif 150 #ifdef SIGWINCH 151 SIGNUM_NAME(WINCH), 152 #endif 153 154 /* GNU/Linux 2.2. */ 155 #ifdef SIGINFO 156 SIGNUM_NAME(INFO), 157 #endif 158 #ifdef SIGIO 159 SIGNUM_NAME(IO), 160 #endif 161 #ifdef SIGSTKFLT 162 SIGNUM_NAME(STKFLT), 163 #endif 164 165 /* OpenBSD. */ 166 #ifdef SIGTHR 167 SIGNUM_NAME(THR), 168 #endif 169 }; 170 171 int signame(char const* name) { 172 char* endptr; 173 int signum; 174 175 if ((signum = strtol(name, &endptr, 10)) && endptr == strchr(name, '\0')) 176 return signum; 177 178 // startswith SIG, remove that so -SIGKILL == -KILL 179 if (strncmp(name, "SIG", 3) == 0) { 180 name += 3; 181 } 182 183 // search for name 184 for (int i = 0; i < (int) LEN(signals); i++) 185 if (streq(signals[i].name, name)) 186 return signals[i].num; 187 188 return -1; 189 } 190 191 const char* sigabbr(int signal) { 192 // search for name 193 for (int i = 0; i < (int) LEN(signals); i++) 194 if (signals[i].num == signal) 195 return signals[i].name; 196 197 return "UNKNOWN"; 198 }