2 * Fundamental C definitions.
4 * No copyright is claimed. This code is in the public domain; do with
26 #ifdef HAVE_SYS_SYSMACROS_H
27 # include <sys/sysmacros.h> /* for major, minor */
30 #ifndef LOGIN_NAME_MAX
31 # define LOGIN_NAME_MAX 256
35 # define NAME_MAX PATH_MAX
39 * __GNUC_PREREQ is deprecated in favour of __has_attribute() and
40 * __has_feature(). The __has macros are supported by clang and gcc>=5.
43 # if defined __GNUC__ && defined __GNUC_MINOR__
44 # define __GNUC_PREREQ(maj, min) \
45 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
47 # define __GNUC_PREREQ(maj, min) 0
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])))
57 # define ignore_result(x) __extension__ ({ \
58 __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
62 # define __must_be_array(a) 0
63 # define __attribute__(_arg_)
64 # define ignore_result(x) ((void) (x))
65 #endif /* !__GNUC__ */
68 * It evaluates to 1 if the attribute/feature is supported by the current
69 * compilation target. Fallback for old compilers.
71 #ifndef __has_attribute
72 #define __has_attribute(x) 0
76 #define __has_feature(x) 0
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))
86 # define __ul_alloc_size(s)
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))
94 # define __ul_calloc_size(n, s)
98 #if __has_attribute(returns_nonnull) || __GNUC_PREREQ (4, 9)
99 # define __ul_returns_nonnull __attribute__((returns_nonnull))
101 # define __ul_returns_nonnull
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
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); }))
114 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
118 # define PATH_MAX 4096
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; })
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; })
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; })
154 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
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.
164 #define container_of(ptr, type, member) __extension__ ({ \
165 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
166 (type *)( (char *)__mptr - offsetof(type,member) );})
169 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
170 # ifdef HAVE___PROGNAME
171 extern char *__progname
;
172 # define program_invocation_short_name __progname
174 # ifdef HAVE_GETEXECNAME
175 # define program_invocation_short_name \
176 prog_inv_sh_nm_from_file(getexecname(), 0)
178 # define program_invocation_short_name \
179 prog_inv_sh_nm_from_file(__FILE__, 1)
181 static char prog_inv_sh_nm_buf
[256];
183 prog_inv_sh_nm_from_file(char *f
, char stripext
)
187 if ((t
= strrchr(f
, '/')) != NULL
)
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';
195 if (stripext
&& (t
= strrchr(prog_inv_sh_nm_buf
, '.')) != NULL
)
198 return prog_inv_sh_nm_buf
;
206 errmsg(char doexit
, int excode
, char adderr
, const char *fmt
, ...)
208 fprintf(stderr
, "%s: ", program_invocation_short_name
);
212 vfprintf(stderr
, fmt
, argp
);
215 fprintf(stderr
, ": ");
218 fprintf(stderr
, "%m");
219 fprintf(stderr
, "\n");
225 # define err(E, FMT...) errmsg(1, E, 1, FMT)
229 # define errx(E, FMT...) errmsg(1, E, 0, FMT)
233 # define warn(FMT...) errmsg(0, 0, 1, FMT)
237 # define warnx(FMT...) errmsg(0, 0, 0, FMT)
239 #endif /* !HAVE_ERR_H */
242 /* Don't use inline function to avoid '#include "nls.h"' in c.h
244 #define errtryhelp(eval) __extension__ ({ \
245 fprintf(stderr, _("Try '%s --help' for more information.\n"), \
246 program_invocation_short_name); \
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)
257 static inline __attribute__((const)) int is_power_of_2(unsigned long num
)
259 return (num
!= 0 && ((num
& (num
- 1)) == 0));
263 typedef int64_t loff_t
;
266 #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
267 #include <sys/types.h>
269 static inline int dirfd(DIR *d
)
276 * Fallback defines for old versions of glibc
281 #define UL_CLOEXECSTR "e"
283 #define UL_CLOEXECSTR ""
290 #ifdef __FreeBSD_kernel__
291 #ifndef F_DUPFD_CLOEXEC
292 #define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
297 #ifndef AI_ADDRCONFIG
298 #define AI_ADDRCONFIG 0x0020
302 #define IUTF8 0040000
306 * MAXHOSTNAMELEN replacement
308 static inline size_t get_hostname_max(void)
310 long len
= sysconf(_SC_HOST_NAME_MAX
);
315 #ifdef MAXHOSTNAMELEN
316 return MAXHOSTNAMELEN
;
318 return HOST_NAME_MAX
;
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).
330 static inline int xusleep(useconds_t usec
)
332 #ifdef HAVE_NANOSLEEP
333 struct timespec waittime
= {
334 .tv_sec
= usec
/ 1000000L,
335 .tv_nsec
= (usec
% 1000000L) * 1000
337 return nanosleep(&waittime
, NULL
);
338 #elif defined(HAVE_USLEEP)
341 # error "System with usleep() or nanosleep() required!"
346 * Constant strings for usage() functions. For more info see
347 * Documentation/{howto-usage-function.txt,boilerplate.c}
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"
357 #define USAGE_OPTSTR_HELP _("display this help")
358 #define USAGE_OPTSTR_VERSION _("display version")
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
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
371 #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
373 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
375 #define print_version(eval) __extension__ ({ \
376 printf(UTIL_LINUX_VERSION); \
381 * scanf modifiers for "strings allocation"
383 #ifdef HAVE_SCANF_MS_MODIFIER
384 #define UL_SCNsA "%ms"
385 #elif defined(HAVE_SCANF_AS_MODIFIER)
386 #define UL_SCNsA "%as"
401 * Macros to convert #define'itions to strings, for example
403 * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
405 #define stringify_value(s) stringify(s)
406 #define stringify(s) #s
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.
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))
418 # define UL_ASAN_BLACKLIST /* nothing */
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.
426 #define UL_GETPW_BUFSIZ (16 * 1024)
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.
432 #if !defined MAP_ANONYMOUS && defined MAP_ANON
433 # define MAP_ANONYMOUS (MAP_ANON)
436 #endif /* UTIL_LINUX_C_H */