summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/src/errcode.c
blob: 7343e818be2f7333f22a6f41f73bbb042c0d5e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
   Portability layer for error codes

   Copyright (C) Amitay Isaacs  2018

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

/*
 * These errors are as listed in POSIX standard
 * IEEE Std 1003.1-2017 (Revision of IEEE Std 1003.1-2008)
 *
 * Error codes marked obsolete are removed (ENODATA, ENOSR, ENOSTR, ETIME)
 */

#include "replace.h"

struct {
	const char *label;
	int code;
} err_codes[] = {
	{ "E2BIG", E2BIG },

	{ "EACCES", EACCES },
	{ "EADDRINUSE", EADDRINUSE },
	{ "EADDRNOTAVAIL", EADDRNOTAVAIL },
	{ "EAFNOSUPPORT", EAFNOSUPPORT },
	{ "EAGAIN", EAGAIN },
	{ "EALREADY", EALREADY },

	{ "EBADF", EBADF },
	{ "EBADMSG", EBADMSG },
	{ "EBUSY", EBUSY },

	{ "ECANCELED", ECANCELED },
	{ "ECHILD", ECHILD },
	{ "ECONNABORTED", ECONNABORTED },
	{ "ECONNREFUSED", ECONNREFUSED },
	{ "ECONNRESET", ECONNRESET },

	{ "EDEADLK", EDEADLK },
	{ "EDESTADDRREQ", EDESTADDRREQ },
	{ "EDOM", EDOM },
	{ "EDQUOT", EDQUOT },

	{ "EEXIST", EEXIST },

	{ "EFAULT", EFAULT },
	{ "EFBIG", EFBIG },

	{ "EHOSTUNREACH", EHOSTUNREACH },

	{ "EIDRM", EIDRM },
	{ "EILSEQ", EILSEQ },
	{ "EINPROGRESS", EINPROGRESS },
	{ "EINTR", EINTR },
	{ "EINVAL", EINVAL },
	{ "EIO", EIO },
	{ "EISCONN", EISCONN },
	{ "EISDIR", EISDIR },

	{ "ELOOP", ELOOP },

	{ "EMFILE", EMFILE },
	{ "EMLINK", EMLINK },
	{ "EMSGSIZE", EMSGSIZE },
	{ "EMULTIHOP", EMULTIHOP },

	{ "ENAMETOOLONG", ENAMETOOLONG },
	{ "ENETDOWN", ENETDOWN },
	{ "ENETRESET", ENETRESET },
	{ "ENETUNREACH", ENETUNREACH },
	{ "ENFILE", ENFILE },
	{ "ENOBUFS", ENOBUFS },
	{ "ENODEV", ENODEV },
	{ "ENOENT", ENOENT },
	{ "ENOEXEC", ENOEXEC },
	{ "ENOLCK", ENOLCK },
	{ "ENOLINK", ENOLINK },
	{ "ENOMEM", ENOMEM },
	{ "ENOMSG", ENOMSG },
	{ "ENOPROTOOPT", ENOPROTOOPT },
	{ "ENOSPC", ENOSPC },
	{ "ENOSYS", ENOSYS },
	{ "ENOTCONN", ENOTCONN },
	{ "ENOTDIR", ENOTDIR },
	{ "ENOTEMPTY", ENOTEMPTY },
	{ "ENOTSOCK", ENOTSOCK },
	{ "ENOTSUP", ENOTSUP },
	{ "ENOTTY", ENOTTY },
	{ "ENXIO", ENXIO },

	{ "EOPNOTSUPP", EOPNOTSUPP },
	{ "EOVERFLOW", EOVERFLOW },

	{ "EPERM", EPERM },
	{ "EPIPE", EPIPE },
	{ "EPROTO", EPROTO },
	{ "EPROTONOSUPPORT", EPROTONOSUPPORT },
	{ "EPROTOTYPE", EPROTOTYPE },

	{ "ERANGE", ERANGE },
	{ "EROFS", EROFS },

	{ "ESPIPE", ESPIPE },
	{ "ESRCH", ESRCH },
	{ "ESTALE", ESTALE },

	{ "ETIMEDOUT", ETIMEDOUT },
	{ "ETXTBSY", ETXTBSY },

	{ "EWOULDBLOCK", EWOULDBLOCK },

	{ "EXDEV", EXDEV },
};

static void dump(void)
{
	size_t i;

	for (i=0; i<ARRAY_SIZE(err_codes); i++) {
		printf("%s %d\n", err_codes[i].label, err_codes[i].code);
	}
}

static void match_label(const char *str)
{
	int code = -1;
	size_t i;

	for (i=0; i<ARRAY_SIZE(err_codes); i++) {
		if (strcasecmp(err_codes[i].label, str) == 0) {
			code = err_codes[i].code;
			break;
		}
	}

	printf("%d\n", code);
}

static void match_code(int code)
{
	const char *label = "UNKNOWN";
	size_t i;

	for (i=0; i<ARRAY_SIZE(err_codes); i++) {
		if (err_codes[i].code == code) {
			label = err_codes[i].label;
			break;
		}
	}

	printf("%s\n", label);
}

int main(int argc, const char **argv)
{
	long int code;
	char *endptr;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s dump|<errcode>\n", argv[0]);
		exit(1);
	}

	if (strcmp(argv[1], "dump") == 0) {
		dump();
	} else {
		code = strtol(argv[1], &endptr, 0);
		if (*endptr == '\0') {
			match_code(code);
		} else {
			match_label(argv[1]);
		}
	}

	exit(0);
}