| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "errno-list.h" |
| 8 |
|
| 9 |
static const struct errno_name* lookup_errno(register const char *str, |
| 10 |
register GPERF_LEN_TYPE len); |
| 11 |
|
| 12 |
#include "errno-from-name.inc" |
| 13 |
|
| 14 |
int errno_from_name(const char *name) { |
| 15 |
const struct errno_name *sc; |
| 16 |
|
| 17 |
assert(name); |
| 18 |
|
| 19 |
sc = lookup_errno(name, strlen(name)); |
| 20 |
if (!sc) |
| 21 |
return -EINVAL; |
| 22 |
|
| 23 |
assert(sc->id > 0); |
| 24 |
return sc->id; |
| 25 |
} |
| 26 |
|
| 27 |
#ifdef __GLIBC__ |
| 28 |
const char* errno_name_no_fallback(int id) { |
| 29 |
if (id == 0) /* To stay in line with our implementation below. */ |
| 30 |
return NULL; |
| 31 |
|
| 32 |
return strerrorname_np(ABS(id)); |
| 33 |
} |
| 34 |
#else |
| 35 |
# include "errno-to-name.inc" |
| 36 |
|
| 37 |
const char* errno_name_no_fallback(int id) { |
| 38 |
if (id < 0) |
| 39 |
id = -id; |
| 40 |
|
| 41 |
if ((size_t) id >= ELEMENTSOF(errno_names)) |
| 42 |
return NULL; |
| 43 |
|
| 44 |
return errno_names[id]; |
| 45 |
} |
| 46 |
#endif |
| 47 |
|
| 48 |
const char* errno_name(int id, char buf[static ERRNO_NAME_BUF_LEN]) { |
| 49 |
const char *a = errno_name_no_fallback(id); |
| 50 |
if (a) |
| 51 |
return a; |
| 52 |
snprintf(buf, ERRNO_NAME_BUF_LEN, "%d", abs(id)); |
| 53 |
return buf; |
| 54 |
} |