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