xref: /illumos-gate/usr/src/lib/libc/port/gen/strerror.c (revision faadcf7e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26b599bd93SRobert Mustacchi /*
27b599bd93SRobert Mustacchi  * Copyright 2015 Joyent, Inc.
28*faadcf7eSRobert Mustacchi  * Copyright 2024 Oxide Computer Company
29b599bd93SRobert Mustacchi  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
32*faadcf7eSRobert Mustacchi /*	  All Rights Reserved	*/
337c478bd9Sstevel@tonic-gate 
347257d1b4Sraf #include "lint.h"
357c478bd9Sstevel@tonic-gate #include "_libc_gettext.h"
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate extern const char _sys_errs[];
417c478bd9Sstevel@tonic-gate extern const int _sys_index[];
427c478bd9Sstevel@tonic-gate extern int _sys_num_err;
43*faadcf7eSRobert Mustacchi extern const char *_sys_err_names[];
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate char *
strerror_l(int errnum,locale_t loc)46b599bd93SRobert Mustacchi strerror_l(int errnum, locale_t loc)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	if (errnum < _sys_num_err && errnum >= 0)
49b599bd93SRobert Mustacchi 		return (_libc_gettext_l(&_sys_errs[_sys_index[errnum]],
50b599bd93SRobert Mustacchi 		    loc));
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	errno = EINVAL;
53b599bd93SRobert Mustacchi 	return (_libc_gettext_l("Unknown error", loc));
54b599bd93SRobert Mustacchi }
55b599bd93SRobert Mustacchi 
56b599bd93SRobert Mustacchi char *
strerror(int errnum)57b599bd93SRobert Mustacchi strerror(int errnum)
58b599bd93SRobert Mustacchi {
59b599bd93SRobert Mustacchi 	return (strerror_l(errnum, uselocale(NULL)));
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
62*faadcf7eSRobert Mustacchi /*
63*faadcf7eSRobert Mustacchi  * A version of sterror() that always operates in the C locale. It returns NULL
64*faadcf7eSRobert Mustacchi  * rather than the "Unknown error" string.
65*faadcf7eSRobert Mustacchi  */
66*faadcf7eSRobert Mustacchi const char *
strerrordesc_np(int errnum)67*faadcf7eSRobert Mustacchi strerrordesc_np(int errnum)
68*faadcf7eSRobert Mustacchi {
69*faadcf7eSRobert Mustacchi 	if (errnum < _sys_num_err && errnum >= 0)
70*faadcf7eSRobert Mustacchi 		return (&_sys_errs[_sys_index[errnum]]);
71*faadcf7eSRobert Mustacchi 
72*faadcf7eSRobert Mustacchi 	errno = EINVAL;
73*faadcf7eSRobert Mustacchi 	return (NULL);
74*faadcf7eSRobert Mustacchi }
75*faadcf7eSRobert Mustacchi 
76*faadcf7eSRobert Mustacchi const char *
strerrorname_np(int errnum)77*faadcf7eSRobert Mustacchi strerrorname_np(int errnum)
78*faadcf7eSRobert Mustacchi {
79*faadcf7eSRobert Mustacchi 	if (errnum >= 0 && errnum < _sys_num_err &&
80*faadcf7eSRobert Mustacchi 	    _sys_err_names[errnum] != NULL) {
81*faadcf7eSRobert Mustacchi 		return (_sys_err_names[errnum]);
82*faadcf7eSRobert Mustacchi 	}
83*faadcf7eSRobert Mustacchi 
84*faadcf7eSRobert Mustacchi 	errno = EINVAL;
85*faadcf7eSRobert Mustacchi 	return (NULL);
86*faadcf7eSRobert Mustacchi }
87*faadcf7eSRobert Mustacchi 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Implemented strerror_r in Solaris 10 to comply with SUSv3 2001.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate int
strerror_r(int errnum,char * strerrbuf,size_t buflen)927257d1b4Sraf strerror_r(int errnum, char *strerrbuf, size_t buflen)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	char *buf;
957c478bd9Sstevel@tonic-gate 	int ret = 0;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (errnum < _sys_num_err && errnum >= 0) {
987c478bd9Sstevel@tonic-gate 		buf = _libc_gettext((char *)&_sys_errs[_sys_index[errnum]]);
997c478bd9Sstevel@tonic-gate 	} else {
1007c478bd9Sstevel@tonic-gate 		buf = _libc_gettext("Unknown error");
1017c478bd9Sstevel@tonic-gate 		ret = errno = EINVAL;
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	/*
1057c478bd9Sstevel@tonic-gate 	 * At compile time, there is no way to determine the max size of
1067c478bd9Sstevel@tonic-gate 	 * language-dependent error message.
1077c478bd9Sstevel@tonic-gate 	 */
1087c478bd9Sstevel@tonic-gate 	if (buflen < (strlen(buf) + 1)) {
1097c478bd9Sstevel@tonic-gate 		ret = errno = ERANGE;
1107c478bd9Sstevel@tonic-gate 	} else {
1117c478bd9Sstevel@tonic-gate 		(void) strcpy(strerrbuf, buf);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	return (ret);
1157c478bd9Sstevel@tonic-gate }
116