source: mainline/uspace/lib/c/generic/str_error.c@ 3061bc1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3061bc1 was 68a2fab2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Use defined macros instead of literal number in str_error.c array.
This makes the code more robust against changes to the way errno_t type
is defined.

  • Property mode set to 100644
File size: 3.0 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/** @addtogroup libc
31 * @{
32 */
33/** @file
34 */
35
36#include <errno.h>
37#include <str_error.h>
38#include <stdio.h>
39#include <fibril.h>
40
41#define NOERR_LEN 64
42
43/* The arrays below are automatically generated from the same file that
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) name,
51
52static const errno_t err_num[] = {
53#include <abi/errno.in>
54};
55
56#undef __errno_entry
57#define __errno_entry(name, num, desc) #name,
58
59static const char* err_name[] = {
60#include <abi/errno.in>
61};
62
63#undef __errno_entry
64#define __errno_entry(name, num, desc) "[" #name "]" desc,
65
66static const char* err_desc[] = {
67#include <abi/errno.in>
68};
69
70static fibril_local char noerr[NOERR_LEN];
71
72/* Returns index corresponding to the given errno, or -1 if not found. */
73static int find_errno(errno_t e)
74{
75 /* Just a dumb linear search.
76 * There are too few entries to warrant anything smarter.
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
90const char *str_error_name(errno_t e)
91{
92 int i = find_errno(e);
93
94 if (i >= 0) {
95 return err_name[i];
96 }
97
98 snprintf(noerr, NOERR_LEN, "(%d)", (int)e);
99 return noerr;
100}
101
102const char *str_error(errno_t e)
103{
104 int i = find_errno(e);
105
106 if (i >= 0) {
107 return err_desc[i];
108 }
109
110 snprintf(noerr, NOERR_LEN, "Unknown error code (%d)", (int)e);
111 return noerr;
112}
113
114/** @}
115 */
Note: See TracBrowser for help on using the repository browser.