Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 3529fbf0 in mainline for uspace/lib/c/generic/str_error.c


Ignore:
Timestamp:
2017-12-19T15:18:40Z (4 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master
Children:
74a2a2d
Parents:
bdb8ba9
Message:

Ensure that errno.h constants aren't missing textual descriptions.

File:
1 edited

Legend:

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

    rbdb8ba9 r3529fbf0  
    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 // TODO: this file should be generated from errno declarations
     43/* The arrays bellow 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) num,
     51
     52static const int err_num[] = {
     53#include <abi/errno.in>
     54};
     55
     56#undef __errno_entry
     57#define __errno_entry(name, num, desc) #name,
    4458
    4559static const char* err_name[] = {
    46         "EOK",
    47         "ENOENT",
    48         "ENOMEM",
    49         "ELIMIT",
    50         "EREFUSED",
    51         "EFORWARD",
    52         "EPERM",
    53         "EHANGUP",
    54         "EPARTY",
    55         "EEXIST",
    56         "EBADMEM",
    57         "ENOTSUP",
    58         "EADDRNOTAVAIL",
    59         "ETIMEOUT",
    60         "EINVAL",
    61         "EBUSY",
    62         "EOVERFLOW",
    63         "EINTR"
     60#include <abi/errno.in>
    6461};
    6562
     63#undef __errno_entry
     64#define __errno_entry(name, num, desc) "[" #name "]" desc,
     65
    6666static const char* err_desc[] = {
    67         "No error",
    68         "No such entry",
    69         "Not enough memory",
    70         "Limit exceeded",
    71         "Connection refused",
    72         "Forwarding error",
    73         "Permission denied",
    74         "Answerbox closed connection",
    75         "Other party error",
    76         "Entry already exists",
    77         "Bad memory pointer",
    78         "Not supported",
    79         "Address not available",
    80         "Timeout expired",
    81         "Invalid value",
    82         "Resource is busy",
    83         "Result does not fit its size",
    84         "Operation interrupted"
     67#include <abi/errno.in>
    8568};
    8669
    8770static fibril_local char noerr[NOERR_LEN];
    8871
     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 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
    8990const char *str_error_name(errno_t e)
    9091{
    91         if ((e <= 0) && (e >= MIN_ERRNO))
    92                 return err_name[-e];
     92        int i = find_errno(e);
    9393
    94         /* Ad hoc descriptions of error codes interesting for USB. */
    95         // FIXME: integrate these as first-class error values
    96         switch (e) {
    97                 case ENOFS:
    98                         return "ENOFS";
    99                 case EBADCHECKSUM:
    100                         return "EBADCHECKSUM";
    101                 case ESTALL:
    102                         return "ESTALL";
    103                 case EAGAIN:
    104                         return "EAGAIN";
    105                 case EEMPTY:
    106                         return "EEMPTY";
    107                 default:
    108                         break;
     94        if (i >= 0) {
     95                return err_name[i];
    10996        }
    11097
     
    115102const char *str_error(errno_t e)
    116103{
    117         if ((e <= 0) && (e >= MIN_ERRNO))
    118                 return err_desc[-e];
    119        
    120         /* Ad hoc descriptions of error codes interesting for USB. */
    121         // FIXME: integrate these as first-class error values
    122         switch (e) {
    123                 case ENOFS:
    124                         return "No such file system type";
    125                 case EBADCHECKSUM:
    126                         return "Bad checksum";
    127                 case ESTALL:
    128                         return "Operation stalled";
    129                 case EAGAIN:
    130                         return "Resource temporarily unavailable";
    131                 case EEMPTY:
    132                         return "Resource is empty";
    133                 default:
    134                         break;
     104        int i = find_errno(e);
     105
     106        if (i >= 0) {
     107                return err_desc[i];
    135108        }
    136109
Note: See TracChangeset for help on using the changeset viewer.