2 * Fundamental C definitions.
22 * Compiler specific stuff
25 # if defined __GNUC__ && defined __GNUC_MINOR__
26 # define __GNUC_PREREQ(maj, min) \
27 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
29 # define __GNUC_PREREQ(maj, min) 0
35 /* &a[0] degrades to a pointer: a different type from an array */
36 # define __must_be_array(a) \
37 UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
39 # define ignore_result(x) ({ \
40 __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
44 # define __must_be_array(a) 0
45 # define __attribute__(_arg_)
46 # define ignore_result(x) ((void) (x))
47 #endif /* !__GNUC__ */
52 #ifndef __ul_alloc_size
53 # if __GNUC_PREREQ (4, 3)
54 # define __ul_alloc_size(s) __attribute__((alloc_size(s)))
56 # define __ul_alloc_size(s)
60 #ifndef __ul_calloc_size
61 # if __GNUC_PREREQ (4, 3)
62 # define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s)))
64 # define __ul_calloc_size(n, s)
68 /* Force a compilation error if condition is true, but also produce a
69 * result (of value 0 and type size_t), so the expression can be used
70 * e.g. in a structure initializer (or where-ever else comma expressions
73 #define UL_BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
74 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
77 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
81 # define PATH_MAX 4096
93 # define min(x, y) ({ \
94 __typeof__(x) _min1 = (x); \
95 __typeof__(y) _min2 = (y); \
96 (void) (&_min1 == &_min2); \
97 _min1 < _min2 ? _min1 : _min2; })
101 # define max(x, y) ({ \
102 __typeof__(x) _max1 = (x); \
103 __typeof__(y) _max2 = (y); \
104 (void) (&_max1 == &_max2); \
105 _max1 > _max2 ? _max1 : _max2; })
109 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
113 #define container_of(ptr, type, member) ({ \
114 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
115 (type *)( (char *)__mptr - offsetof(type,member) );})
118 #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
119 # ifdef HAVE___PROGNAME
120 extern char *__progname
;
121 # define program_invocation_short_name __progname
123 # ifdef HAVE_GETEXECNAME
124 # define program_invocation_short_name \
125 prog_inv_sh_nm_from_file(getexecname(), 0)
127 # define program_invocation_short_name \
128 prog_inv_sh_nm_from_file(__FILE__, 1)
130 static char prog_inv_sh_nm_buf
[256];
132 prog_inv_sh_nm_from_file(char *f
, char stripext
)
136 if ((t
= strrchr(f
, '/')) != NULL
)
141 strncpy(prog_inv_sh_nm_buf
, t
, sizeof(prog_inv_sh_nm_buf
) - 1);
142 prog_inv_sh_nm_buf
[sizeof(prog_inv_sh_nm_buf
) - 1] = '\0';
144 if (stripext
&& (t
= strrchr(prog_inv_sh_nm_buf
, '.')) != NULL
)
147 return prog_inv_sh_nm_buf
;
155 errmsg(char doexit
, int excode
, char adderr
, const char *fmt
, ...)
157 fprintf(stderr
, "%s: ", program_invocation_short_name
);
161 vfprintf(stderr
, fmt
, argp
);
164 fprintf(stderr
, ": ");
167 fprintf(stderr
, "%m");
168 fprintf(stderr
, "\n");
174 # define err(E, FMT...) errmsg(1, E, 1, FMT)
178 # define errx(E, FMT...) errmsg(1, E, 0, FMT)
182 # define warn(FMT...) errmsg(0, 0, 1, FMT)
186 # define warnx(FMT...) errmsg(0, 0, 0, FMT)
188 #endif /* !HAVE_ERR_H */
191 static inline __attribute__((const)) int is_power_of_2(unsigned long num
)
193 return (num
!= 0 && ((num
& (num
- 1)) == 0));
197 typedef int64_t loff_t
;
200 #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
201 #include <sys/types.h>
203 static inline int dirfd(DIR *d
)
210 * Fallback defines for old versions of glibc
217 #ifndef AI_ADDRCONFIG
218 #define AI_ADDRCONFIG 0x0020
222 #define IUTF8 0040000
226 * Constant strings for usage() functions. For more info see
227 * Documentation/howto-usage-function.txt and sys-utils/arch.c
229 #define USAGE_HEADER _("\nUsage:\n")
230 #define USAGE_OPTIONS _("\nOptions:\n")
231 #define USAGE_SEPARATOR _("\n")
232 #define USAGE_HELP _(" -h, --help display this help and exit\n")
233 #define USAGE_VERSION _(" -V, --version output version information and exit\n")
234 #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
236 #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
239 * scanf modifiers for "strings allocation"
241 #ifdef HAVE_SCANF_MS_MODIFIER
242 #define UL_SCNsA "%ms"
243 #elif defined(HAVE_SCANF_AS_MODIFIER)
244 #define UL_SCNsA "%as"
247 #endif /* UTIL_LINUX_C_H */