summaryrefslogtreecommitdiff
path: root/libc/string/strerror.c
blob: 80f82de251c12f94a233d09ee115dae4325b6d16 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB.  If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA.  */

/*
 * Manuel Novoa III       Dec 2000
 *
 * Converted to use my new (un)signed long (long) to string routines, which
 * are smaller than the previous functions and don't require static buffers.
 * Removed dependence on strcat in the process.
 *
 * Also appended a test routine ( -DCHECK_BUF ) to allow a quick check
 * on the buffer length when the sys_errorlist is modified.
 *
 * Added the option WANT_ERRORLIST for low-memory applications to omit the
 * error message strings and only output the error number.
 *
 * Manuel Novoa III       Feb 2002
 *
 * Change to _int10tostr and fix a bug in end-of-buf arg.
 *
 * Erik Andersen          June 2002
 *
 * Added strerror_r (per SuSv3 which differs from glibc) and adapted
 * strerror to match.
 */

#define WANT_ERRORLIST     1

#define _STDIO_UTILITY			/* For _int10tostr. */
#include <stdio.h>
#include <string.h>
#include <errno.h>

int strerror_r(int err, char *retbuf, size_t buflen)
{
#if WANT_ERRORLIST
    if (err < 0 ||  err >= sys_nerr || sys_errlist[err] == NULL) {
	return -EINVAL;
    }
    if (retbuf==NULL || buflen < 1) {
	return -ERANGE;
    }
    strncpy(retbuf, sys_errlist[err], buflen);
    retbuf[buflen-1] = '\0';
    return 0;
#else
    char *pos;
    static const char unknown_error[] = "Unknown Error: errno"; /* = */

    if (err < 0 ||  err >= sys_nerr || sys_errlist[err] == NULL) {
	return -EINVAL;
    }
    /* unknown error -- leave space for the '=' */
    pos = _int10tostr(retbuf+sizeof(retbuf)-1, err) - sizeof(unknown_error);
    strcpy(pos, unknown_error);
    *(pos + sizeof(unknown_error) - 1) = '=';
    return 0;
#endif
}

/* Return a string descibing the errno code in ERRNUM.
   The storage is good only until the next call to strerror.
   Writing to the storage causes undefined behavior.  */
char *strerror(int err)
{
#if WANT_ERRORLIST
    static char retbuf[48];
#else
#if __BUFLEN_INT10TOSTR > 12
#error currently set up for 32 bit ints max!
#endif
    static char retbuf[33];			/* 33 is sufficient for 32 bit ints */
#endif
    
    if (strerror_r(err, retbuf, sizeof(retbuf)) != 0) {
	return NULL;
    }
    return(retbuf);
}

#ifdef CHECK_BUF
/* quick way to check buffer length */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
	int max = 0;
	int j, retcode;
	char *p;
#if WANT_ERRORLIST
	int i;
#endif

	retcode = EXIT_SUCCESS;

#if WANT_ERRORLIST
	for ( i=0 ; i < sys_nerr ; i++ ) {
		j = strlen(sys_errlist[i])+1;
		if (j > max) max = j;
	}
#endif

	p = strerror(INT_MIN);
	j = retbuf+sizeof(retbuf) - p;
	if ( > max) {
	    max = j;
	    printf("strerror.c - Test of INT_MIN: <%s>  %d\n", p, j);
	}

	if (sizeof(retbuf) != max) {
		printf("Error: strerror.c - dimension of retbuf should be = %d\n", max);
		retcode = EXIT_FAILURE;
	}
	printf("Passed.\n");

	return retcode;
}
#endif