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