strsignal.c 3.1 KB

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