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