[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 |
|
---|
[7c3fb9b] | 43 | /*
|
---|
| 44 | * The arrays below are automatically generated from the same file that
|
---|
[3529fbf0] | 45 | * errno.h constants are generated from. Triple-include of the same list
|
---|
| 46 | * with redefinitions of __errno() macro are used to ensure that the
|
---|
| 47 | * information cannot get out of synch. This is inpired by musl libc.
|
---|
| 48 | */
|
---|
| 49 |
|
---|
| 50 | #undef __errno_entry
|
---|
[68a2fab2] | 51 | #define __errno_entry(name, num, desc) name,
|
---|
[3529fbf0] | 52 |
|
---|
[68a2fab2] | 53 | static const errno_t err_num[] = {
|
---|
[3529fbf0] | 54 | #include <abi/errno.in>
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | #undef __errno_entry
|
---|
| 58 | #define __errno_entry(name, num, desc) #name,
|
---|
[9af1c61] | 59 |
|
---|
[1433ecda] | 60 | static const char *err_name[] = {
|
---|
[3529fbf0] | 61 | #include <abi/errno.in>
|
---|
[9af1c61] | 62 | };
|
---|
| 63 |
|
---|
[3529fbf0] | 64 | #undef __errno_entry
|
---|
| 65 | #define __errno_entry(name, num, desc) "[" #name "]" desc,
|
---|
| 66 |
|
---|
[1433ecda] | 67 | static const char *err_desc[] = {
|
---|
[3529fbf0] | 68 | #include <abi/errno.in>
|
---|
[0a72efc] | 69 | };
|
---|
| 70 |
|
---|
| 71 | static fibril_local char noerr[NOERR_LEN];
|
---|
| 72 |
|
---|
[3529fbf0] | 73 | /* Returns index corresponding to the given errno, or -1 if not found. */
|
---|
| 74 | static int find_errno(errno_t e)
|
---|
| 75 | {
|
---|
[7c3fb9b] | 76 | /*
|
---|
| 77 | * Just a dumb linear search.
|
---|
[6ef9d9a] | 78 | * There are too few entries to warrant anything smarter.
|
---|
[3529fbf0] | 79 | */
|
---|
| 80 |
|
---|
| 81 | int len = sizeof(err_num) / sizeof(errno_t);
|
---|
| 82 |
|
---|
| 83 | for (int i = 0; i < len; i++) {
|
---|
| 84 | if (err_num[i] == e) {
|
---|
| 85 | return i;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | return -1;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[9af1c61] | 92 | const char *str_error_name(errno_t e)
|
---|
| 93 | {
|
---|
[3529fbf0] | 94 | int i = find_errno(e);
|
---|
| 95 |
|
---|
| 96 | if (i >= 0) {
|
---|
| 97 | return err_name[i];
|
---|
[9af1c61] | 98 | }
|
---|
| 99 |
|
---|
| 100 | snprintf(noerr, NOERR_LEN, "(%d)", (int)e);
|
---|
| 101 | return noerr;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | const char *str_error(errno_t e)
|
---|
[0a72efc] | 105 | {
|
---|
[3529fbf0] | 106 | int i = find_errno(e);
|
---|
| 107 |
|
---|
| 108 | if (i >= 0) {
|
---|
| 109 | return err_desc[i];
|
---|
[55f690c] | 110 | }
|
---|
| 111 |
|
---|
[9af1c61] | 112 | snprintf(noerr, NOERR_LEN, "Unknown error code (%d)", (int)e);
|
---|
[0a72efc] | 113 | return noerr;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /** @}
|
---|
| 117 | */
|
---|