[0a72efc] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Martin Decky
|
---|
[3529fbf0] | 3 | * Copyright (c) 2017 CZ.NIC, z.s.p.o.
|
---|
[0a72efc] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libc
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[55f690c] | 36 | #include <errno.h>
|
---|
[2721a75] | 37 | #include <str_error.h>
|
---|
[0a72efc] | 38 | #include <stdio.h>
|
---|
| 39 | #include <fibril.h>
|
---|
| 40 |
|
---|
| 41 | #define NOERR_LEN 64
|
---|
| 42 |
|
---|
[74a2a2d] | 43 | /* The arrays below are automatically generated from the same file that
|
---|
[3529fbf0] | 44 | * errno.h constants are generated from. Triple-include of the same list
|
---|
| 45 | * with redefinitions of __errno() macro are used to ensure that the
|
---|
| 46 | * information cannot get out of synch. This is inpired by musl libc.
|
---|
| 47 | */
|
---|
| 48 |
|
---|
| 49 | #undef __errno_entry
|
---|
| 50 | #define __errno_entry(name, num, desc) num,
|
---|
| 51 |
|
---|
| 52 | static const int err_num[] = {
|
---|
| 53 | #include <abi/errno.in>
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | #undef __errno_entry
|
---|
| 57 | #define __errno_entry(name, num, desc) #name,
|
---|
[9af1c61] | 58 |
|
---|
| 59 | static const char* err_name[] = {
|
---|
[3529fbf0] | 60 | #include <abi/errno.in>
|
---|
[9af1c61] | 61 | };
|
---|
| 62 |
|
---|
[3529fbf0] | 63 | #undef __errno_entry
|
---|
| 64 | #define __errno_entry(name, num, desc) "[" #name "]" desc,
|
---|
| 65 |
|
---|
[0a72efc] | 66 | static const char* err_desc[] = {
|
---|
[3529fbf0] | 67 | #include <abi/errno.in>
|
---|
[0a72efc] | 68 | };
|
---|
| 69 |
|
---|
| 70 | static fibril_local char noerr[NOERR_LEN];
|
---|
| 71 |
|
---|
[3529fbf0] | 72 | /* Returns index corresponding to the given errno, or -1 if not found. */
|
---|
| 73 | static int find_errno(errno_t e)
|
---|
| 74 | {
|
---|
| 75 | /* Just a dumb linear search.
|
---|
[6ef9d9a] | 76 | * There are too few entries to warrant anything smarter.
|
---|
[3529fbf0] | 77 | */
|
---|
| 78 |
|
---|
| 79 | int len = sizeof(err_num) / sizeof(errno_t);
|
---|
| 80 |
|
---|
| 81 | for (int i = 0; i < len; i++) {
|
---|
| 82 | if (err_num[i] == e) {
|
---|
| 83 | return i;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | return -1;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[9af1c61] | 90 | const char *str_error_name(errno_t e)
|
---|
| 91 | {
|
---|
[3529fbf0] | 92 | int i = find_errno(e);
|
---|
| 93 |
|
---|
| 94 | if (i >= 0) {
|
---|
| 95 | return err_name[i];
|
---|
[9af1c61] | 96 | }
|
---|
| 97 |
|
---|
| 98 | snprintf(noerr, NOERR_LEN, "(%d)", (int)e);
|
---|
| 99 | return noerr;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | const char *str_error(errno_t e)
|
---|
[0a72efc] | 103 | {
|
---|
[3529fbf0] | 104 | int i = find_errno(e);
|
---|
| 105 |
|
---|
| 106 | if (i >= 0) {
|
---|
| 107 | return err_desc[i];
|
---|
[55f690c] | 108 | }
|
---|
| 109 |
|
---|
[9af1c61] | 110 | snprintf(noerr, NOERR_LEN, "Unknown error code (%d)", (int)e);
|
---|
[0a72efc] | 111 | return noerr;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /** @}
|
---|
| 115 | */
|
---|