]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/c.h
include/ttyutils: define values if missing.
[thirdparty/util-linux.git] / include / c.h
1 /*
2 * Fundamental C definitions.
3 */
4
5 #ifndef UTIL_LINUX_C_H
6 #define UTIL_LINUX_C_H
7
8 #include <limits.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <stdarg.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #include <assert.h>
19
20 #ifdef HAVE_ERR_H
21 # include <err.h>
22 #endif
23
24 #ifdef HAVE_SYS_SYSMACROS_H
25 # include <sys/sysmacros.h> /* for major, minor */
26 #endif
27
28 #ifndef LOGIN_NAME_MAX
29 # define LOGIN_NAME_MAX 256
30 #endif
31
32 #ifndef NAME_MAX
33 # define NAME_MAX PATH_MAX
34 #endif
35
36 /*
37 * Compiler-specific stuff
38 */
39 #ifndef __GNUC_PREREQ
40 # if defined __GNUC__ && defined __GNUC_MINOR__
41 # define __GNUC_PREREQ(maj, min) \
42 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
43 # else
44 # define __GNUC_PREREQ(maj, min) 0
45 # endif
46 #endif
47
48 #ifdef __GNUC__
49
50 /* &a[0] degrades to a pointer: a different type from an array */
51 # define __must_be_array(a) \
52 UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
53
54 # define ignore_result(x) __extension__ ({ \
55 __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
56 })
57
58 #else /* !__GNUC__ */
59 # define __must_be_array(a) 0
60 # define __attribute__(_arg_)
61 # define ignore_result(x) ((void) (x))
62 #endif /* !__GNUC__ */
63
64 /*
65 * Function attributes
66 */
67 #ifndef __ul_alloc_size
68 # if __GNUC_PREREQ (4, 3)
69 # define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
70 # else
71 # define __ul_alloc_size(s)
72 # endif
73 #endif
74
75 #ifndef __ul_calloc_size
76 # if __GNUC_PREREQ (4, 3)
77 # define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result))
78 # else
79 # define __ul_calloc_size(n, s)
80 # endif
81 #endif
82
83 /*
84 * Force a compilation error if condition is true, but also produce a
85 * result (of value 0 and type size_t), so the expression can be used
86 * e.g. in a structure initializer (or wherever else comma expressions
87 * aren't permitted).
88 */
89 #define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); }))
90 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
91
92 #ifndef ARRAY_SIZE
93 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
94 #endif
95
96 #ifndef PATH_MAX
97 # define PATH_MAX 4096
98 #endif
99
100 #ifndef TRUE
101 # define TRUE 1
102 #endif
103
104 #ifndef FALSE
105 # define FALSE 0
106 #endif
107
108 #ifndef min
109 # define min(x, y) __extension__ ({ \
110 __typeof__(x) _min1 = (x); \
111 __typeof__(y) _min2 = (y); \
112 (void) (&_min1 == &_min2); \
113 _min1 < _min2 ? _min1 : _min2; })
114 #endif
115
116 #ifndef max
117 # define max(x, y) __extension__ ({ \
118 __typeof__(x) _max1 = (x); \
119 __typeof__(y) _max2 = (y); \
120 (void) (&_max1 == &_max2); \
121 _max1 > _max2 ? _max1 : _max2; })
122 #endif
123
124 #ifndef cmp_numbers
125 # define cmp_numbers(x, y) __extension__ ({ \
126 __typeof__(x) _a = (x); \
127 __typeof__(y) _b = (y); \
128 (void) (&_a == &_b); \
129 _a == _b ? 0 : _a > _b ? 1 : -1; })
130 #endif
131
132 #ifndef offsetof
133 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
134 #endif
135
136 /*
137 * container_of - cast a member of a structure out to the containing structure
138 * @ptr: the pointer to the member.
139 * @type: the type of the container struct this is embedded in.
140 * @member: the name of the member within the struct.
141 */
142 #ifndef container_of
143 #define container_of(ptr, type, member) __extension__ ({ \
144 void *__mptr = (void *)(ptr); \
145 ((type *)(__mptr - offsetof(type, member))); })
146 #endif
147
148 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
149 # ifdef HAVE___PROGNAME
150 extern char *__progname;
151 # define program_invocation_short_name __progname
152 # else
153 # ifdef HAVE_GETEXECNAME
154 # define program_invocation_short_name \
155 prog_inv_sh_nm_from_file(getexecname(), 0)
156 # else
157 # define program_invocation_short_name \
158 prog_inv_sh_nm_from_file(__FILE__, 1)
159 # endif
160 static char prog_inv_sh_nm_buf[256];
161 static inline char *
162 prog_inv_sh_nm_from_file(char *f, char stripext)
163 {
164 char *t;
165
166 if ((t = strrchr(f, '/')) != NULL)
167 t++;
168 else
169 t = f;
170
171 strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
172 prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
173
174 if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
175 *t = '\0';
176
177 return prog_inv_sh_nm_buf;
178 }
179 # endif
180 #endif
181
182
183 #ifndef HAVE_ERR_H
184 static inline void
185 errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
186 {
187 fprintf(stderr, "%s: ", program_invocation_short_name);
188 if (fmt != NULL) {
189 va_list argp;
190 va_start(argp, fmt);
191 vfprintf(stderr, fmt, argp);
192 va_end(argp);
193 if (adderr)
194 fprintf(stderr, ": ");
195 }
196 if (adderr)
197 fprintf(stderr, "%m");
198 fprintf(stderr, "\n");
199 if (doexit)
200 exit(excode);
201 }
202
203 #ifndef HAVE_ERR
204 # define err(E, FMT...) errmsg(1, E, 1, FMT)
205 #endif
206
207 #ifndef HAVE_ERRX
208 # define errx(E, FMT...) errmsg(1, E, 0, FMT)
209 #endif
210
211 #ifndef HAVE_WARN
212 # define warn(FMT...) errmsg(0, 0, 1, FMT)
213 #endif
214
215 #ifndef HAVE_WARNX
216 # define warnx(FMT...) errmsg(0, 0, 0, FMT)
217 #endif
218 #endif /* !HAVE_ERR_H */
219
220
221 /* Don't use inline function to avoid '#include "nls.h"' in c.h
222 */
223 #define errtryhelp(eval) __extension__ ({ \
224 fprintf(stderr, _("Try '%s --help' for more information.\n"), \
225 program_invocation_short_name); \
226 exit(eval); \
227 })
228
229 /* After failed execvp() */
230 #define EX_EXEC_FAILED 126 /* Program located, but not usable. */
231 #define EX_EXEC_ENOENT 127 /* Could not find program to exec. */
232 #define errexec(name) err(errno == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED, \
233 _("failed to execute %s"), name)
234
235
236 static inline __attribute__((const)) int is_power_of_2(unsigned long num)
237 {
238 return (num != 0 && ((num & (num - 1)) == 0));
239 }
240
241 #ifndef HAVE_LOFF_T
242 typedef int64_t loff_t;
243 #endif
244
245 #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
246 #include <sys/types.h>
247 #include <dirent.h>
248 static inline int dirfd(DIR *d)
249 {
250 return d->dd_fd;
251 }
252 #endif
253
254 /*
255 * Fallback defines for old versions of glibc
256 */
257 #include <fcntl.h>
258
259 #ifdef O_CLOEXEC
260 #define UL_CLOEXECSTR "e"
261 #else
262 #define UL_CLOEXECSTR ""
263 #endif
264
265 #ifndef O_CLOEXEC
266 #define O_CLOEXEC 0
267 #endif
268
269 #ifdef __FreeBSD_kernel__
270 #ifndef F_DUPFD_CLOEXEC
271 #define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
272 #endif
273 #endif
274
275
276 #ifndef AI_ADDRCONFIG
277 #define AI_ADDRCONFIG 0x0020
278 #endif
279
280 #ifndef IUTF8
281 #define IUTF8 0040000
282 #endif
283
284 /*
285 * MAXHOSTNAMELEN replacement
286 */
287 static inline size_t get_hostname_max(void)
288 {
289 long len = sysconf(_SC_HOST_NAME_MAX);
290
291 if (0 < len)
292 return len;
293
294 #ifdef MAXHOSTNAMELEN
295 return MAXHOSTNAMELEN;
296 #elif HOST_NAME_MAX
297 return HOST_NAME_MAX;
298 #endif
299 return 64;
300 }
301
302 /*
303 * The usleep function was marked obsolete in POSIX.1-2001 and was removed
304 * in POSIX.1-2008. It was replaced with nanosleep() that provides more
305 * advantages (like no interaction with signals and other timer functions).
306 */
307 #include <time.h>
308
309 static inline int xusleep(useconds_t usec)
310 {
311 #ifdef HAVE_NANOSLEEP
312 struct timespec waittime = {
313 .tv_sec = usec / 1000000L,
314 .tv_nsec = (usec % 1000000L) * 1000
315 };
316 return nanosleep(&waittime, NULL);
317 #elif defined(HAVE_USLEEP)
318 return usleep(usec);
319 #else
320 # error "System with usleep() or nanosleep() required!"
321 #endif
322 }
323
324 /*
325 * Constant strings for usage() functions. For more info see
326 * Documentation/{howto-usage-function.txt,boilerplate.c}
327 */
328 #define USAGE_HEADER _("\nUsage:\n")
329 #define USAGE_OPTIONS _("\nOptions:\n")
330 #define USAGE_FUNCTIONS _("\nFunctions:\n")
331 #define USAGE_COMMANDS _("\nCommands:\n")
332 #define USAGE_COLUMNS _("\nAvailable output columns:\n")
333 #define USAGE_SEPARATOR "\n"
334
335 #define USAGE_OPTSTR_HELP _("display this help")
336 #define USAGE_OPTSTR_VERSION _("display version")
337
338 #define USAGE_HELP_OPTIONS(marg_dsc) \
339 "%-" #marg_dsc "s%s\n" \
340 "%-" #marg_dsc "s%s\n" \
341 , " -h, --help", USAGE_OPTSTR_HELP \
342 , " -V, --version", USAGE_OPTSTR_VERSION
343
344 #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
345
346 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
347
348 /*
349 * scanf modifiers for "strings allocation"
350 */
351 #ifdef HAVE_SCANF_MS_MODIFIER
352 #define UL_SCNsA "%ms"
353 #elif defined(HAVE_SCANF_AS_MODIFIER)
354 #define UL_SCNsA "%as"
355 #endif
356
357 /*
358 * seek stuff
359 */
360 #ifndef SEEK_DATA
361 # define SEEK_DATA 3
362 #endif
363 #ifndef SEEK_HOLE
364 # define SEEK_HOLE 4
365 #endif
366
367
368 /*
369 * Macros to convert #define'itions to strings, for example
370 * #define XYXXY 42
371 * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
372 */
373 #define stringify_value(s) stringify(s)
374 #define stringify(s) #s
375
376 /*
377 * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
378 * instrumentation shipped with Clang and GCC) to not instrument the
379 * annotated function. Furthermore, it will prevent the compiler from
380 * inlining the function because inlining currently breaks the blacklisting
381 * mechanism of AddressSanitizer.
382 */
383 #if defined(__has_feature)
384 # if __has_feature(address_sanitizer)
385 # define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address))
386 # else
387 # define UL_ASAN_BLACKLIST /* nothing */
388 # endif
389 #else
390 # define UL_ASAN_BLACKLIST /* nothing */
391 #endif
392
393
394
395 /*
396 * Note that sysconf(_SC_GETPW_R_SIZE_MAX) returns *initial* suggested size for
397 * pwd buffer and in some cases it is not large enough. See POSIX and
398 * getpwnam_r man page for more details.
399 */
400 #define UL_GETPW_BUFSIZ (16 * 1024)
401
402 /*
403 * Darwin or other BSDs may only have MAP_ANON. To get it on Darwin we must
404 * define _DARWIN_C_SOURCE before including sys/mman.h. We do this in config.h.
405 */
406 #if !defined MAP_ANONYMOUS && defined MAP_ANON
407 # define MAP_ANONYMOUS (MAP_ANON)
408 #endif
409
410 #endif /* UTIL_LINUX_C_H */