sig.c (2447B)
1 /* see LICENSE file for copyright and license details */ 2 #include "../sig.h" 3 4 #include <signal.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <strings.h> 8 9 const char *const aruu_sys_signame[NSIG] = { 10 [0] = "EXIT", 11 #ifdef SIGHUP 12 [SIGHUP] = "HUP", 13 #endif 14 #ifdef SIGINT 15 [SIGINT] = "INT", 16 #endif 17 #ifdef SIGQUIT 18 [SIGQUIT] = "QUIT", 19 #endif 20 #ifdef SIGILL 21 [SIGILL] = "ILL", 22 #endif 23 #ifdef SIGTRAP 24 [SIGTRAP] = "TRAP", 25 #endif 26 #ifdef SIGABRT 27 [SIGABRT] = "ABRT", 28 #endif 29 #ifdef SIGBUS 30 [SIGBUS] = "BUS", 31 #endif 32 #ifdef SIGFPE 33 [SIGFPE] = "FPE", 34 #endif 35 #ifdef SIGKILL 36 [SIGKILL] = "KILL", 37 #endif 38 #ifdef SIGUSR1 39 [SIGUSR1] = "USR1", 40 #endif 41 #ifdef SIGSEGV 42 [SIGSEGV] = "SEGV", 43 #endif 44 #ifdef SIGUSR2 45 [SIGUSR2] = "USR2", 46 #endif 47 #ifdef SIGPIPE 48 [SIGPIPE] = "PIPE", 49 #endif 50 #ifdef SIGALRM 51 [SIGALRM] = "ALRM", 52 #endif 53 #ifdef SIGTERM 54 [SIGTERM] = "TERM", 55 #endif 56 #ifdef SIGCHLD 57 [SIGCHLD] = "CHLD", 58 #endif 59 #ifdef SIGCONT 60 [SIGCONT] = "CONT", 61 #endif 62 #ifdef SIGSTOP 63 [SIGSTOP] = "STOP", 64 #endif 65 #ifdef SIGTSTP 66 [SIGTSTP] = "TSTP", 67 #endif 68 #ifdef SIGTTIN 69 [SIGTTIN] = "TTIN", 70 #endif 71 #ifdef SIGTTOU 72 [SIGTTOU] = "TTOU", 73 #endif 74 #ifdef SIGURG 75 [SIGURG] = "URG", 76 #endif 77 #ifdef SIGXCPU 78 [SIGXCPU] = "XCPU", 79 #endif 80 #ifdef SIGXFSZ 81 [SIGXFSZ] = "XFSZ", 82 #endif 83 #ifdef SIGVTALRM 84 [SIGVTALRM] = "VTALRM", 85 #endif 86 #ifdef SIGPROF 87 [SIGPROF] = "PROF", 88 #endif 89 #ifdef SIGWINCH 90 [SIGWINCH] = "WINCH", 91 #endif 92 #ifdef SIGIO 93 [SIGIO] = "IO", 94 #endif 95 #ifdef SIGPWR 96 [SIGPWR] = "PWR", 97 #endif 98 #ifdef SIGSYS 99 [SIGSYS] = "SYS", 100 #endif 101 }; 102 103 const int aruu_sys_nsig = NSIG; 104 105 int 106 aruu_sig2str(int signo, char *str) 107 { 108 const char *name; 109 110 if (signo <= 0 || signo >= aruu_sys_nsig) 111 return -1; 112 name = aruu_sys_signame[signo]; 113 114 if (name == NULL) 115 return -1; 116 strcpy(str, name); 117 return 0; 118 } 119 120 int 121 aruu_str2sig(const char *str, int *signop) 122 { 123 int n; 124 const char *s = str; 125 const char *const *table; 126 int limit; 127 128 table = aruu_sys_signame; 129 limit = aruu_sys_nsig; 130 131 if (strncasecmp(s, "SIG", 3) == 0) 132 s += 3; 133 134 /* check if it is a number */ 135 char *ep; 136 long l = strtol(s, &ep, 10); 137 if (*s && !*ep) { 138 if (l >= 0 && l < limit) { 139 *signop = (int)l; 140 return 0; 141 } 142 return -1; 143 } 144 145 for (n = 1; n < limit; n++) { 146 if (table[n] && strcasecmp(table[n], s) == 0) { 147 *signop = n; 148 return 0; 149 } 150 } 151 return -1; 152 }