1 /*        $NetBSD: strerror_r.c,v 1.6 2024/06/08 21:35:18 joerg Exp $ */
2 
3 /*
4  * Copyright (c) 1988 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: strerror_r.c,v 1.6 2024/06/08 21:35:18 joerg Exp $");
34 
35 #include "namespace.h"
36 #include <assert.h>
37 #include <atomic.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdio.h>  /* for sys_nerr on FreeBSD */
43 #ifdef NLS
44 #include <limits.h>
45 #include <nl_types.h>
46 #define __SETLOCALE_SOURCE__
47 #include <locale.h>
48 #include "setlocale_local.h"
49 #endif
50 
51 #include "extern.h"
52 
53 #define   UPREFIX   "Unknown error: %d"
54 
__weak_alias(strerror_r,_strerror_r)55 __weak_alias(strerror_r, _strerror_r)
56 
57 #ifdef NLS
58 static void
59 load_errlist(locale_t loc)
60 {
61           const char **errlist;
62           char *errlist_prefix;
63           int i;
64           nl_catd catd;
65           catd = catopen_l("libc", NL_CAT_LOCALE, loc);
66 
67           if (loc->cache->errlist_prefix == NULL) {
68                     errlist_prefix = strdup(catgets(catd, 1, 0xffff, UPREFIX));
69                     if (errlist_prefix == NULL)
70                               goto cleanup2;
71 
72                     membar_release();
73                     if (atomic_cas_ptr(__UNCONST(&loc->cache->errlist_prefix),
74                                            NULL, errlist_prefix) != NULL)
75                               free(errlist_prefix);
76           }
77 
78           if (loc->cache->errlist)
79                     goto cleanup2;
80 
81           errlist = calloc(sys_nerr, sizeof(*errlist));
82           if (errlist == NULL)
83                     goto cleanup2;
84           for (i = 0; i < sys_nerr; ++i) {
85                     errlist[i] = strdup(catgets(catd, 1, i, sys_errlist[i]));
86                     if (errlist[i] == NULL)
87                               goto cleanup;
88           }
89           membar_release();
90           if (atomic_cas_ptr(__UNCONST(&loc->cache->errlist), NULL, errlist) != NULL)
91                     goto cleanup;
92           goto cleanup2;
93 
94   cleanup:
95           for (i = 0; i < sys_nerr; ++i)
96                     free(__UNCONST(errlist[i]));
97           free(errlist);
98   cleanup2:
99           catclose(catd);
100 }
101 #endif
102 
103 int
_strerror_lr(int num,char * buf,size_t buflen,locale_t loc)104 _strerror_lr(int num, char *buf, size_t buflen, locale_t loc)
105 {
106           unsigned int errnum = num;
107           int retval = 0;
108           size_t slen;
109           int saved_errno = errno;
110 #ifdef NLS
111           const char * const *errlist;
112           const char *errlist_prefix;
113 #endif
114 
115           _DIAGASSERT(buf != NULL);
116 
117           if (errnum < (unsigned int) sys_nerr) {
118 #ifdef NLS
119                     errlist = *loc->cache->errlistp;
120                     membar_datadep_consumer();
121                     if (errlist == NULL) {
122                               load_errlist(loc);
123                               errlist = *loc->cache->errlistp;
124                               membar_datadep_consumer();
125                               if (errlist == NULL)
126                                         errlist = *LC_C_LOCALE->cache->errlistp;
127                     }
128                     slen = strlcpy(buf, errlist[errnum], buflen);
129 #else
130                     slen = strlcpy(buf, sys_errlist[errnum], buflen);
131 #endif
132           } else {
133 #ifdef NLS
134                     errlist_prefix = loc->cache->errlist_prefix;
135                     membar_datadep_consumer();
136                     if (errlist_prefix == NULL) {
137                               load_errlist(loc);
138                               errlist_prefix = loc->cache->errlist_prefix;
139                               membar_datadep_consumer();
140                               if (errlist_prefix == NULL)
141                                         errlist_prefix = LC_C_LOCALE->cache->errlist_prefix;
142                     }
143                     slen = snprintf_l(buf, buflen, loc, errlist_prefix, num);
144 #else
145                     slen = snprintf(buf, buflen, UPREFIX, num);
146 #endif
147                     retval = EINVAL;
148           }
149 
150           if (slen >= buflen)
151                     retval = ERANGE;
152 
153           errno = saved_errno;
154 
155           return retval;
156 }
157 
158 int
strerror_r(int num,char * buf,size_t buflen)159 strerror_r(int num, char *buf, size_t buflen)
160 {
161 #ifdef NLS
162           return _strerror_lr(num, buf, buflen, _current_locale());
163 #else
164           return _strerror_lr(num, buf, buflen, NULL);
165 #endif
166 }
167