strsignal.c 3.1 KB

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