2 * Fundamental C definitions.
24 #ifdef HAVE_SYS_SYSMACROS_H
25 # include <sys/sysmacros.h> /* for major, minor */
28 #ifndef LOGIN_NAME_MAX
29 # define LOGIN_NAME_MAX 256
33 * Compiler-specific stuff
36 # if defined __GNUC__ && defined __GNUC_MINOR__
37 # define __GNUC_PREREQ(maj, min) \
38 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
40 # define __GNUC_PREREQ(maj, min) 0
46 /* &a[0] degrades to a pointer: a different type from an array */
47 # define __must_be_array(a) \
48 UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
50 # define ignore_result(x) __extension__ ({ \
51 __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
55 # define __must_be_array(a) 0
56 # define __attribute__(_arg_)
57 # define ignore_result(x) ((void) (x))
58 #endif /* !__GNUC__ */
63 #ifndef __ul_alloc_size
64 # if __GNUC_PREREQ (4, 3)
65 # define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
67 # define __ul_alloc_size(s)
71 #ifndef __ul_calloc_size
72 # if __GNUC_PREREQ (4, 3)
73 # define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result))
75 # define __ul_calloc_size(n, s)
80 * Force a compilation error if condition is true, but also produce a
81 * result (of value 0 and type size_t), so the expression can be used
82 * e.g. in a structure initializer (or wherever else comma expressions
85 #define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); }))
86 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
89 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
93 # define PATH_MAX 4096
105 # define min(x, y) __extension__ ({ \
106 __typeof__(x) _min1 = (x); \
107 __typeof__(y) _min2 = (y); \
108 (void) (&_min1 == &_min2); \
109 _min1 < _min2 ? _min1 : _min2; })
113 # define max(x, y) __extension__ ({ \
114 __typeof__(x) _max1 = (x); \
115 __typeof__(y) _max2 = (y); \
116 (void) (&_max1 == &_max2); \
117 _max1 > _max2 ? _max1 : _max2; })
121 # define cmp_numbers(x, y) __extension__ ({ \
122 __typeof__(x) _a = (x); \
123 __typeof__(y) _b = (y); \
124 (void) (&_a == &_b); \
125 _a == _b ? 0 : _a > _b ? 1 : -1; })
129 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
133 #define container_of(ptr, type, member) __extension__ ({ \
134 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
135 (type *)( (char *)__mptr - offsetof(type,member) );})
138 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
139 # ifdef HAVE___PROGNAME
140 extern char *__progname
;
141 # define program_invocation_short_name __progname
143 # ifdef HAVE_GETEXECNAME
144 # define program_invocation_short_name \
145 prog_inv_sh_nm_from_file(getexecname(), 0)
147 # define program_invocation_short_name \
148 prog_inv_sh_nm_from_file(__FILE__, 1)
150 static char prog_inv_sh_nm_buf
[256];
152 prog_inv_sh_nm_from_file(char *f
, char stripext
)
156 if ((t
= strrchr(f
, '/')) != NULL
)
161 strncpy(prog_inv_sh_nm_buf
, t
, sizeof(prog_inv_sh_nm_buf
) - 1);
162 prog_inv_sh_nm_buf
[sizeof(prog_inv_sh_nm_buf
) - 1] = '\0';
164 if (stripext
&& (t
= strrchr(prog_inv_sh_nm_buf
, '.')) != NULL
)
167 return prog_inv_sh_nm_buf
;
175 errmsg(char doexit
, int excode
, char adderr
, const char *fmt
, ...)
177 fprintf(stderr
, "%s: ", program_invocation_short_name
);
181 vfprintf(stderr
, fmt
, argp
);
184 fprintf(stderr
, ": ");
187 fprintf(stderr
, "%m");
188 fprintf(stderr
, "\n");
194 # define err(E, FMT...) errmsg(1, E, 1, FMT)
198 # define errx(E, FMT...) errmsg(1, E, 0, FMT)
202 # define warn(FMT...) errmsg(0, 0, 1, FMT)
206 # define warnx(FMT...) errmsg(0, 0, 0, FMT)
208 #endif /* !HAVE_ERR_H */
211 /* Don't use inline function to avoid '#include "nls.h"' in c.h
213 #define errtryhelp(eval) __extension__ ({ \
214 fprintf(stderr, _("Try '%s --help' for more information.\n"), \
215 program_invocation_short_name); \
219 /* After failed execvp() */
220 #define EX_EXEC_FAILED 126 /* Program located, but not usable. */
221 #define EX_EXEC_ENOENT 127 /* Could not find program to exec. */
222 #define errexec(name) err(errno == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED, \
223 _("failed to execute %s"), name)
226 static inline __attribute__((const)) int is_power_of_2(unsigned long num
)
228 return (num
!= 0 && ((num
& (num
- 1)) == 0));
232 typedef int64_t loff_t
;
235 #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
236 #include <sys/types.h>
238 static inline int dirfd(DIR *d
)
245 * Fallback defines for old versions of glibc
250 #define UL_CLOEXECSTR "e"
252 #define UL_CLOEXECSTR ""
259 #ifdef __FreeBSD_kernel__
260 #ifndef F_DUPFD_CLOEXEC
261 #define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
266 #ifndef AI_ADDRCONFIG
267 #define AI_ADDRCONFIG 0x0020
271 #define IUTF8 0040000
275 * MAXHOSTNAMELEN replacement
277 static inline size_t get_hostname_max(void)
279 long len
= sysconf(_SC_HOST_NAME_MAX
);
284 #ifdef MAXHOSTNAMELEN
285 return MAXHOSTNAMELEN
;
287 return HOST_NAME_MAX
;
293 * The usleep function was marked obsolete in POSIX.1-2001 and was removed
294 * in POSIX.1-2008. It was replaced with nanosleep() that provides more
295 * advantages (like no interaction with signals and other timer functions).
299 static inline int xusleep(useconds_t usec
)
301 #ifdef HAVE_NANOSLEEP
302 struct timespec waittime
= {
303 .tv_sec
= usec
/ 1000000L,
304 .tv_nsec
= (usec
% 1000000L) * 1000
306 return nanosleep(&waittime
, NULL
);
307 #elif defined(HAVE_USLEEP)
310 # error "System with usleep() or nanosleep() required!"
315 * Constant strings for usage() functions. For more info see
316 * Documentation/{howto-usage-function.txt,boilerplate.c}
318 #define USAGE_HEADER _("\nUsage:\n")
319 #define USAGE_OPTIONS _("\nOptions:\n")
320 #define USAGE_FUNCTIONS _("\nFunctions:\n")
321 #define USAGE_COMMANDS _("\nCommands:\n")
322 #define USAGE_COLUMNS _("\nAvailable output columns:\n")
323 #define USAGE_SEPARATOR "\n"
325 #define USAGE_OPTSTR_HELP _("display this help")
326 #define USAGE_OPTSTR_VERSION _("display version")
328 #define USAGE_HELP_OPTIONS(marg_dsc) \
329 "%-" #marg_dsc "s%s\n" \
330 "%-" #marg_dsc "s%s\n" \
331 , " -h, --help", USAGE_OPTSTR_HELP \
332 , " -V, --version", USAGE_OPTSTR_VERSION
334 #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
336 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
339 * scanf modifiers for "strings allocation"
341 #ifdef HAVE_SCANF_MS_MODIFIER
342 #define UL_SCNsA "%ms"
343 #elif defined(HAVE_SCANF_AS_MODIFIER)
344 #define UL_SCNsA "%as"
359 * Macros to convert #define'itions to strings, for example
361 * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
363 #define stringify_value(s) stringify(s)
364 #define stringify(s) #s
367 * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
368 * instrumentation shipped with Clang and GCC) to not instrument the
369 * annotated function. Furthermore, it will prevent the compiler from
370 * inlining the function because inlining currently breaks the blacklisting
371 * mechanism of AddressSanitizer.
373 #if defined(__has_feature)
374 # if __has_feature(address_sanitizer)
375 # define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address))
377 # define UL_ASAN_BLACKLIST /* nothing */
380 # define UL_ASAN_BLACKLIST /* nothing */
386 * Note that sysconf(_SC_GETPW_R_SIZE_MAX) returns *initial* suggested size for
387 * pwd buffer and in some cases it is not large enough. See POSIX and
388 * getpwnam_r man page for more details.
390 #define UL_GETPW_BUFSIZ (16 * 1024)
393 * Darwin or other BSDs may only have MAP_ANON. To get it on Darwin we must
394 * define _DARWIN_C_SOURCE before including sys/mman.h. We do this in config.h.
396 #if !defined MAP_ANONYMOUS && defined MAP_ANON
397 # define MAP_ANONYMOUS (MAP_ANON)
400 #endif /* UTIL_LINUX_C_H */