#include "signal.h" #include #include #include #define SIG(s) [SIG##s-1] = #s static const char map[][4] = { SIG(HUP), SIG(INT), SIG(QUIT), SIG(ILL), SIG(TRAP), SIG(ABRT), SIG(BUS), SIG(FPE), SIG(KILL), SIG(USR1), SIG(SEGV), SIG(USR2), SIG(PIPE), SIG(ALRM), SIG(TERM), SIG(CHLD), SIG(CONT), SIG(STOP), SIG(TSTP), SIG(TTIN), SIG(TTOU), SIG(URG), SIG(XCPU), SIG(XFSZ), SIG(PROF), SIG(IO), SIG(PWR), SIG(SYS), #ifdef SIGEMT SIG(EMT), #endif }; static const char other[] = { #ifdef SIGSTKFLT SIGSTKFLT, 'S','T','K','F','L','T', 0, #endif SIGVTALRM, 'V','T','A','L','R','M', 0, SIGWINCH, 'W','I','N','C','H', 0, RTMIN, 'R','T','M','I','N', 0, RTMAX, 'R','T','M','A','X', 0, /* aliases */ SIGPOLL, 'P','O','L','L', 0, SIGIOT, 'I','O','T', 0, SIGUNUSED, 'U','N','U','S','E','D', 0, 0 }; int sig2str(int sig, char *str) { const char *p; int i, num; if (sig > 0 && sig-1 < sizeof map/sizeof *map && *map[sig-1]) { for (i = 0; i < sizeof *map; i++) str[i] = map[sig-1][i]; str[i] = 0; return 0; } else if (sig > RTMIN && sig < RTMAX) { for (i = 0; i < 6; i++) str[i] = "RTMIN+"[i]; num = sig-RTMIN; if (num > 10) str[i++] = '0'+num/10; str[i++] = '0'+num%10; str[i] = 0; return 0; } else for (p = other, i = 0; i < 5; i++) { if (*p++ == sig) { for (i = 0; p[i]; i++) str[i] = p[i]; return 0; } while (*p++) ; p++; } errno = EINVAL; return -1; } int str2sig(const char *restrict str, int *restrict sig) { const char *p; size_t len; int i, num; len = strnlen(str, 5); if (len == 3 || len == 4) for (i = 0; i < sizeof map/sizeof *map; i++) if (strncmp(str, map[i], 4) == 0) return (*sig = i+1, 0); if (len <= 6) for (p = other; *p; p += len+1) if (strncmp(str, p+1, (len = strnlen(p+1, 6)+1)) == 0) return (*sig = *p, 0); i = strncmp(str, "RTMIN+", 6)==0||strncmp(str, "RTMAX-", 6)==0?6:0; if (str[i] >= '1' && str[i] <= '9') { num = str[i++]-'0'; if (isdigit(str[i])) num = num*10+str[i++]-'0'; if (!str[i]) { if (isdigit(*str)) { if (num <= RTMAX) return (*sig = num, 0); } else if (num < RTMAX-RTMIN) return (*sig = str[5]=='+'?RTMIN+num:RTMAX-num, 0); } } /* errno = EINVAL ? */ return -1; }