Changeset 132ab5d1 in mainline for uspace/lib/c/generic/str_error.c


Ignore:
Timestamp:
2018-01-30T03:20:45Z (7 years ago)
Author:
Jenda <jenda.jzqk73@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5a6cc679
Parents:
8bfb163 (diff), 6a5d05b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge commit '6a5d05bd2551e64111bea4f9332dd7448c26ce84' into forwardport

Separate return value from error code in gen_irq_code*().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/str_error.c

    r8bfb163 r132ab5d1  
    11/*
    22 * Copyright (c) 2010 Martin Decky
     3 * Copyright (c) 2017 CZ.NIC, z.s.p.o.
    34 * All rights reserved.
    45 *
     
    3839#include <fibril.h>
    3940
    40 #define MIN_ERRNO  -17
    4141#define NOERR_LEN  64
    4242
     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
    4366static const char* err_desc[] = {
    44         "No error",
    45         "No such entry",
    46         "Not enough memory",
    47         "Limit exceeded",
    48         "Connection refused",
    49         "Forwarding error",
    50         "Permission denied",
    51         "Answerbox closed connection",
    52         "Other party error",
    53         "Entry already exists",
    54         "Bad memory pointer",
    55         "Not supported",
    56         "Address not available",
    57         "Timeout expired",
    58         "Invalid value",
    59         "Resource is busy",
    60         "Result does not fit its size",
    61         "Operation interrupted"
     67#include <abi/errno.in>
    6268};
    6369
    6470static fibril_local char noerr[NOERR_LEN];
    6571
    66 const char *str_error(const int e)
     72/* Returns index corresponding to the given errno, or -1 if not found. */
     73static int find_errno(errno_t e)
    6774{
    68         if ((e <= 0) && (e >= MIN_ERRNO))
    69                 return err_desc[-e];
    70        
    71         /* Ad hoc descriptions of error codes interesting for USB. */
    72         // FIXME: integrate these as first-class error values
    73         switch (e) {
    74                 case ENOFS:
    75                         return "No such file system type";
    76                 case EBADCHECKSUM:
    77                         return "Bad checksum";
    78                 case ESTALL:
    79                         return "Operation stalled";
    80                 case EAGAIN:
    81                         return "Resource temporarily unavailable";
    82                 case EEMPTY:
    83                         return "Resource is empty";
    84                 default:
    85                         break;
     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                }
    8685        }
    8786
    88         snprintf(noerr, NOERR_LEN, "Unkown error code %d", e);
     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);
    89111        return noerr;
    90112}
Note: See TracChangeset for help on using the changeset viewer.