strsignal.c 3.5 KB

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