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 # define NAME_MAX PATH_MAX
37 * Compiler-specific stuff
40 # if defined __GNUC__ && defined __GNUC_MINOR__
41 # define __GNUC_PREREQ(maj, min) \
42 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
44 # define __GNUC_PREREQ(maj, min) 0
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])))
54 # define ignore_result(x) __extension__ ({ \
55 __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
59 # define __must_be_array(a) 0
60 # define __attribute__(_arg_)
61 # define ignore_result(x) ((void) (x))
62 #endif /* !__GNUC__ */
67 #ifndef __ul_alloc_size
68 # if __GNUC_PREREQ (4, 3)
69 # define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
71 # define __ul_alloc_size(s)
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))
79 # define __ul_calloc_size(n, s)
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
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); }))
93 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
97 # define PATH_MAX 4096
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; })
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; })
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; })
133 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
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.
143 #define container_of(ptr, type, member) __extension__ ({ \
144 void *__mptr = (void *)(ptr); \
145 ((type *)(__mptr - offsetof(type, member))); })
148 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
149 # ifdef HAVE___PROGNAME
150 extern char *__progname
;
151 # define program_invocation_short_name __progname
153 # ifdef HAVE_GETEXECNAME
154 # define program_invocation_short_name \
155 prog_inv_sh_nm_from_file(getexecname(), 0)
157 # define program_invocation_short_name \
158 prog_inv_sh_nm_from_file(__FILE__, 1)
160 static char prog_inv_sh_nm_buf
[256];
162 prog_inv_sh_nm_from_file(char *f
, char stripext
)
166 if ((t
= strrchr(f
, '/')) != NULL
)
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';
174 if (stripext
&& (t
= strrchr(prog_inv_sh_nm_buf
, '.')) != NULL
)
177 return prog_inv_sh_nm_buf
;
185 errmsg(char doexit
, int excode
, char adderr
, const char *fmt
, ...)
187 fprintf(stderr
, "%s: ", program_invocation_short_name
);
191 vfprintf(stderr
, fmt
, argp
);
194 fprintf(stderr
, ": ");
197 fprintf(stderr
, "%m");
198 fprintf(stderr
, "\n");
204 # define err(E, FMT...) errmsg(1, E, 1, FMT)
208 # define errx(E, FMT...) errmsg(1, E, 0, FMT)
212 # define warn(FMT...) errmsg(0, 0, 1, FMT)
216 # define warnx(FMT...) errmsg(0, 0, 0, FMT)
218 #endif /* !HAVE_ERR_H */
221 /* Don't use inline function to avoid '#include "nls.h"' in c.h
223 #define errtryhelp(eval) __extension__ ({ \
224 fprintf(stderr, _("Try '%s --help' for more information.\n"), \
225 program_invocation_short_name); \
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)
236 static inline __attribute__((const)) int is_power_of_2(unsigned long num
)
238 return (num
!= 0 && ((num
& (num
- 1)) == 0));
242 typedef int64_t loff_t
;
245 #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
246 #include <sys/types.h>
248 static inline int dirfd(DIR *d
)
255 * Fallback defines for old versions of glibc
260 #define UL_CLOEXECSTR "e"
262 #define UL_CLOEXECSTR ""
269 #ifdef __FreeBSD_kernel__
270 #ifndef F_DUPFD_CLOEXEC
271 #define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
276 #ifndef AI_ADDRCONFIG
277 #define AI_ADDRCONFIG 0x0020
281 #define IUTF8 0040000
285 * MAXHOSTNAMELEN replacement
287 static inline size_t get_hostname_max(void)
289 long len
= sysconf(_SC_HOST_NAME_MAX
);
294 #ifdef MAXHOSTNAMELEN
295 return MAXHOSTNAMELEN
;
297 return HOST_NAME_MAX
;
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).
309 static inline int xusleep(useconds_t usec
)
311 #ifdef HAVE_NANOSLEEP
312 struct timespec waittime
= {
313 .tv_sec
= usec
/ 1000000L,
314 .tv_nsec
= (usec
% 1000000L) * 1000
316 return nanosleep(&waittime
, NULL
);
317 #elif defined(HAVE_USLEEP)
320 # error "System with usleep() or nanosleep() required!"
325 * Constant strings for usage() functions. For more info see
326 * Documentation/{howto-usage-function.txt,boilerplate.c}
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"
335 #define USAGE_OPTSTR_HELP _("display this help")
336 #define USAGE_OPTSTR_VERSION _("display version")
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
344 #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
346 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
349 * scanf modifiers for "strings allocation"
351 #ifdef HAVE_SCANF_MS_MODIFIER
352 #define UL_SCNsA "%ms"
353 #elif defined(HAVE_SCANF_AS_MODIFIER)
354 #define UL_SCNsA "%as"
369 * Macros to convert #define'itions to strings, for example
371 * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
373 #define stringify_value(s) stringify(s)
374 #define stringify(s) #s
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.
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))
387 # define UL_ASAN_BLACKLIST /* nothing */
390 # define UL_ASAN_BLACKLIST /* nothing */
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.
400 #define UL_GETPW_BUFSIZ (16 * 1024)
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.
406 #if !defined MAP_ANONYMOUS && defined MAP_ANON
407 # define MAP_ANONYMOUS (MAP_ANON)
410 #endif /* UTIL_LINUX_C_H */