2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
36 /* Required on Windows where unistd.h doesn't exist */
37 # define R_OK 4 /* Test for read permission. */
38 # define W_OK 2 /* Test for write permission. */
39 # define X_OK 1 /* Test for execute permission. */
40 # define F_OK 0 /* Test for existence. */
47 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
53 #define STATIC_ASSERT(expr, message) do { \
54 switch (0) { case 0: case (expr): ; } \
57 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
59 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #define MAX(a, b) ((a) > (b) ? (a) : (b))
62 /* Round up @a so it's divisible by @b. */
63 #define ROUNDUP(a, b) (((a) + (b) - 1) / (b) * (b))
65 #define STRINGIFY(x) #x
66 #define STRINGIFY2(x) STRINGIFY(x)
68 /* Check if a character is valid in a string literal */
72 /* Currently we only check for NULL character, but this could be extended
73 * in the future to further ASCII control characters. */
81 istrcmp(const char *a, const char *b);
84 istrncmp(const char *a, const char *b, size_t n);
87 streq(const char *s1, const char *s2)
90 return strcmp(s1, s2) == 0;
94 streq_null(const char *s1, const char *s2)
96 if (s1 == NULL || s2 == NULL)
102 streq_not_null(const char *s1, const char *s2)
106 return streq(s1, s2);
110 istreq(const char *s1, const char *s2)
112 return istrcmp(s1, s2) == 0;
116 istreq_prefix(const char *s1, const char *s2)
118 return istrncmp(s1, s2, strlen(s1)) == 0;
122 strdup_safe(const char *s)
124 return s ? strdup(s) : NULL;
128 strlen_safe(const char *s)
130 return s ? strlen(s) : 0;
134 isempty(const char *s)
136 return s == NULL || s[0] == '\0';
139 static inline const char *
140 strnull(const char *s)
142 return s ? s : "(null)";
145 static inline const char *
146 strempty(const char *s)
152 memdup(const void *mem, size_t nmemb, size_t size)
154 void *p = calloc(nmemb, size);
156 memcpy(p, mem, nmemb * size);
160 #if !(defined(HAVE_STRNDUP) && HAVE_STRNDUP)
162 strndup(const char *s, size_t n)
164 size_t slen = strlen(s);
165 size_t len = MIN(slen, n);
166 char *p = malloc(len + 1);
175 /* ctype.h is locale-dependent and has other oddities. */
179 return ch == ' ' || (ch >= '\t' && ch <= '\r');
185 return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
191 return ch >= '0' && ch <= '9';
197 return is_alpha(ch) || is_digit(ch);
204 (ch >= '0' && ch <= '9') ||
205 (ch >= 'a' && ch <= 'f') ||
206 (ch >= 'A' && ch <= 'F');
212 /* See table in ascii(7). */
213 return ch >= '!' && ch <= '~';
217 * Return the bit position of the most significant bit.
218 * Note: this is 1-based! It's more useful this way, and returns 0 when
221 static inline unsigned
222 msb_pos(uint32_t mask)
233 one_bit_set(uint32_t x)
235 return x && (x & (x - 1)) == 0;
239 map_file(FILE *file, char **string_out, size_t *size_out);
242 unmap_file(char *string, size_t size);
245 check_eaccess(const char *path, int mode)
247 #if defined(HAVE_EACCESS)
248 if (eaccess(path, mode) != 0)
250 #elif defined(HAVE_EUIDACCESS)
251 if (euidaccess(path, mode) != 0)
258 #if defined(HAVE_SECURE_GETENV)
259 # define secure_getenv secure_getenv
260 #elif defined(HAVE___SECURE_GETENV)
261 # define secure_getenv __secure_getenv
263 # define secure_getenv getenv
266 #if defined(HAVE___BUILTIN_EXPECT)
267 # define likely(x) __builtin_expect(!!(x), 1)
268 # define unlikely(x) __builtin_expect(!!(x), 0)
270 # define likely(x) (x)
271 # define unlikely(x) (x)
274 /* Compiler Attributes */
276 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
277 # define XKB_EXPORT __attribute__((visibility("default")))
278 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
279 # define XKB_EXPORT __global
280 #else /* not gcc >= 4 and not Sun Studio >= 8 */
284 #if defined(__MINGW32__)
285 # define ATTR_PRINTF(x,y) __attribute__((__format__(__MINGW_PRINTF_FORMAT, x, y)))
286 #elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
287 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
288 #else /* not gcc >= 2.3 */
289 # define ATTR_PRINTF(x,y)
292 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
293 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
294 # define ATTR_NORETURN __attribute__((__noreturn__))
296 # define ATTR_NORETURN
299 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
300 #define ATTR_MALLOC __attribute__((__malloc__))
305 #if defined(__GNUC__) && (__GNUC__ >= 4)
306 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
308 # define ATTR_NULL_SENTINEL
309 #endif /* GNUC >= 4 */
311 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 295)
312 #define ATTR_PACKED __attribute__((__packed__))
317 #if !(defined(HAVE_ASPRINTF) && HAVE_ASPRINTF)
318 int asprintf(char **strp, const char *fmt, ...) ATTR_PRINTF(2, 3);
319 # if !(defined(HAVE_VASPRINTF) && HAVE_VASPRINTF)
321 int vasprintf(char **strp, const char *fmt, va_list ap);
322 # endif /* !HAVE_VASPRINTF */
323 #endif /* !HAVE_ASPRINTF */
327 snprintf_safe(char *buf, size_t sz, const char *format, ...)
332 va_start(ap, format);
333 rc = vsnprintf(buf, sz, format, ap);
336 return rc >= 0 && (size_t)rc < sz;
341 vasprintf_safe(const char *fmt, va_list args)
346 len = vasprintf(&str, fmt, args);
355 * A version of asprintf that returns the allocated string or NULL on error.
359 asprintf_safe(const char *fmt, ...)
365 str = vasprintf_safe(fmt, args);