strsignal.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2002 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /*
  8. * Sep 11, 2003
  9. * Patch by Atsushi Nemoto <anemo@mba.ocn.ne.jp> to do arch-required
  10. * mapping of signal strings (alpha, mips, hppa, sparc).
  11. */
  12. /* TODO: make a threadsafe version? */
  13. #include <features.h>
  14. #include <string.h>
  15. #include <bits/uClibc_uintmaxtostr.h>
  16. #include <signal.h>
  17. #define _SYS_NSIG 32
  18. #ifdef __UCLIBC_HAS_SIGNUM_MESSAGES__
  19. # define _SYS_SIGMSG_MAXLEN 25
  20. #else
  21. # define _SYS_SIGMSG_MAXLEN 0
  22. #endif
  23. #if _SYS_SIGMSG_MAXLEN < __UIM_BUFLEN_INT + 15
  24. # define _STRSIGNAL_BUFSIZE (__UIM_BUFLEN_INT + 15)
  25. #else
  26. # define _STRSIGNAL_BUFSIZE _SYS_SIGMSG_MAXLEN
  27. #endif
  28. #ifdef __UCLIBC_HAS_SIGNUM_MESSAGES__
  29. extern const char _string_syssigmsgs[] attribute_hidden;
  30. #if defined(__alpha__) || defined(__mips__) || defined(__sparc__)
  31. static const unsigned char sstridx[] = {
  32. 0,
  33. SIGHUP,
  34. SIGINT,
  35. SIGQUIT,
  36. SIGILL,
  37. SIGTRAP,
  38. SIGIOT,
  39. SIGBUS,
  40. SIGFPE,
  41. SIGKILL,
  42. SIGUSR1,
  43. SIGSEGV,
  44. SIGUSR2,
  45. SIGPIPE,
  46. SIGALRM,
  47. SIGTERM,
  48. #if defined SIGSTKFLT
  49. SIGSTKFLT,
  50. #else
  51. 0,
  52. #endif
  53. SIGCHLD,
  54. SIGCONT,
  55. SIGSTOP,
  56. SIGTSTP,
  57. SIGTTIN,
  58. SIGTTOU,
  59. SIGURG,
  60. SIGXCPU,
  61. SIGXFSZ,
  62. SIGVTALRM,
  63. SIGPROF,
  64. SIGWINCH,
  65. SIGIO,
  66. SIGPWR,
  67. SIGSYS,
  68. #if defined SIGEMT
  69. SIGEMT,
  70. #endif
  71. };
  72. #endif
  73. char *strsignal(int signum)
  74. {
  75. register char *s;
  76. int i;
  77. static char buf[_STRSIGNAL_BUFSIZE];
  78. static const char unknown[] = {
  79. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  80. };
  81. #if defined(__alpha__) || defined(__mips__) || defined(__sparc__)
  82. /* Need to translate signum to string index. */
  83. for (i = 0; i < sizeof(sstridx)/sizeof(sstridx[0]); i++) {
  84. if (sstridx[i] == signum) {
  85. goto GOT_SSTRIDX;
  86. }
  87. }
  88. i = INT_MAX; /* Failed. */
  89. GOT_SSTRIDX:
  90. #else
  91. /* No signum to string index translation needed. */
  92. i = signum;
  93. #endif
  94. if (((unsigned int) signum) < _SYS_NSIG) {
  95. /* Trade time for space. This function should rarely be called
  96. * so rather than keeping an array of pointers for the different
  97. * messages, just run through the buffer until we find the
  98. * correct string. */
  99. for (s = (char *) _string_syssigmsgs; i; ++s) {
  100. if (!*s) {
  101. --i;
  102. }
  103. }
  104. if (*s) { /* Make sure we have an actual message. */
  105. goto DONE;
  106. }
  107. }
  108. s = _int10tostr(buf + sizeof(buf)-1, signum) - sizeof(unknown);
  109. memcpy(s, unknown, sizeof(unknown));
  110. DONE:
  111. return s;
  112. }
  113. #else /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  114. char *strsignal(int signum)
  115. {
  116. static char buf[_STRSIGNAL_BUFSIZE];
  117. static const char unknown[] = {
  118. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  119. };
  120. return memcpy(_int10tostr(buf + sizeof(buf)-1, signum) - sizeof(unknown),
  121. unknown, sizeof(unknown));
  122. }
  123. #endif /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  124. libc_hidden_def(strsignal)