strsignal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #define _GNU_SOURCE
  14. #include <features.h>
  15. #include <string.h>
  16. #include <bits/uClibc_uintmaxtostr.h>
  17. #include <signal.h>
  18. #define _SYS_NSIG 32
  19. #ifdef __UCLIBC_HAS_SIGNUM_MESSAGES__
  20. # define _SYS_SIGMSG_MAXLEN 25
  21. #else /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  22. # define _SYS_SIGMSG_MAXLEN 0
  23. #endif /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  24. #if _SYS_SIGMSG_MAXLEN < __UIM_BUFLEN_INT + 15
  25. # define _STRSIGNAL_BUFSIZE (__UIM_BUFLEN_INT + 15)
  26. #else
  27. # define _STRSIGNAL_BUFSIZE _SYS_SIGMSG_MAXLEN
  28. #endif
  29. #ifdef __UCLIBC_HAS_SIGNUM_MESSAGES__
  30. extern const char _string_syssigmsgs[] attribute_hidden;
  31. #if defined(__alpha__) || defined(__mips__) || defined(__hppa__) || defined(__sparc__)
  32. static const unsigned char sstridx[] = {
  33. 0,
  34. SIGHUP,
  35. SIGINT,
  36. SIGQUIT,
  37. SIGILL,
  38. SIGTRAP,
  39. SIGIOT,
  40. SIGBUS,
  41. SIGFPE,
  42. SIGKILL,
  43. SIGUSR1,
  44. SIGSEGV,
  45. SIGUSR2,
  46. SIGPIPE,
  47. SIGALRM,
  48. SIGTERM,
  49. #if defined(__alpha__) || defined(__mips__) || defined(__sparc__)
  50. 0,
  51. #else
  52. SIGSTKFLT,
  53. #endif
  54. SIGCHLD,
  55. SIGCONT,
  56. SIGSTOP,
  57. SIGTSTP,
  58. SIGTTIN,
  59. SIGTTOU,
  60. SIGURG,
  61. SIGXCPU,
  62. SIGXFSZ,
  63. SIGVTALRM,
  64. SIGPROF,
  65. SIGWINCH,
  66. SIGIO,
  67. SIGPWR,
  68. SIGSYS,
  69. #if defined(__alpha__) || defined(__mips__) || defined(__hppa__) || defined(__sparc__)
  70. SIGEMT,
  71. #endif
  72. };
  73. #endif
  74. char attribute_hidden *__strsignal(int signum)
  75. {
  76. register char *s;
  77. int i;
  78. static char buf[_STRSIGNAL_BUFSIZE];
  79. static const char unknown[] = {
  80. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  81. };
  82. #if defined(__alpha__) || defined(__mips__) || defined(__hppa__) || defined(__sparc__)
  83. /* Need to translate signum to string index. */
  84. for (i = 0 ; i < sizeof(sstridx)/sizeof(sstridx[0]) ; i++) {
  85. if (sstridx[i] == signum) {
  86. goto GOT_SSTRIDX;
  87. }
  88. }
  89. i = INT_MAX; /* Failed. */
  90. GOT_SSTRIDX:
  91. #else
  92. /* No signum to string index translation needed. */
  93. i = signum;
  94. #endif
  95. if (((unsigned int) signum) < _SYS_NSIG) {
  96. /* Trade time for space. This function should rarely be called
  97. * so rather than keeping an array of pointers for the different
  98. * messages, just run through the buffer until we find the
  99. * correct string. */
  100. for (s = (char *) _string_syssigmsgs ; i ; ++s) {
  101. if (!*s) {
  102. --i;
  103. }
  104. }
  105. if (*s) { /* Make sure we have an actual message. */
  106. goto DONE;
  107. }
  108. }
  109. s = _int10tostr(buf+sizeof(buf)-1, signum) - sizeof(unknown);
  110. __memcpy(s, unknown, sizeof(unknown));
  111. DONE:
  112. return s;
  113. }
  114. #else /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  115. char attribute_hidden *__strsignal(int signum)
  116. {
  117. static char buf[_STRSIGNAL_BUFSIZE];
  118. static const char unknown[] = {
  119. 'U', 'n', 'k', 'n', 'o', 'w', 'n', ' ', 's', 'i', 'g', 'n', 'a', 'l', ' '
  120. };
  121. return (char *) __memcpy(_int10tostr(buf+sizeof(buf)-1, signum)
  122. - sizeof(unknown),
  123. unknown, sizeof(unknown));
  124. }
  125. #endif /* __UCLIBC_HAS_SIGNUM_MESSAGES__ */
  126. strong_alias(__strsignal,strsignal)