#define UPREFIX "Unknown signal"
+/*
+ * Define a buffer size big enough to describe a 64-bit signed integer
+ * converted to ASCII decimal (19 bytes), with an optional leading sign
+ * (1 byte); delimiter (": ", 2 bytes); and a trailing NUL (1 byte).
+ */
+#define TMPSIZE (19 + 1 + 2 + 1)
+#define EBUFSIZE NL_TEXTMAX * sizeof(char)
+
static once_t sig_init_once = ONCE_INITIALIZER;
static thread_key_t sig_key;
static int sig_keycreated = 0;
!sig_keycreated)
goto thr_err;
if ((ebuf = thr_getspecific(sig_key)) == NULL) {
- if ((ebuf = malloc(NL_TEXTMAX * sizeof(char))) == NULL)
+ if ((ebuf = malloc(EBUFSIZE)) == NULL)
goto thr_err;
if (thr_setspecific(sig_key, ebuf) != 0) {
free(ebuf);
return (ebuf);
}
-/* XXX: negative 'num' ? (REGR) */
-char *
-strsignal(int num)
+int
+strsignal_r(int num, char *strsignalbuf, size_t buflen)
{
- char *ebuf;
- char tmp[20];
+ int retval = 0;
+ char tmp[TMPSIZE] = { 0 };
size_t n;
int signum;
char *t, *p;
-#if defined(NLS)
- int saved_errno = errno;
- nl_catd catd;
- catd = catopen("libc", NL_CAT_LOCALE);
-#endif
+ signum = num;
+ if (num < 0) {
+ signum = -signum;
+ }
- ebuf = sig_tlsalloc();
- if(ebuf == NULL) {
- errno = ENOMEM;
- return NULL;
+ t = tmp;
+ do {
+ *t++ = "0123456789"[signum % 10];
+ } while (signum /= 10);
+ if (num < 0) {
+ *t++ = '-';
}
+ int suffixlen = strlen(tmp) + 2;
if (num > 0 && num < NSIG) {
- n = strlcpy(ebuf,
-#if defined(NLS)
- catgets(catd, 2, num, sys_siglist[num]),
-#else
+ n = strlcpy(strsignalbuf,
sys_siglist[num],
-#endif
- NL_TEXTMAX * sizeof(char));
+ buflen);
+ if (n >= (buflen - suffixlen)) {
+ retval = ERANGE;
+ }
} else {
- n = strlcpy(ebuf,
-#if defined(NLS)
- catgets(catd, 2, 0xffff, UPREFIX),
-#else
+ n = strlcpy(strsignalbuf,
UPREFIX,
-#endif
- NL_TEXTMAX * sizeof(char));
+ buflen);
+ retval = EINVAL;
}
- signum = num;
- if (num < 0)
- signum = -signum;
+ if (n < (buflen - suffixlen)) {
+ p = (strsignalbuf + n);
+ *p++ = ':';
+ *p++ = ' ';
- t = tmp;
- do {
- *t++ = "0123456789"[signum % 10];
- } while (signum /= 10);
- if (num < 0)
- *t++ = '-';
+ for (;;) {
+ *p++ = *--t;
+ if (t <= tmp)
+ break;
+ }
+ *p = '\0';
+ }
+
+ return retval;
+}
- p = (ebuf + n);
- *p++ = ':';
- *p++ = ' ';
+/* XXX: negative 'num' ? (REGR) */
+char *
+strsignal(int num)
+{
+ char *ebuf;
+
+ ebuf = sig_tlsalloc();
+ if (ebuf == NULL) {
+ errno = ENOMEM;
+ return NULL;
+ }
- for (;;) {
- *p++ = *--t;
- if (t <= tmp)
- break;
+ if (strsignal_r(num, ebuf, EBUFSIZE)) {
+ errno = EINVAL;
}
- *p = '\0';
-#if defined(NLS)
- catclose(catd);
- errno = saved_errno;
-#endif
return (ebuf);
}