source: mainline/uspace/lib/c/generic/str_error.c@ eb13ef8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since eb13ef8 was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

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