strsignal.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #define WANT_SIGLIST 1
  38. #include <stdlib.h>
  39. #include <malloc.h>
  40. #include <signal.h>
  41. #include <limits.h>
  42. #define __USE_GNU
  43. #include <string.h>
  44. #if (INT_MAX >> 31)
  45. /* We're set up for 32 bit ints */
  46. #error need to check size allocation for static buffer 'retbuf'
  47. #endif
  48. extern char *__ltostr(char *buf, long uval, int base, int uppercase);
  49. /********************** Function strsignal ************************************/
  50. #ifdef L_strsignal
  51. #if WANT_SIGLIST
  52. const char *const sys_siglist[] = {
  53. "Unknown signal",
  54. "Hangup",
  55. "Interrupt",
  56. "Quit",
  57. "Illegal instruction",
  58. "Trace/breakpoint trap",
  59. "IOT trap/Abort",
  60. "Bus error",
  61. "Floating point exception",
  62. "Killed",
  63. "User defined signal 1",
  64. "Segmentation fault",
  65. "User defined signal 2",
  66. "Broken pipe",
  67. "Alarm clock",
  68. "Terminated",
  69. "Stack fault",
  70. "Child exited",
  71. "Continued",
  72. "Stopped (signal)",
  73. "Stopped",
  74. "Stopped (tty input)",
  75. "Stopped (tty output)",
  76. "Urgent condition",
  77. "CPU time limit exceeded",
  78. "File size limit exceeded",
  79. "Virtual time alarm",
  80. "Profile signal",
  81. "Window size changed",
  82. "Possible I/O",
  83. "Power failure",
  84. "Unused signal",
  85. NULL
  86. };
  87. #endif
  88. #define NUM_KNOWN_SIGNALS 32
  89. static char retbuf[28]; /* 28 is sufficient for 32 bit ints */
  90. static const char unknown_signal[] = "Unknown Signal:";
  91. char *strsignal(int sig)
  92. {
  93. char *pos;
  94. #ifdef WANT_SIGLIST
  95. /* if ((sig >= 0) && (sig < _NSIG)) { */
  96. /* WARNING!!! NOT ALL _NSIG DEFINED!!! */
  97. if ((sig >= 0) && (sig < NUM_KNOWN_SIGNALS)) {
  98. strcpy(retbuf, sys_siglist[sig]);
  99. return retbuf;
  100. }
  101. #endif
  102. pos = __ltostr(retbuf + sizeof(unknown_signal) + 1, sig, 10, 0)
  103. - sizeof(unknown_signal);
  104. strcpy(pos, unknown_signal);
  105. *(pos + sizeof(unknown_signal) - 1) = ' ';
  106. return pos;
  107. }
  108. #endif
  109. /********************** Function psignal ************************************/
  110. #ifdef L_psignal
  111. #include <stdio.h>
  112. void psignal(int sig, const char *s)
  113. {
  114. fprintf(stderr, "%s: %s\n", s, strsignal(sig));
  115. }
  116. #endif
  117. /********************** THE END ********************************************/
  118. #ifdef CHECK_BUF
  119. /* quick way to check for sufficient buffer length */
  120. #include <stdio.h>
  121. #include <stdlib.h>
  122. int main(void)
  123. {
  124. int max = 0;
  125. int j, retcode;
  126. const char *p;
  127. #if WANT_SIGLIST
  128. int i;
  129. #endif
  130. retcode = EXIT_SUCCESS;
  131. #if WANT_SIGLIST
  132. /*printf("_NSIG = %d from headers\n", _NSIG);*/
  133. for ( i=0 ; sys_siglist[i] ; i++ ) {
  134. j = strlen(sys_siglist[i])+1;
  135. if (j > max) max = j;
  136. }
  137. if (i != NUM_KNOWN_SIGNALS) {
  138. printf("Error: strsignal.c - NUM_KNOWN_SIGNALS should be %d\n", i);
  139. retcode = EXIT_FAILURE;
  140. }
  141. #endif
  142. p = strsignal(INT_MIN);
  143. j = strlen(p)+1;
  144. if (j > max) max = j;
  145. /*printf("strsignal.c - Test of INT_MIN: <%s> %d\n", p, j);*/
  146. if (sizeof(retbuf) != max) {
  147. printf("Error: strsignal.c - dimension of retbuf should be = %d\n", max);
  148. retcode = EXIT_FAILURE;
  149. }
  150. /*printf("strsignal.c - dimension of retbuf correct at %d\n", max);*/
  151. printf("Passed.\n");
  152. return retcode;
  153. }
  154. #endif