1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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 <ast.h>
32 #include <error.h>
33 
34 #include "FEATURE/errno"
35 
36 #undef	strerror
37 
38 #if !defined(sys_errlist) && !_def_errno_sys_errlist
39 #if _dat_sys_errlist
40 extern char*	sys_errlist[];
41 #else
42 #undef		_dat_sys_nerr
43 char*		sys_errlist[] = { 0 };
44 #endif
45 #endif
46 
47 #if !defined(sys_nerr) && !_def_errno_sys_nerr
48 #if _dat_sys_nerr
49 extern int	sys_nerr;
50 #else
51 #undef		_dat_sys_nerr
52 int		sys_nerr = 0;
53 #endif
54 #endif
55 
56 #if _lib_strerror
57 extern char*	strerror(int);
58 #endif
59 
60 #if _PACKAGE_astsa
61 
62 #define fmtbuf(n)	((n),tmp)
63 
64 static char		tmp[32];
65 
66 #endif
67 
68 char*
69 _ast_strerror(int err)
70 {
71 	char*		msg;
72 	int		z;
73 
74 #if _lib_strerror
75 	z = errno;
76 	msg = strerror(err);
77 	errno = z;
78 #else
79 	if (err > 0 && err <= sys_nerr)
80 		msg = (char*)sys_errlist[err];
81 	else
82 		msg = 0;
83 #endif
84 	if (msg)
85 	{
86 #if !_PACKAGE_astsa
87 		if (ERROR_translating())
88 		{
89 #if _lib_strerror
90 			static int	sys;
91 
92 			if (!sys)
93 			{
94 				char*	s;
95 				char*	t;
96 				char*	p;
97 
98 #if _lib_strerror
99 				/*
100 				 * stash the pending strerror() msg
101 				 */
102 
103 				msg = strcpy(fmtbuf(strlen(msg) + 1), msg);
104 #endif
105 
106 				/*
107 				 * make sure that strerror() translates
108 				 */
109 
110 				if (!(s = strerror(1)))
111 					sys = -1;
112 				else
113 				{
114 					t = fmtbuf(z = strlen(s) + 1);
115 					strcpy(t, s);
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 				}
121 			}
122 			if (sys > 0)
123 				return msg;
124 #endif
125 			return ERROR_translate(NiL, NiL, "errlist", msg);
126 		}
127 #endif
128 		return msg;
129 	}
130 	msg = fmtbuf(z = 32);
131 	sfsprintf(msg, z, ERROR_translate(NiL, NiL, "errlist", "Error %d"), err);
132 	return msg;
133 }
134 
135 #if !_lib_strerror
136 
137 #if defined(__EXPORT__)
138 #define extern		__EXPORT__
139 #endif
140 
141 extern char*
142 strerror(int err)
143 {
144 	return _ast_strerror(err);
145 }
146 
147 #endif
148