strsignal.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* vi: set sw=4 ts=4: */
  2. /* Copyright (C) 2000 Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
  3. * This file is part of the uClinux and is distributed under the
  4. * GNU Library General Public License.
  5. */
  6. /*
  7. * Manuel Novoa III Dec 2000
  8. *
  9. * Converted to use my new (un)signed long (long) to string routines, which
  10. * are smaller than the previous functions and don't require static buffers.
  11. * Removed dependence on strcat in the process.
  12. *
  13. * Also fixed a bug in the signal name lookup code. While the table is
  14. * declared with dimension > 60, there are currently on 32 signals listed.
  15. *
  16. * Also appended a test routine ( -DCHECK_BUF ) to allow a quick check
  17. * on the buffer length and the number of known signals when the sys_errorlist
  18. * is modified.
  19. *
  20. * Added the option WANT_SIGLIST for low-memory applications to omit the
  21. * signal message strings and only output the signal number.
  22. */
  23. #define WANT_SIGLIST 1
  24. #include <stdlib.h>
  25. #include <malloc.h>
  26. #include <signal.h>
  27. #include <limits.h>
  28. #define __USE_GNU
  29. #include <string.h>
  30. #if (INT_MAX >> 31)
  31. /* We're set up for 32 bit ints */
  32. #error need to check size allocation for static buffer 'retbuf'
  33. #endif
  34. extern char *__ltostr(char *buf, long uval, int base, int uppercase);
  35. /********************** Function strsignal ************************************/
  36. #ifdef L_strsignal
  37. #if WANT_SIGLIST
  38. const char *const sys_siglist[] = {
  39. "Unknown signal",
  40. "Hangup",
  41. "Interrupt",
  42. "Quit",
  43. "Illegal instruction",
  44. "Trace/breakpoint trap",
  45. "IOT trap/Abort",
  46. "Bus error",
  47. "Floating point exception",
  48. "Killed",
  49. "User defined signal 1",
  50. "Segmentation fault",
  51. "User defined signal 2",
  52. "Broken pipe",
  53. "Alarm clock",
  54. "Terminated",
  55. "Stack fault",
  56. "Child exited",
  57. "Continued",
  58. "Stopped (signal)",
  59. "Stopped",
  60. "Stopped (tty input)",
  61. "Stopped (tty output)",
  62. "Urgent condition",
  63. "CPU time limit exceeded",
  64. "File size limit exceeded",
  65. "Virtual time alarm",
  66. "Profile signal",
  67. "Window size changed",
  68. "Possible I/O",
  69. "Power failure",
  70. "Unused signal",
  71. NULL
  72. };
  73. #endif
  74. #define NUM_KNOWN_SIGNALS 32
  75. static char retbuf[28]; /* 28 is sufficient for 32 bit ints */
  76. static const char unknown_signal[] = "Unknown Signal:";
  77. char *strsignal(int sig)
  78. {
  79. char *pos;
  80. #ifdef WANT_SIGLIST
  81. /* if ((sig >= 0) && (sig < _NSIG)) { */
  82. /* WARNING!!! NOT ALL _NSIG DEFINED!!! */
  83. if ((sig >= 0) && (sig < NUM_KNOWN_SIGNALS)) {
  84. strcpy(retbuf, sys_siglist[sig]);
  85. return retbuf;
  86. }
  87. #endif
  88. pos = __ltostr(retbuf + sizeof(unknown_signal) + 1, sig, 10, 0)
  89. - sizeof(unknown_signal);
  90. strcpy(pos, unknown_signal);
  91. *(pos + sizeof(unknown_signal) - 1) = ' ';
  92. return pos;
  93. }
  94. #endif
  95. /********************** Function psignal ************************************/
  96. #ifdef L_psignal
  97. #include <stdio.h>
  98. void psignal(int sig, const char *s)
  99. {
  100. fprintf(stderr, "%s: %s\n", s, strsignal(sig));
  101. }
  102. #endif
  103. /********************** THE END ********************************************/
  104. #ifdef CHECK_BUF
  105. /* quick way to check for sufficient buffer length */
  106. #include <stdio.h>
  107. #include <stdlib.h>
  108. int main(void)
  109. {
  110. int max = 0;
  111. int j, retcode;
  112. const char *p;
  113. #if WANT_SIGLIST
  114. int i;
  115. #endif
  116. retcode = EXIT_SUCCESS;
  117. #if WANT_SIGLIST
  118. /*printf("_NSIG = %d from headers\n", _NSIG);*/
  119. for ( i=0 ; sys_siglist[i] ; i++ ) {
  120. j = strlen(sys_siglist[i])+1;
  121. if (j > max) max = j;
  122. }
  123. if (i != NUM_KNOWN_SIGNALS) {
  124. printf("Error: strsignal.c - NUM_KNOWN_SIGNALS should be %d\n", i);
  125. retcode = EXIT_FAILURE;
  126. }
  127. #endif
  128. p = strsignal(INT_MIN);
  129. j = strlen(p)+1;
  130. if (j > max) max = j;
  131. /*printf("strsignal.c - Test of INT_MIN: <%s> %d\n", p, j);*/
  132. if (sizeof(retbuf) != max) {
  133. printf("Error: strsignal.c - dimension of retbuf should be = %d\n", max);
  134. retcode = EXIT_FAILURE;
  135. }
  136. /*printf("strsignal.c - dimension of retbuf correct at %d\n", max);*/
  137. printf("Passed.\n");
  138. return retcode;
  139. }
  140. #endif