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 * __GNUC_PREREQ is deprecated in favour of __has_attribute() and
38 * __has_feature(). The __has macros are supported by clang and gcc>=5.
41 # if defined __GNUC__ && defined __GNUC_MINOR__
42 # define __GNUC_PREREQ(maj, min) \
43 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
45 # define __GNUC_PREREQ(maj, min) 0
51 /* &a[0] degrades to a pointer: a different type from an array */
52 # define __must_be_array(a) \
53 UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
55 # define ignore_result(x) __extension__ ({ \
56 __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
60 # define __must_be_array(a) 0
61 # define __attribute__(_arg_)
62 # define ignore_result(x) ((void) (x))
63 #endif /* !__GNUC__ */
66 * It evaluates to 1 if the attribute/feature is supported by the current
67 * compilation targed. Fallback for old compilers.
69 #ifndef __has_attribute
70 #define __has_attribute(x) 0
74 #define __has_feature(x) 0
80 #ifndef __ul_alloc_size
81 # if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3)
82 # define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
84 # define __ul_alloc_size(s)
88 #ifndef __ul_calloc_size
89 # if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3)
90 # define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result))
92 # define __ul_calloc_size(n, s)
96 #if __has_attribute(returns_nonnull) || __GNUC_PREREQ (4, 9)
97 # define __ul_returns_nonnull __attribute__((returns_nonnull))
99 # define __ul_returns_nonnull
103 * Force a compilation error if condition is true, but also produce a
104 * result (of value 0 and type size_t), so the expression can be used
105 * e.g. in a structure initializer (or wherever else comma expressions
108 #define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); }))
109 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
112 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
116 # define PATH_MAX 4096
128 # define min(x, y) __extension__ ({ \
129 __typeof__(x) _min1 = (x); \
130 __typeof__(y) _min2 = (y); \
131 (void) (&_min1 == &_min2); \
132 _min1 < _min2 ? _min1 : _min2; })
136 # define max(x, y) __extension__ ({ \
137 __typeof__(x) _max1 = (x); \
138 __typeof__(y) _max2 = (y); \
139 (void) (&_max1 == &_max2); \
140 _max1 > _max2 ? _max1 : _max2; })
144 # define cmp_numbers(x, y) __extension__ ({ \
145 __typeof__(x) _a = (x); \
146 __typeof__(y) _b = (y); \
147 (void) (&_a == &_b); \
148 _a == _b ? 0 : _a > _b ? 1 : -1; })
152 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
156 * container_of - cast a member of a structure out to the containing structure
157 * @ptr: the pointer to the member.
158 * @type: the type of the container struct this is embedded in.
159 * @member: the name of the member within the struct.
162 #define container_of(ptr, type, member) __extension__ ({ \
163 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
164 (type *)( (char *)__mptr - offsetof(type,member) );})
167 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
168 # ifdef HAVE___PROGNAME
169 extern char *__progname
;
170 # define program_invocation_short_name __progname
172 # ifdef HAVE_GETEXECNAME
173 # define program_invocation_short_name \
174 prog_inv_sh_nm_from_file(getexecname(), 0)
176 # define program_invocation_short_name \
177 prog_inv_sh_nm_from_file(__FILE__, 1)
179 static char prog_inv_sh_nm_buf
[256];
181 prog_inv_sh_nm_from_file(char *f
, char stripext
)
185 if ((t
= strrchr(f
, '/')) != NULL
)
190 strncpy(prog_inv_sh_nm_buf
, t
, sizeof(prog_inv_sh_nm_buf
) - 1);
191 prog_inv_sh_nm_buf
[sizeof(prog_inv_sh_nm_buf
) - 1] = '\0';
193 if (stripext
&& (t
= strrchr(prog_inv_sh_nm_buf
, '.')) != NULL
)
196 return prog_inv_sh_nm_buf
;
204 errmsg(char doexit
, int excode
, char adderr
, const char *fmt
, ...)
206 fprintf(stderr
, "%s: ", program_invocation_short_name
);
210 vfprintf(stderr
, fmt
, argp
);
213 fprintf(stderr
, ": ");
216 fprintf(stderr
, "%m");
217 fprintf(stderr
, "\n");
223 # define err(E, FMT...) errmsg(1, E, 1, FMT)
227 # define errx(E, FMT...) errmsg(1, E, 0, FMT)
231 # define warn(FMT...) errmsg(0, 0, 1, FMT)
235 # define warnx(FMT...) errmsg(0, 0, 0, FMT)
237 #endif /* !HAVE_ERR_H */
240 /* Don't use inline function to avoid '#include "nls.h"' in c.h
242 #define errtryhelp(eval) __extension__ ({ \
243 fprintf(stderr, _("Try '%s --help' for more information.\n"), \
244 program_invocation_short_name); \
248 /* After failed execvp() */
249 #define EX_EXEC_FAILED 126 /* Program located, but not usable. */
250 #define EX_EXEC_ENOENT 127 /* Could not find program to exec. */
251 #define errexec(name) err(errno == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED, \
252 _("failed to execute %s"), name)
255 static inline __attribute__((const)) int is_power_of_2(unsigned long num
)
257 return (num
!= 0 && ((num
& (num
- 1)) == 0));
261 typedef int64_t loff_t
;
264 #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
265 #include <sys/types.h>
267 static inline int dirfd(DIR *d
)
274 * Fallback defines for old versions of glibc
279 #define UL_CLOEXECSTR "e"
281 #define UL_CLOEXECSTR ""
288 #ifdef __FreeBSD_kernel__
289 #ifndef F_DUPFD_CLOEXEC
290 #define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
295 #ifndef AI_ADDRCONFIG
296 #define AI_ADDRCONFIG 0x0020
300 #define IUTF8 0040000
304 * MAXHOSTNAMELEN replacement
306 static inline size_t get_hostname_max(void)
308 long len
= sysconf(_SC_HOST_NAME_MAX
);
313 #ifdef MAXHOSTNAMELEN
314 return MAXHOSTNAMELEN
;
316 return HOST_NAME_MAX
;
322 * The usleep function was marked obsolete in POSIX.1-2001 and was removed
323 * in POSIX.1-2008. It was replaced with nanosleep() that provides more
324 * advantages (like no interaction with signals and other timer functions).
328 static inline int xusleep(useconds_t usec
)
330 #ifdef HAVE_NANOSLEEP
331 struct timespec waittime
= {
332 .tv_sec
= usec
/ 1000000L,
333 .tv_nsec
= (usec
% 1000000L) * 1000
335 return nanosleep(&waittime
, NULL
);
336 #elif defined(HAVE_USLEEP)
339 # error "System with usleep() or nanosleep() required!"
344 * Constant strings for usage() functions. For more info see
345 * Documentation/{howto-usage-function.txt,boilerplate.c}
347 #define USAGE_HEADER _("\nUsage:\n")
348 #define USAGE_OPTIONS _("\nOptions:\n")
349 #define USAGE_FUNCTIONS _("\nFunctions:\n")
350 #define USAGE_COMMANDS _("\nCommands:\n")
351 #define USAGE_COLUMNS _("\nAvailable output columns:\n")
352 #define USAGE_SEPARATOR "\n"
354 #define USAGE_OPTSTR_HELP _("display this help")
355 #define USAGE_OPTSTR_VERSION _("display version")
357 #define USAGE_HELP_OPTIONS(marg_dsc) \
358 "%-" #marg_dsc "s%s\n" \
359 "%-" #marg_dsc "s%s\n" \
360 , " -h, --help", USAGE_OPTSTR_HELP \
361 , " -V, --version", USAGE_OPTSTR_VERSION
363 #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
365 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
367 #define print_version(eval) __extension__ ({ \
368 printf(UTIL_LINUX_VERSION); \
373 * scanf modifiers for "strings allocation"
375 #ifdef HAVE_SCANF_MS_MODIFIER
376 #define UL_SCNsA "%ms"
377 #elif defined(HAVE_SCANF_AS_MODIFIER)
378 #define UL_SCNsA "%as"
393 * Macros to convert #define'itions to strings, for example
395 * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
397 #define stringify_value(s) stringify(s)
398 #define stringify(s) #s
401 * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
402 * instrumentation shipped with Clang and GCC) to not instrument the
403 * annotated function. Furthermore, it will prevent the compiler from
404 * inlining the function because inlining currently breaks the blacklisting
405 * mechanism of AddressSanitizer.
407 #if __has_feature(address_sanitizer) && __has_attribute(no_sanitize_memory) && __has_attribute(no_sanitize_address)
408 # define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address))
410 # define UL_ASAN_BLACKLIST /* nothing */
414 * Note that sysconf(_SC_GETPW_R_SIZE_MAX) returns *initial* suggested size for
415 * pwd buffer and in some cases it is not large enough. See POSIX and
416 * getpwnam_r man page for more details.
418 #define UL_GETPW_BUFSIZ (16 * 1024)
421 * Darwin or other BSDs may only have MAP_ANON. To get it on Darwin we must
422 * define _DARWIN_C_SOURCE before including sys/mman.h. We do this in config.h.
424 #if !defined MAP_ANONYMOUS && defined MAP_ANON
425 # define MAP_ANONYMOUS (MAP_ANON)
428 #endif /* UTIL_LINUX_C_H */