xref: /onnv-gate/usr/src/lib/libast/common/string/strerror.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin 
244887Schin /*
254887Schin  * Glenn Fowler
264887Schin  * AT&T Research
274887Schin  *
284887Schin  * return error message string given errno
294887Schin  */
304887Schin 
31*12068SRoger.Faulkner@Oracle.COM #include "lclib.h"
324887Schin 
334887Schin #include "FEATURE/errno"
344887Schin 
354887Schin #undef	strerror
364887Schin 
374887Schin #if !defined(sys_errlist) && !_def_errno_sys_errlist
384887Schin #if _dat_sys_errlist
394887Schin extern char*	sys_errlist[];
404887Schin #else
414887Schin #undef		_dat_sys_nerr
424887Schin char*		sys_errlist[] = { 0 };
434887Schin #endif
444887Schin #endif
454887Schin 
464887Schin #if !defined(sys_nerr) && !_def_errno_sys_nerr
474887Schin #if _dat_sys_nerr
484887Schin extern int	sys_nerr;
494887Schin #else
504887Schin #undef		_dat_sys_nerr
514887Schin int		sys_nerr = 0;
524887Schin #endif
534887Schin #endif
544887Schin 
554887Schin #if _lib_strerror
564887Schin extern char*	strerror(int);
574887Schin #endif
584887Schin 
594887Schin #if _PACKAGE_astsa
604887Schin 
614887Schin #define fmtbuf(n)	((n),tmp)
624887Schin 
634887Schin static char		tmp[32];
644887Schin 
654887Schin #endif
664887Schin 
674887Schin char*
_ast_strerror(int err)684887Schin _ast_strerror(int err)
694887Schin {
704887Schin 	char*		msg;
714887Schin 	int		z;
724887Schin 
734887Schin #if _lib_strerror
744887Schin 	z = errno;
754887Schin 	msg = strerror(err);
764887Schin 	errno = z;
774887Schin #else
784887Schin 	if (err > 0 && err <= sys_nerr)
794887Schin 		msg = (char*)sys_errlist[err];
804887Schin 	else
814887Schin 		msg = 0;
824887Schin #endif
834887Schin 	if (msg)
844887Schin 	{
854887Schin #if !_PACKAGE_astsa
864887Schin 		if (ERROR_translating())
874887Schin 		{
884887Schin #if _lib_strerror
894887Schin 			static int	sys;
904887Schin 
914887Schin 			if (!sys)
924887Schin 			{
934887Schin 				char*	s;
944887Schin 				char*	t;
954887Schin 				char*	p;
964887Schin 
974887Schin #if _lib_strerror
984887Schin 				/*
994887Schin 				 * stash the pending strerror() msg
1004887Schin 				 */
1014887Schin 
1024887Schin 				msg = strcpy(fmtbuf(strlen(msg) + 1), msg);
1034887Schin #endif
1044887Schin 
1054887Schin 				/*
1064887Schin 				 * make sure that strerror() translates
1074887Schin 				 */
1084887Schin 
1094887Schin 				if (!(s = strerror(1)))
1104887Schin 					sys = -1;
1114887Schin 				else
1124887Schin 				{
1134887Schin 					t = fmtbuf(z = strlen(s) + 1);
1144887Schin 					strcpy(t, s);
115*12068SRoger.Faulkner@Oracle.COM 					ast.locale.set |= AST_LC_internal;
1164887Schin 					p = setlocale(LC_MESSAGES, NiL);
1174887Schin 					setlocale(LC_MESSAGES, "C");
1184887Schin 					sys = (s = strerror(1)) && strcmp(s, t) ? 1 : -1;
1194887Schin 					setlocale(LC_MESSAGES, p);
120*12068SRoger.Faulkner@Oracle.COM 					ast.locale.set &= ~AST_LC_internal;
1214887Schin 				}
1224887Schin 			}
1234887Schin 			if (sys > 0)
1244887Schin 				return msg;
1254887Schin #endif
1264887Schin 			return ERROR_translate(NiL, NiL, "errlist", msg);
1274887Schin 		}
1284887Schin #endif
1294887Schin 		return msg;
1304887Schin 	}
1314887Schin 	msg = fmtbuf(z = 32);
1324887Schin 	sfsprintf(msg, z, ERROR_translate(NiL, NiL, "errlist", "Error %d"), err);
1334887Schin 	return msg;
1344887Schin }
1354887Schin 
1364887Schin #if !_lib_strerror
1374887Schin 
1384887Schin #if defined(__EXPORT__)
1394887Schin #define extern		__EXPORT__
1404887Schin #endif
1414887Schin 
1424887Schin extern char*
strerror(int err)1434887Schin strerror(int err)
1444887Schin {
1454887Schin 	return _ast_strerror(err);
1464887Schin }
1474887Schin 
1484887Schin #endif
149