source: mainline/common/str_error.c@ e2b417f

Last change on this file since e2b417f was fdfb24e, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 20 months ago

Deduplicate string related functions

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * Copyright (c) 2010 Martin Decky
3 * Copyright (c) 2017 CZ.NIC, z.s.p.o.
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#include <str_error.h>
31
32#include <errno.h>
33#include <stddef.h>
34
35
36/*
37 * The arrays below are automatically generated from the same file that
38 * errno.h constants are generated from. Triple-include of the same list
39 * with redefinitions of __errno() macro are used to ensure that the
40 * information cannot get out of synch. This is inpired by musl libc.
41 */
42
43#undef __errno_entry
44#define __errno_entry(name, num, desc) name,
45
46static const errno_t err_num[] = {
47#include <abi/errno.in>
48};
49
50#undef __errno_entry
51#define __errno_entry(name, num, desc) #name,
52
53static const char *err_name[] = {
54#include <abi/errno.in>
55};
56
57#undef __errno_entry
58#define __errno_entry(name, num, desc) "[" #name "] " desc,
59
60static const char *err_desc[] = {
61#include <abi/errno.in>
62};
63
64/* Returns index corresponding to the given errno, or -1 if not found. */
65static int find_errno(errno_t e)
66{
67 /*
68 * Just a dumb linear search.
69 * There are too few entries to warrant anything smarter.
70 */
71
72 int len = sizeof(err_num) / sizeof(errno_t);
73
74 for (int i = 0; i < len; i++) {
75 if (err_num[i] == e) {
76 return i;
77 }
78 }
79
80 return -1;
81}
82
83const char *str_error_name(errno_t e)
84{
85 int i = find_errno(e);
86
87 if (i >= 0) {
88 return err_name[i];
89 }
90
91 return NULL;
92}
93
94const char *str_error(errno_t e)
95{
96 int i = find_errno(e);
97
98 if (i >= 0) {
99 return err_desc[i];
100 }
101
102 return NULL;
103}
Note: See TracBrowser for help on using the repository browser.