Changeset 3529fbf0 in mainline for uspace/lib/c/generic/str_error.c
- Timestamp:
- 2017-12-19T15:18:40Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 74a2a2d
- Parents:
- bdb8ba9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/str_error.c
rbdb8ba9 r3529fbf0 1 1 /* 2 2 * Copyright (c) 2010 Martin Decky 3 * Copyright (c) 2017 CZ.NIC, z.s.p.o. 3 4 * All rights reserved. 4 5 * … … 38 39 #include <fibril.h> 39 40 40 #define MIN_ERRNO -1741 41 #define NOERR_LEN 64 42 42 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 52 static const int err_num[] = { 53 #include <abi/errno.in> 54 }; 55 56 #undef __errno_entry 57 #define __errno_entry(name, num, desc) #name, 44 58 45 59 static 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> 64 61 }; 65 62 63 #undef __errno_entry 64 #define __errno_entry(name, num, desc) "[" #name "]" desc, 65 66 66 static 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> 85 68 }; 86 69 87 70 static fibril_local char noerr[NOERR_LEN]; 88 71 72 /* Returns index corresponding to the given errno, or -1 if not found. */ 73 static 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 89 90 const char *str_error_name(errno_t e) 90 91 { 91 if ((e <= 0) && (e >= MIN_ERRNO)) 92 return err_name[-e]; 92 int i = find_errno(e); 93 93 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]; 109 96 } 110 97 … … 115 102 const char *str_error(errno_t e) 116 103 { 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]; 135 108 } 136 109
Note:
See TracChangeset
for help on using the changeset viewer.