1/***********************************************************************
2*                                                                      *
3*               This software is part of the ast package               *
4*          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5*                      and is licensed under the                       *
6*                  Common Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*            http://www.opensource.org/licenses/cpl1.0.txt             *
11*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*                                                                      *
13*              Information and Software Systems Research               *
14*                            AT&T Research                             *
15*                           Florham Park NJ                            *
16*                                                                      *
17*                 Glenn Fowler <gsf@research.att.com>                  *
18*                  David Korn <dgk@research.att.com>                   *
19*                   Phong Vo <kpv@research.att.com>                    *
20*                                                                      *
21***********************************************************************/
22#pragma prototyped
23
24/*
25 * Glenn Fowler
26 * AT&T Research
27 *
28 * return error message string given errno
29 */
30
31#include "lclib.h"
32
33#include "FEATURE/errno"
34
35#undef	strerror
36
37#if !defined(sys_errlist) && !_def_errno_sys_errlist
38#if _dat_sys_errlist
39extern char*	sys_errlist[];
40#else
41#undef		_dat_sys_nerr
42char*		sys_errlist[] = { 0 };
43#endif
44#endif
45
46#if !defined(sys_nerr) && !_def_errno_sys_nerr
47#if _dat_sys_nerr
48extern int	sys_nerr;
49#else
50#undef		_dat_sys_nerr
51int		sys_nerr = 0;
52#endif
53#endif
54
55#if _lib_strerror
56extern char*	strerror(int);
57#endif
58
59#if _PACKAGE_astsa
60
61#define fmtbuf(n)	((n),tmp)
62
63static char		tmp[32];
64
65#endif
66
67char*
68_ast_strerror(int err)
69{
70	char*		msg;
71	int		z;
72
73#if _lib_strerror
74	z = errno;
75	msg = strerror(err);
76	errno = z;
77#else
78	if (err > 0 && err <= sys_nerr)
79		msg = (char*)sys_errlist[err];
80	else
81		msg = 0;
82#endif
83	if (msg)
84	{
85#if !_PACKAGE_astsa
86		if (ERROR_translating())
87		{
88#if _lib_strerror
89			static int	sys;
90
91			if (!sys)
92			{
93				char*	s;
94				char*	t;
95				char*	p;
96
97#if _lib_strerror
98				/*
99				 * stash the pending strerror() msg
100				 */
101
102				msg = strcpy(fmtbuf(strlen(msg) + 1), msg);
103#endif
104
105				/*
106				 * make sure that strerror() translates
107				 */
108
109				if (!(s = strerror(1)))
110					sys = -1;
111				else
112				{
113					t = fmtbuf(z = strlen(s) + 1);
114					strcpy(t, s);
115					ast.locale.set |= AST_LC_internal;
116					p = setlocale(LC_MESSAGES, NiL);
117					setlocale(LC_MESSAGES, "C");
118					sys = (s = strerror(1)) && strcmp(s, t) ? 1 : -1;
119					setlocale(LC_MESSAGES, p);
120					ast.locale.set &= ~AST_LC_internal;
121				}
122			}
123			if (sys > 0)
124				return msg;
125#endif
126			return ERROR_translate(NiL, NiL, "errlist", msg);
127		}
128#endif
129		return msg;
130	}
131	msg = fmtbuf(z = 32);
132	sfsprintf(msg, z, ERROR_translate(NiL, NiL, "errlist", "Error %d"), err);
133	return msg;
134}
135
136#if !_lib_strerror
137
138#if defined(__EXPORT__)
139#define extern		__EXPORT__
140#endif
141
142extern char*
143strerror(int err)
144{
145	return _ast_strerror(err);
146}
147
148#endif
149