-
Notifications
You must be signed in to change notification settings - Fork 97
/
strerror.c
67 lines (50 loc) · 1.66 KB
/
strerror.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*---------------------------------------------------------------------------*\
NAME
strerror - get string representation of errno value
DESCRIPTION
Replacement for standard C strerror() function, in case it's missing.
Not thread-safe.
LICENSE
This source code is released under a BSD-style license. See the
LICENSE file for details.
Copyright (c) 2003-2015 Brian M. Clapper, [email protected]
\*---------------------------------------------------------------------------*/
#define MAX_INT_DIGITS (11) /* max. digits in a LONG (w/ sign) */
char *strerror (const int errno)
{
extern char *sys_errlist []; /* list of error messages */
extern int sys_nerr; /* size of sys_errlist */
static char buffer [MAX_INT_DIGITS + 1];
char *result; /* function result */
if ( (errno >= 0) && (errno < sys_nerr) )
result = sys_errlist[errno];
else
{
/*
To avoid possible buffer overflow, avoid using sprintf() with
"%d". snprintf() isn't available everywhere, so we convert the
error code to a string manually. This is ASCII/ISO-8859-1 specific.
*/
int i = MAX_INT_DIGITS;
int save_errno = errno;
int neg = 0;
int stop = 0;
buffer[i] = '\0';
if (save_errno < 0)
{
neg++;
stop = 1;
save_errno = -save_errno;
}
while ( (--i >= stop) && (save_errno != 0) )
{
int digit = save_errno % 10;
save_errno /= 10;
buffer[i] = (char) (digit + '0');
}
if (neg)
buffer[i] = '-';
result = &(buffer[i]);
}
return (result);
}