1 /*
2    Portability layer for error codes
3 
4    Copyright (C) Amitay Isaacs  2018
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /*
21  * These errors are as listed in POSIX standard
22  * IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008)
23  *
24  * Error codes marked obsolete are removed (ENODATA, ENOSR, ENOSTR, ETIME)
25  */
26 
27 #include "replace.h"
28 
29 struct {
30 	const char *label;
31 	int code;
32 } err_codes[] = {
33 	{ "E2BIG", E2BIG },
34 
35 	{ "EACCES", EACCES },
36 	{ "EADDRINUSE", EADDRINUSE },
37 	{ "EADDRNOTAVAIL", EADDRNOTAVAIL },
38 	{ "EAFNOSUPPORT", EAFNOSUPPORT },
39 	{ "EAGAIN", EAGAIN },
40 	{ "EALREADY", EALREADY },
41 
42 	{ "EBADF", EBADF },
43 	{ "EBADMSG", EBADMSG },
44 	{ "EBUSY", EBUSY },
45 
46 	{ "ECANCELED", ECANCELED },
47 	{ "ECHILD", ECHILD },
48 	{ "ECONNABORTED", ECONNABORTED },
49 	{ "ECONNREFUSED", ECONNREFUSED },
50 	{ "ECONNRESET", ECONNRESET },
51 
52 	{ "EDEADLK", EDEADLK },
53 	{ "EDESTADDRREQ", EDESTADDRREQ },
54 	{ "EDOM", EDOM },
dummy_handler(uint64_t srvid,TDB_DATA data,void * private_data)55 	{ "EDQUOT", EDQUOT },
56 
57 	{ "EEXIST", EEXIST },
58 
59 	{ "EFAULT", EFAULT },
60 	{ "EFBIG", EFBIG },
61 
main(int argc,const char * argv[])62 	{ "EHOSTUNREACH", EHOSTUNREACH },
63 
64 	{ "EIDRM", EIDRM },
65 	{ "EILSEQ", EILSEQ },
66 	{ "EINPROGRESS", EINPROGRESS },
67 	{ "EINTR", EINTR },
68 	{ "EINVAL", EINVAL },
69 	{ "EIO", EIO },
70 	{ "EISCONN", EISCONN },
71 	{ "EISDIR", EISDIR },
72 
73 	{ "ELOOP", ELOOP },
74 
75 	{ "EMFILE", EMFILE },
76 	{ "EMLINK", EMLINK },
77 	{ "EMSGSIZE", EMSGSIZE },
78 	{ "EMULTIHOP", EMULTIHOP },
79 
80 	{ "ENAMETOOLONG", ENAMETOOLONG },
81 	{ "ENETDOWN", ENETDOWN },
82 	{ "ENETRESET", ENETRESET },
83 	{ "ENETUNREACH", ENETUNREACH },
84 	{ "ENFILE", ENFILE },
85 	{ "ENOBUFS", ENOBUFS },
86 	{ "ENODEV", ENODEV },
87 	{ "ENOENT", ENOENT },
88 	{ "ENOEXEC", ENOEXEC },
89 	{ "ENOLCK", ENOLCK },
90 	{ "ENOLINK", ENOLINK },
91 	{ "ENOMEM", ENOMEM },
92 	{ "ENOMSG", ENOMSG },
93 	{ "ENOPROTOOPT", ENOPROTOOPT },
94 	{ "ENOSPC", ENOSPC },
95 	{ "ENOSYS", ENOSYS },
96 	{ "ENOTCONN", ENOTCONN },
97 	{ "ENOTDIR", ENOTDIR },
98 	{ "ENOTEMPTY", ENOTEMPTY },
99 	{ "ENOTSOCK", ENOTSOCK },
100 	{ "ENOTSUP", ENOTSUP },
101 	{ "ENOTTY", ENOTTY },
102 	{ "ENXIO", ENXIO },
103 
104 	{ "EOPNOTSUPP", EOPNOTSUPP },
105 	{ "EOVERFLOW", EOVERFLOW },
106 
107 	{ "EPERM", EPERM },
108 	{ "EPIPE", EPIPE },
109 	{ "EPROTO", EPROTO },
110 	{ "EPROTONOSUPPORT", EPROTONOSUPPORT },
111 	{ "EPROTOTYPE", EPROTOTYPE },
112 
113 	{ "ERANGE", ERANGE },
114 	{ "EROFS", EROFS },
115 
116 	{ "ESPIPE", ESPIPE },
117 	{ "ESRCH", ESRCH },
118 	{ "ESTALE", ESTALE },
119 
120 	{ "ETIMEDOUT", ETIMEDOUT },
121 	{ "ETXTBSY", ETXTBSY },
122 
123 	{ "EWOULDBLOCK", EWOULDBLOCK },
124 
125 	{ "EXDEV", EXDEV },
126 };
127 
128 static void dump(void)
129 {
130 	size_t i;
131 
132 	for (i=0; i<ARRAY_SIZE(err_codes); i++) {
133 		printf("%s %d\n", err_codes[i].label, err_codes[i].code);
134 	}
135 }
136 
137 static void match_label(const char *str)
138 {
139 	int code = -1;
140 	size_t i;
141 
142 	for (i=0; i<ARRAY_SIZE(err_codes); i++) {
143 		if (strcasecmp(err_codes[i].label, str) == 0) {
144 			code = err_codes[i].code;
145 			break;
146 		}
147 	}
148 
149 	printf("%d\n", code);
150 }
151 
152 static void match_code(int code)
153 {
154 	const char *label = "UNKNOWN";
155 	size_t i;
156 
157 	for (i=0; i<ARRAY_SIZE(err_codes); i++) {
158 		if (err_codes[i].code == code) {
159 			label = err_codes[i].label;
160 			break;
161 		}
162 	}
163 
164 	printf("%s\n", label);
165 }
166 
167 int main(int argc, const char **argv)
168 {
169 	long int code;
170 	char *endptr;
171 
172 	if (argc != 2) {
173 		fprintf(stderr, "Usage: %s dump|<errcode>\n", argv[0]);
174 		exit(1);
175 	}
176 
177 	if (strcmp(argv[1], "dump") == 0) {
178 		dump();
179 	} else {
180 		code = strtol(argv[1], &endptr, 0);
181 		if (*endptr == '\0') {
182 			match_code(code);
183 		} else {
184 			match_label(argv[1]);
185 		}
186 	}
187 
188 	exit(0);
189 }
190