Debian Codegraph @

gnulib-tests/test-errno-h.c

raw
1 /* Test of <errno.h> substitute.
2 Copyright (C) 2008-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2008. */
18
19 #include <config.h>
20
21 #include <errno.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 /* Check all POSIX-defined errno values, using M (v) to check value v. */
28 #define CHECK_POSIX_ERRNOS(m) \
29 m (E2BIG) \
30 m (EACCES) \
31 m (EADDRINUSE) \
32 m (EADDRNOTAVAIL) \
33 m (EAFNOSUPPORT) \
34 m (EAGAIN) \
35 m (EALREADY) \
36 m (EBADF) \
37 m (EBADMSG) \
38 m (EBUSY) \
39 m (ECANCELED) \
40 m (ECHILD) \
41 m (ECONNABORTED) \
42 m (ECONNREFUSED) \
43 m (ECONNRESET) \
44 m (EDEADLK) \
45 m (EDESTADDRREQ) \
46 m (EDOM) \
47 m (EDQUOT) \
48 m (EEXIST) \
49 m (EFAULT) \
50 m (EFBIG) \
51 m (EHOSTUNREACH) \
52 m (EIDRM) \
53 m (EILSEQ) \
54 m (EINPROGRESS) \
55 m (EINTR) \
56 m (EINVAL) \
57 m (EIO) \
58 m (EISCONN) \
59 m (EISDIR) \
60 m (ELOOP) \
61 m (EMFILE) \
62 m (EMLINK) \
63 m (EMSGSIZE) \
64 m (EMULTIHOP) \
65 m (ENAMETOOLONG) \
66 m (ENETDOWN) \
67 m (ENETRESET) \
68 m (ENETUNREACH) \
69 m (ENFILE) \
70 m (ENOBUFS) \
71 m (ENODEV) \
72 m (ENOENT) \
73 m (ENOEXEC) \
74 m (ENOLCK) \
75 m (ENOLINK) \
76 m (ENOMEM) \
77 m (ENOMSG) \
78 m (ENOPROTOOPT) \
79 m (ENOSPC) \
80 m (ENOSYS) \
81 m (ENOTCONN) \
82 m (ENOTDIR) \
83 m (ENOTEMPTY) \
84 m (ENOTRECOVERABLE) \
85 m (ENOTSOCK) \
86 m (ENOTSUP) \
87 m (ENOTTY) \
88 m (ENXIO) \
89 m (EOPNOTSUPP) \
90 m (EOVERFLOW) \
91 m (EOWNERDEAD) \
92 m (EPERM) \
93 m (EPIPE) \
94 m (EPROTO) \
95 m (EPROTONOSUPPORT) \
96 m (EPROTOTYPE) \
97 m (ERANGE) \
98 m (EROFS) \
99 m (ESOCKTNOSUPPORT) \
100 m (ESPIPE) \
101 m (ESRCH) \
102 m (ESTALE) \
103 m (ETIMEDOUT) \
104 m (ETXTBSY) \
105 m (EWOULDBLOCK) \
106 m (EXDEV) \
107 /* end of CHECK_POSIX_ERRNOS */
108
109 /* Verify that the POSIX mandated errno values can be used as integer
110 constant expressions and are all positive (except on Haiku). */
111 #if defined __HAIKU__
112 # define NONZERO_INTEGER_CONSTANT_EXPRESSION(e) static_assert (0 != (e) << 0);
113 CHECK_POSIX_ERRNOS (NONZERO_INTEGER_CONSTANT_EXPRESSION)
114 #else
115 # define POSITIVE_INTEGER_CONSTANT_EXPRESSION(e) static_assert (0 < (e) << 0);
116 CHECK_POSIX_ERRNOS (POSITIVE_INTEGER_CONSTANT_EXPRESSION)
117 #endif
118
119 /* Verify that errno values can all be used in #if. */
120 #define USABLE_IN_IF(e) ^ e
121 #if 0 CHECK_POSIX_ERRNOS (USABLE_IN_IF)
122 #endif
123
124 /* Check that errno values all differ, except possibly for
125 EWOULDBLOCK == EAGAIN and ENOTSUP == EOPNOTSUPP. */
126 #define ERRTAB(e) { #e, e },
127 static struct nameval { char const *name; int value; }
128 errtab[] = { CHECK_POSIX_ERRNOS (ERRTAB) };
129
130 static int
131 errtab_cmp (void const *va, void const *vb)
132 {
133 struct nameval const *a = va, *b = vb;
134
135 /* Sort by value first, then by name (to simplify later tests).
136 Subtraction cannot overflow as both are positive. */
137 int diff = a->value - b->value;
138 return diff ? diff : strcmp (a->name, b->name);
139 }
140
141 int
142 main ()
143 {
144 int test_exit_status = EXIT_SUCCESS;
145
146 /* Verify that errno can be assigned. */
147 errno = EOVERFLOW;
148
149 /* Check that errno values all differ, except possibly for
150 EAGAIN == EWOULDBLOCK and ENOTSUP == EOPNOTSUPP. */
151 int nerrtab = sizeof errtab / sizeof *errtab;
152 qsort (errtab, nerrtab, sizeof *errtab, errtab_cmp);
153 for (int i = 1; i < nerrtab; i++)
154 if (errtab[i - 1].value == errtab[i].value)
155 {
156 fprintf (stderr, "%s == %s == %d\n",
157 errtab[i - 1].name, errtab[i].name, errtab[i].value);
158 if (! ((strcmp ("EAGAIN", errtab[i - 1].name) == 0
159 && strcmp ("EWOULDBLOCK", errtab[i].name) == 0)
160 || (strcmp ("ENOTSUP", errtab[i - 1].name) == 0
161 && strcmp ("EOPNOTSUPP", errtab[i].name) == 0)))
162 test_exit_status = EXIT_FAILURE;
163 }
164
165 return test_exit_status;
166 }