2009-10-15 17:42:22 -05:00
|
|
|
/*
|
|
|
|
* Fundamental C definitions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef UTIL_LINUX_C_H
|
|
|
|
#define UTIL_LINUX_C_H
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compiler specific stuff
|
|
|
|
*/
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
|
|
|
/* &a[0] degrades to a pointer: a different type from an array */
|
|
|
|
# define __must_be_array(a) \
|
|
|
|
BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))
|
|
|
|
|
2011-02-07 08:52:21 -06:00
|
|
|
# define ignore_result(x) ({ typeof(x) __dummy = (x); (void) __dummy; })
|
|
|
|
|
2009-10-15 17:42:22 -05:00
|
|
|
#else /* !__GNUC__ */
|
|
|
|
# define __must_be_array(a) 0
|
|
|
|
# define __attribute__(_arg_)
|
2011-02-07 08:52:21 -06:00
|
|
|
# define ignore_result(x) ((void) (x))
|
2009-10-15 17:42:22 -05:00
|
|
|
#endif /* !__GNUC__ */
|
|
|
|
|
|
|
|
|
|
|
|
/* Force a compilation error if condition is true, but also produce a
|
|
|
|
* result (of value 0 and type size_t), so the expression can be used
|
|
|
|
* e.g. in a structure initializer (or where-ever else comma expressions
|
|
|
|
* aren't permitted).
|
|
|
|
*/
|
|
|
|
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
|
|
|
|
#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
|
|
|
|
|
|
|
|
#ifndef ARRAY_SIZE
|
|
|
|
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
# define PATH_MAX 4096
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef TRUE
|
|
|
|
# define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FALSE
|
|
|
|
# define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
2010-03-16 09:24:04 -05:00
|
|
|
#ifndef min
|
|
|
|
# define min(x, y) ({ \
|
2010-02-12 14:42:46 -06:00
|
|
|
typeof(x) _min1 = (x); \
|
|
|
|
typeof(y) _min2 = (y); \
|
|
|
|
(void) (&_min1 == &_min2); \
|
|
|
|
_min1 < _min2 ? _min1 : _min2; })
|
2010-03-16 09:24:04 -05:00
|
|
|
#endif
|
2010-02-12 14:42:46 -06:00
|
|
|
|
2010-03-16 09:24:04 -05:00
|
|
|
#ifndef max
|
|
|
|
# define max(x, y) ({ \
|
2010-02-12 14:42:46 -06:00
|
|
|
typeof(x) _max1 = (x); \
|
|
|
|
typeof(y) _max2 = (y); \
|
|
|
|
(void) (&_max1 == &_max2); \
|
|
|
|
_max1 > _max2 ? _max1 : _max2; })
|
2010-03-16 09:24:04 -05:00
|
|
|
#endif
|
2010-02-12 14:42:46 -06:00
|
|
|
|
2011-01-04 14:34:45 -06:00
|
|
|
|
2010-10-14 18:33:28 -05:00
|
|
|
static inline __attribute__((const)) int is_power_of_2(unsigned long num)
|
|
|
|
{
|
|
|
|
return (num != 0 && ((num & (num - 1)) == 0));
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:04:04 -06:00
|
|
|
#ifndef HAVE_LOFF_T
|
|
|
|
typedef int64_t loff_t;
|
|
|
|
#endif
|
|
|
|
|
2011-01-21 10:48:14 -06:00
|
|
|
#if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
static inline int dirfd(DIR *d)
|
|
|
|
{
|
|
|
|
return d->dd_fd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-25 14:40:46 -06:00
|
|
|
#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
|
|
|
|
# ifdef HAVE___PROGNAME
|
|
|
|
extern char *__progname;
|
|
|
|
# define program_invocation_short_name __progname
|
|
|
|
# else
|
|
|
|
# include <string.h>
|
|
|
|
# ifdef HAVE_GETEXECNAME
|
|
|
|
# include <stdlib.h>
|
|
|
|
# define program_invocation_short_name \
|
|
|
|
prog_inv_sh_nm_from_file(getexecname(), 0)
|
|
|
|
# else
|
|
|
|
# define program_invocation_short_name \
|
|
|
|
prog_inv_sh_nm_from_file(__FILE__, 1)
|
|
|
|
# endif
|
|
|
|
static char prog_inv_sh_nm_buf[256];
|
|
|
|
static inline char *
|
|
|
|
prog_inv_sh_nm_from_file(char *f, char stripext)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
if ((t = strrchr(f, '/')) != NULL)
|
|
|
|
t++;
|
|
|
|
else
|
|
|
|
t = f;
|
|
|
|
|
|
|
|
strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
|
|
|
|
prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
|
|
|
|
|
|
|
|
if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
|
|
|
|
*t = '\0';
|
|
|
|
|
|
|
|
return prog_inv_sh_nm_buf;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2010-10-14 18:33:28 -05:00
|
|
|
|
2009-10-15 17:42:22 -05:00
|
|
|
#endif /* UTIL_LINUX_C_H */
|