X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.h;h=531242213490161c7572c25311c2d121289710fd;hb=5ffa8c818120e35c89becd938d160235c069dd12;hp=2d03ac8fd076a6854fc962e1f366a9d341a19bc5;hpb=8facc3498ed037f842891ff55d1f60fe834f4ba0;p=elogind.git diff --git a/src/shared/util.h b/src/shared/util.h index 2d03ac8fd..531242213 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -45,12 +45,13 @@ #include #if SIZEOF_PID_T == 4 -# define PID_FMT "%" PRIi32 +# define PID_PRI PRIi32 #elif SIZEOF_PID_T == 2 -# define PID_FMT "%" PRIi16 +# define PID_PRI PRIi16 #else # error Unknown pid_t size #endif +#define PID_FMT "%" PID_PRI #if SIZEOF_UID_T == 4 # define UID_FMT "%" PRIu32 @@ -69,7 +70,7 @@ #endif #if SIZEOF_TIME_T == 8 -# define PRI_TIME PRIu64 +# define PRI_TIME PRIi64 #elif SIZEOF_TIME_T == 4 # define PRI_TIME PRIu32 #else @@ -347,12 +348,14 @@ static inline uint32_t random_u32(void) { } /* For basic lookup tables with strictly enumerated entries */ -#define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ +#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ scope const char *name##_to_string(type i) { \ if (i < 0 || i >= (type) ELEMENTSOF(name##_table)) \ return NULL; \ return name##_table[i]; \ - } \ + } + +#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ scope type name##_from_string(const char *s) { \ type i; \ if (!s) \ @@ -362,11 +365,17 @@ static inline uint32_t random_u32(void) { streq(name##_table[i], s)) \ return i; \ return (type) -1; \ - } \ + } + +#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope) \ + _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope) \ + _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope) \ struct __useless_struct_to_allow_trailing_semicolon__ -#define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,) -#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static) +#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static) +#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static) /* For string conversions where numbers are also acceptable */ #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max) \ @@ -472,6 +481,8 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus); int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0); int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5); +#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf)) + int fd_columns(int fd); unsigned columns(void); int fd_lines(int fd);