-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrerrno.c
188 lines (175 loc) · 4.52 KB
/
strerrno.c
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
#include <errno.h>
#include "stdio_cx.h"
typedef struct error_list
{
errno_t value;
const char * str;
} error_list_t;
const error_list_t str_err_list[] =
{
{EPERM, "EPERM"},
{ENOENT, "ENOENT"},
{ESRCH, "ESRCH"},
{EINTR, "EINTR"},
{EIO, "EIO"},
{ENXIO, "ENXIO"},
{E2BIG, "E2BIG"},
{ENOEXEC, "ENOEXEC"},
{EBADF, "EBADF"},
{ECHILD, "ECHILD"},
{EAGAIN, "EAGAIN"},
{ENOMEM, "ENOMEM"},
{EACCES, "EACCES"},
{EFAULT, "EFAULT"},
{ENOTBLK, "ENOTBLK"},
{EBUSY, "EBUSY"},
{EEXIST, "EEXIST"},
{EXDEV, "EXDEV"},
{ENODEV, "ENODEV"},
{ENOTDIR, "ENOTDIR"},
{EISDIR, "EISDIR"},
{EINVAL, "EINVAL"},
{ENFILE, "ENFILE"},
{EMFILE, "EMFILE"},
{ENOTTY, "ENOTTY"},
{ETXTBSY, "ETXTBSY"},
{EFBIG, "EFBIG"},
{ENOSPC, "ENOSPC"},
{ESPIPE, "ESPIPE"},
{EROFS, "EROFS"},
{EMLINK, "EMLINK"},
{EPIPE, "EPIPE"},
{EDOM, "EDOM"},
{ERANGE, "ERANGE"},
{EDEADLK, "EDEADLK"},
{ENAMETOOLONG, "ENAMETOOLONG"},
{ENOLCK, "ENOLCK"},
{ENOSYS, "ENOSYS"},
{ENOTEMPTY, "ENOTEMPTY"},
{ELOOP, "ELOOP"},
{ENOMSG, "ENOMSG"},
{EBADR, "EBADR"},
{EXFULL, "EXFULL"},
{ENOANO, "ENOANO"},
{EBADRQC, "EBADRQC"},
{EBADSLT, "EBADSLT"},
{EBFONT, "EBFONT"},
{ENOSTR, "ENOSTR"},
{ENODATA, "ENODATA"},
{ETIME, "ETIME"},
{ENOLINK, "ENOLINK"},
{EADV, "EADV"},
{ESRMNT,"ESRMNT"},
{ECOMM, "ECOMM"},
{EPROTO, "EPROTO"},
{EMULTIHOP, "EMULTIHOP"},
{EDOTDOT, "EDOTDOT"},
{EBADMSG, "EBADMSG"},
{EOVERFLOW, "EOVERFLOW"},
{ENOTUNIQ, "ENOTUNIQ"},
{EBADFD, "EBADFD"},
{ENOSR, "EREMOTE"},
{ENONET, "ENONET"},
{ENOPKG, "ENOPKG"},
{EREMOTE, "EREMOTE"},
{EREMCHG, "EREMCHG"},
{ELIBACC, "ELIBACC"},
{ELIBBAD, "ELIBBAD"},
{ELIBSCN, "ELIBSCN"},
{ELIBMAX, "ELIBMAX"},
{ELIBEXEC, "ELIBEXEC"},
{EILSEQ, "EILSEQ"},
{ERESTART, "ERESTART"},
{ESTRPIPE, "ESTRPIPE"},
{EUSERS, "EUSERS"},
{ENOTSOCK, "ENOTSOCK"},
{EDESTADDRREQ, "EDESTADDRREQ"},
{EMSGSIZE, "EMSGSIZE"},
{EPROTOTYPE, "EPROTOTYPE"},
{ENOPROTOOPT, "ENOPROTOOPT"},
{EPROTONOSUPPORT, "EPROTONOSUPPORT"},
{ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT"},
{EOPNOTSUPP, "EOPNOTSUPP"},
{EPFNOSUPPORT, "EPFNOSUPPORT"},
{EAFNOSUPPORT, "EAFNOSUPPORT"},
{EADDRINUSE, "EADDRINUSE"},
{EADDRNOTAVAIL, "EADDRNOTAVAIL"},
{ENETDOWN, "ENETDOWN"},
{ENETUNREACH, "ENETUNREACH"},
{ENETRESET, "ENETRESET"},
{ECONNABORTED, "ECONNABORTED"},
{ECONNRESET, "ECONNRESET"},
{ENOBUFS, "ENOBUFS"},
{EISCONN, "EISCONN"},
{ENOTCONN, "ENOTCONN"},
{ESHUTDOWN, "ESHUTDOWN"},
{ETOOMANYREFS, "ETOOMANYREFS"},
{ETIMEDOUT, "ETIMEDOUT"},
{ECONNREFUSED, "ECONNREFUSED"},
{EHOSTDOWN, "EHOSTDOWN"},
{EHOSTUNREACH, "EHOSTUNREACH"},
{EALREADY, "EALREADY"},
{EINPROGRESS, "EINPROGRESS"},
{ESTALE, "ESTALE"},
{EUCLEAN, "EUCLEAN"},
{ENOTNAM, "ENOTNAM"},
{ENAVAIL, "ENAVAIL"},
{EISNAM, "EISNAM"},
{EREMOTEIO, "EREMOTEIO"},
{EDQUOT, "EDQUOT"},
{ENOMEDIUM, "ENOMEDIUM"},
{EMEDIUMTYPE, "EMEDIUMTYPE"},
{ECANCELED, "ECANCELED"},
{ENOKEY, "ENOKEY"},
{EKEYEXPIRED, "EKEYEXPIRED"},
{EKEYREVOKED, "EKEYREVOKED"},
{EKEYREJECTED, "EKEYREJECTED"},
{EOWNERDEAD, "EOWNERDEAD"},
{ENOTRECOVERABLE, "ENOTRECOVERABLE"},
{ERFKILL, "ERFKILL"},
{EHWPOISON, "EHWPOISON"},
{0, NULL}
};
#define E_FALSE 0
#define E_TRUE 1
const size_t strerrno_min_buf_size = 20;
errno_t strerrno_s(char * const buf, const rsize_t buflen, const errno_t errnum)
{
errno_t result = 0;
size_t list_size = (sizeof(str_err_list) / sizeof(error_list_t));
int matched = E_FALSE;
if(NULL == buf)
{
result = EFAULT;
}
else if(buflen < strerrno_min_buf_size)
{
result = ERANGE;
}
else if(buflen > 0)
{
/* Always terminate with NUL byte in case no other output */
buf[0] = '\0';
}
/* Note, the above could be simplified, but the compiler will optimise the order */
if(0 == result)
{
/* Find in the table */
for(size_t i = 0; i != list_size; ++i)
{
if(errnum == str_err_list[i].value)
{
strncpy(buf, str_err_list[i].str, buflen);
matched = E_TRUE;
break;
}
}
if(!matched)
{
snprintf(buf, buflen, "errno_t_%d_unknown\n", errnum);
buf[buflen-1] = '\0';
result = EINVAL;
}
}
return result;
}