strsignal.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* vi: set sw=4 ts=4: */
  2. /* strsignal for uClibc
  3. *
  4. * Copyright (C) 2000,2001 by Erik Andersen <andersen@uclibc.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU Library General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /*
  21. * Manuel Novoa III Dec 2000
  22. *
  23. * Converted to use my new (un)signed long (long) to string routines, which
  24. * are smaller than the previous functions and don't require static buffers.
  25. * Removed dependence on strcat in the process.
  26. *
  27. * Also fixed a bug in the signal name lookup code. While the table is
  28. * declared with dimension > 60, there are currently on 32 signals listed.
  29. *
  30. * Also appended a test routine ( -DCHECK_BUF ) to allow a quick check
  31. * on the buffer length and the number of known signals when the sys_errorlist
  32. * is modified.
  33. *
  34. * Added the option WANT_SIGLIST for low-memory applications to omit the
  35. * signal message strings and only output the signal number.
  36. *
  37. * Manuel Novoa III Feb 2002
  38. *
  39. * Change to use _int10tostr and fix a bug in end-of-buf arg.
  40. */
  41. #define WANT_SIGLIST 1
  42. #define _STDIO_UTILITY /* For _int10tostr. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <malloc.h>
  46. #include <signal.h>
  47. #ifndef __USE_GNU
  48. #define __USE_GNU
  49. #endif
  50. #include <string.h>
  51. /********************** Function strsignal ************************************/
  52. #ifdef L_strsignal
  53. #if WANT_SIGLIST
  54. const char *const sys_siglist[] = {
  55. "Unknown signal",
  56. "Hangup",
  57. "Interrupt",
  58. "Quit",
  59. "Illegal instruction",
  60. "Trace/breakpoint trap",
  61. "IOT trap/Abort",
  62. "Bus error",
  63. "Floating point exception",
  64. "Killed",
  65. "User defined signal 1",
  66. "Segmentation fault",
  67. "User defined signal 2",
  68. "Broken pipe",
  69. "Alarm clock",
  70. "Terminated",
  71. "Stack fault",
  72. "Child exited",
  73. "Continued",
  74. "Stopped (signal)",
  75. "Stopped",
  76. "Stopped (tty input)",
  77. "Stopped (tty output)",
  78. "Urgent condition",
  79. "CPU time limit exceeded",
  80. "File size limit exceeded",
  81. "Virtual time alarm",
  82. "Profile signal",
  83. "Window size changed",
  84. "Possible I/O",
  85. "Power failure",
  86. "Unused signal",
  87. NULL
  88. };
  89. #endif
  90. #define NUM_KNOWN_SIGNALS 32
  91. #if __BUFLEN_INT10TOSTR > 12
  92. #error currently set up for 32 bit ints max!
  93. #endif
  94. static char retbuf[28]; /* 28 is sufficient for 32 bit ints */
  95. static const char unknown_signal[] = "Unknown Signal:";
  96. char *strsignal(int sig)
  97. {
  98. char *pos;
  99. #ifdef WANT_SIGLIST
  100. /* if ((sig >= 0) && (sig < _NSIG)) { */
  101. /* WARNING!!! NOT ALL _NSIG DEFINED!!! */
  102. if ((sig >= 0) && (sig < NUM_KNOWN_SIGNALS)) {
  103. strcpy(retbuf, sys_siglist[sig]);
  104. return retbuf;
  105. }
  106. #endif
  107. pos = _int10tostr(retbuf+sizeof(retbuf)-1, sig) - sizeof(unknown_signal);
  108. strcpy(pos, unknown_signal);
  109. *(pos + sizeof(unknown_signal) - 1) = ' ';
  110. return pos;
  111. }
  112. #endif
  113. /********************** Function psignal ************************************/
  114. #ifdef L_psignal
  115. void psignal(int sig, const char *s)
  116. {
  117. fprintf(stderr, "%s: %s\n", s, strsignal(sig));
  118. }
  119. #endif
  120. /********************** THE END ********************************************/
  121. #ifdef CHECK_BUF
  122. /* quick way to check for sufficient buffer length */
  123. #include <stdio.h>
  124. #include <stdlib.h>
  125. int main(void)
  126. {
  127. int max = 0;
  128. int j, retcode;
  129. const char *p;
  130. #if WANT_SIGLIST
  131. int i;
  132. #endif
  133. retcode = EXIT_SUCCESS;
  134. #if WANT_SIGLIST
  135. /*printf("_NSIG = %d from headers\n", _NSIG);*/
  136. for ( i=0 ; sys_siglist[i] ; i++ ) {
  137. j = strlen(sys_siglist[i])+1;
  138. if (j > max) max = j;
  139. }
  140. if (i != NUM_KNOWN_SIGNALS) {
  141. printf("Error: strsignal.c - NUM_KNOWN_SIGNALS should be %d\n", i);
  142. retcode = EXIT_FAILURE;
  143. }
  144. #endif
  145. p = strsignal(INT_MIN);
  146. j = retbuf+sizeof(retbuf) - p;
  147. if (j > max) max = j;
  148. /*printf("strsignal.c - Test of INT_MIN: <%s> %d\n", p, j);*/
  149. if (sizeof(retbuf) != max) {
  150. printf("Error: strsignal.c - dimension of retbuf should be = %d\n", max);
  151. retcode = EXIT_FAILURE;
  152. }
  153. /*printf("strsignal.c - dimension of retbuf correct at %d\n", max);*/
  154. printf("Passed.\n");
  155. return retcode;
  156. }
  157. #endif