File: strerror_r.c

package info (click to toggle)
picolibc 1.8.11-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 50,064 kB
  • sloc: ansic: 404,031; asm: 24,984; sh: 2,585; python: 2,289; perl: 680; pascal: 329; exp: 287; makefile: 222; cpp: 71; xml: 40
file content (77 lines) | stat: -rw-r--r-- 2,875 bytes parent folder | download
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
/* Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com> */
/* GNU variant of strerror_r. */
/*
FUNCTION
        <<strerror_r>>---convert error number to string and copy to buffer

INDEX
        strerror_r

SYNOPSIS
        #include <string.h>
        #ifdef _GNU_SOURCE
        char *strerror_r(int <[errnum]>, char *<[buffer]>, size_t <[n]>);
        #else
        int strerror_r(int <[errnum]>, char *<[buffer]>, size_t <[n]>);
        #endif

DESCRIPTION
<<strerror_r>> converts the error number <[errnum]> into a
string and copies the result into the supplied <[buffer]> for
a length up to <[n]>, including the NUL terminator. The value of
<[errnum]> is usually a copy of <<errno>>.  If <<errnum>> is not a known
error number, the result is the empty string.

See <<strerror>> for how strings are mapped to <<errnum>>.

RETURNS
There are two variants: the GNU version always returns a NUL-terminated
string, which is <[buffer]> if all went well, but which is another
pointer if <[n]> was too small (leaving <[buffer]> untouched).  If the
return is not <[buffer]>, your application must not modify that string.
The POSIX version returns 0 on success, <[EINVAL]> if <<errnum>> was not
recognized, and <[ERANGE]> if <[n]> was too small.  The variant chosen
depends on macros that you define before inclusion of <<string.h>>.

PORTABILITY
<<strerror_r>> with a <[char *]> result is a GNU extension.
<<strerror_r>> with an <[int]> result is required by POSIX 2001.
This function is compliant only if <<_user_strerror>> is not provided,
or if it is thread-safe and uses separate storage according to whether
the second argument of that function is non-zero.  For more details
on <<_user_strerror>>, see the <<strerror>> documentation.

POSIX states that the contents of <[buf]> are unspecified on error,
although this implementation guarantees a NUL-terminated string for
all except <[n]> of 0.

POSIX recommends that unknown <[errnum]> result in a message including
that value, however it is not a requirement and this implementation
provides only an empty string (unless you provide <<_user_strerror>>).
POSIX also recommends that unknown <[errnum]> fail with EINVAL even
when providing such a message, however it is not a requirement and
this implementation will return success if <<_user_strerror>> provided
a non-empty alternate string without assigning into its third argument.

<<strerror_r>> requires no supporting OS subroutines.

*/

#undef __STRICT_ANSI__
#define _GNU_SOURCE
#include <errno.h>
#include <string.h>
#include "local.h"
#undef strerror_r

/* For backwards-compatible linking, this must be the GNU signature;
   see xpg_strerror_r.c for the POSIX version.  */
char *
strerror_r(int errnum, char *buffer, size_t n)
{
    char *error = _strerror_r(errnum, 1, NULL);

    if (strlen(error) >= n)
        return error;
    return strcpy(buffer, error);
}