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. weak_alias(sys_siglist, _sys_siglist);
  90. #endif
  91. #define NUM_KNOWN_SIGNALS 32
  92. #if __BUFLEN_INT10TOSTR > 12
  93. #error currently set up for 32 bit ints max!
  94. #endif
  95. static char retbuf[28]; /* 28 is sufficient for 32 bit ints */
  96. static const char unknown_signal[] = "Unknown Signal:";
  97. char *strsignal(int sig)
  98. {
  99. char *pos;
  100. #ifdef WANT_SIGLIST
  101. /* if ((sig >= 0) && (sig < _NSIG)) { */
  102. /* WARNING!!! NOT ALL _NSIG DEFINED!!! */
  103. if ((sig >= 0) && (sig < NUM_KNOWN_SIGNALS)) {
  104. strcpy(retbuf, sys_siglist[sig]);
  105. return retbuf;
  106. }
  107. #endif
  108. pos = _int10tostr(retbuf+sizeof(retbuf)-1, sig) - sizeof(unknown_signal);
  109. strcpy(pos, unknown_signal);
  110. *(pos + sizeof(unknown_signal) - 1) = ' ';
  111. return pos;
  112. }
  113. #endif
  114. /********************** Function psignal ************************************/
  115. #ifdef L_psignal
  116. void psignal(int sig, const char *s)
  117. {
  118. fprintf(stderr, "%s: %s\n", s, strsignal(sig));
  119. }
  120. #endif
  121. /********************** THE END ********************************************/
  122. #ifdef CHECK_BUF
  123. /* quick way to check for sufficient buffer length */
  124. #include <stdio.h>
  125. #include <stdlib.h>
  126. int main(void)
  127. {
  128. int max = 0;
  129. int j, retcode;
  130. const char *p;
  131. #if WANT_SIGLIST
  132. int i;
  133. #endif
  134. retcode = EXIT_SUCCESS;
  135. #if WANT_SIGLIST
  136. /*printf("_NSIG = %d from headers\n", _NSIG);*/
  137. for ( i=0 ; sys_siglist[i] ; i++ ) {
  138. j = strlen(sys_siglist[i])+1;
  139. if (j > max) max = j;
  140. }
  141. if (i != NUM_KNOWN_SIGNALS) {
  142. printf("Error: strsignal.c - NUM_KNOWN_SIGNALS should be %d\n", i);
  143. retcode = EXIT_FAILURE;
  144. }
  145. #endif
  146. p = strsignal(INT_MIN);
  147. j = retbuf+sizeof(retbuf) - p;
  148. if (j > max) max = j;
  149. /*printf("strsignal.c - Test of INT_MIN: <%s> %d\n", p, j);*/
  150. if (sizeof(retbuf) != max) {
  151. printf("Error: strsignal.c - dimension of retbuf should be = %d\n", max);
  152. retcode = EXIT_FAILURE;
  153. }
  154. /*printf("strsignal.c - dimension of retbuf correct at %d\n", max);*/
  155. printf("Passed.\n");
  156. return retcode;
  157. }
  158. #endif