u_signal_names.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) many different people.
  6. * If you wrote this, please acknowledge your work.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. */
  23. #include <signal.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include <strings.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. struct signal_name {
  30. const char *name;
  31. int number;
  32. };
  33. static const struct signal_name signames[] = {
  34. /* POSIX signals */
  35. { "EXIT", 0 }, /* 0 */
  36. { "HUP", SIGHUP }, /* 1 */
  37. { "INT", SIGINT }, /* 2 */
  38. { "QUIT", SIGQUIT }, /* 3 */
  39. { "ILL", SIGILL }, /* 4 */
  40. { "ABRT", SIGABRT }, /* 6 */
  41. { "FPE", SIGFPE }, /* 8 */
  42. { "KILL", SIGKILL }, /* 9 */
  43. { "SEGV", SIGSEGV }, /* 11 */
  44. { "PIPE", SIGPIPE }, /* 13 */
  45. { "ALRM", SIGALRM }, /* 14 */
  46. { "TERM", SIGTERM }, /* 15 */
  47. { "USR1", SIGUSR1 }, /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
  48. { "USR2", SIGUSR2 }, /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
  49. { "CHLD", SIGCHLD }, /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
  50. { "CONT", SIGCONT }, /* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */
  51. { "STOP", SIGSTOP }, /* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */
  52. { "TSTP", SIGTSTP }, /* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */
  53. { "TTIN", SIGTTIN }, /* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */
  54. { "TTOU", SIGTTOU }, /* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */
  55. /* Miscellaneous other signals */
  56. #ifdef SIGTRAP
  57. { "TRAP", SIGTRAP }, /* 5 */
  58. #endif
  59. #ifdef SIGIOT
  60. { "IOT", SIGIOT }, /* 6, same as SIGABRT */
  61. #endif
  62. #ifdef SIGEMT
  63. { "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */
  64. #endif
  65. #ifdef SIGBUS
  66. { "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
  67. #endif
  68. #ifdef SIGSYS
  69. { "SYS", SIGSYS }, /* 12 (mips,alpha,sparc*) */
  70. #endif
  71. #ifdef SIGSTKFLT
  72. { "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */
  73. #endif
  74. #ifdef SIGURG
  75. { "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
  76. #endif
  77. #ifdef SIGIO
  78. { "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
  79. #endif
  80. #ifdef SIGPOLL
  81. { "POLL", SIGPOLL }, /* same as SIGIO */
  82. #endif
  83. #ifdef SIGCLD
  84. { "CLD", SIGCLD }, /* same as SIGCHLD (mips) */
  85. #endif
  86. #ifdef SIGXCPU
  87. { "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
  88. #endif
  89. #ifdef SIGXFSZ
  90. { "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
  91. #endif
  92. #ifdef SIGVTALRM
  93. { "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
  94. #endif
  95. #ifdef SIGPROF
  96. { "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
  97. #endif
  98. #ifdef SIGPWR
  99. { "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
  100. #endif
  101. #ifdef SIGINFO
  102. { "INFO", SIGINFO }, /* 29 (alpha) */
  103. #endif
  104. #ifdef SIGLOST
  105. { "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */
  106. #endif
  107. #ifdef SIGWINCH
  108. { "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
  109. #endif
  110. #ifdef SIGUNUSED
  111. { "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */
  112. #endif
  113. {0, 0}
  114. };
  115. /*
  116. if str_sig == NULL returned signal name [*signo],
  117. if str_sig != NULL - set *signo from signal_name,
  118. findings with digit number or with or without SIG-prefix name
  119. if startnum=0 flag for support finding zero signal,
  120. but str_sig="0" always found, (hmm - standart or realize?)
  121. if startnum<0 returned reverse signal_number <-> signal_name
  122. if found error - returned NULL
  123. */
  124. const char *
  125. u_signal_names(const char *str_sig, int *signo, int startnum)
  126. {
  127. static char retstr[16];
  128. const struct signal_name *s = signames;
  129. static const char prefix[] = "SIG";
  130. const char *sptr;
  131. if(startnum)
  132. s++;
  133. if(str_sig==NULL) {
  134. while (s->name != 0) {
  135. if(s->number == *signo)
  136. break;
  137. s++;
  138. }
  139. } else {
  140. if (isdigit(((unsigned char)*str_sig))) {
  141. char *endp;
  142. long int sn = strtol(str_sig, &endp, 10);
  143. /* test correct and overflow */
  144. if(*endp == 0 && sn >= 0 && sn < NSIG) {
  145. *signo = (int)sn;
  146. /* test for unnamed */
  147. sptr = u_signal_names(0, signo, 0);
  148. if(sptr==NULL)
  149. return NULL;
  150. if(sn!=0)
  151. sptr += 3;
  152. return sptr;
  153. }
  154. } else {
  155. sptr = str_sig;
  156. while (s->name != 0) {
  157. if (strcasecmp(s->name, sptr) == 0) {
  158. *signo = s->number;
  159. if(startnum<0) {
  160. sprintf(retstr, "%d", *signo);
  161. return retstr;
  162. }
  163. break;
  164. }
  165. if(s!=signames && sptr == str_sig &&
  166. strncasecmp(sptr, prefix, 3) == 0) {
  167. sptr += 3; /* strlen(prefix) */
  168. continue;
  169. }
  170. sptr = str_sig;
  171. s++;
  172. }
  173. }
  174. }
  175. if(s->name==0)
  176. return NULL;
  177. if(s!=signames)
  178. strcpy(retstr, prefix);
  179. else
  180. retstr[0] = 0;
  181. return strcat(retstr, s->name);
  182. }