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. */
45 #define STATIC_ASSERT(expr, message) do { \
46 switch (0) { case 0: case (expr): ; } \
49 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52 #define MAX(a, b) ((a) > (b) ? (a) : (b))
54 /* Round up @a so it's divisible by @b. */
55 #define ROUNDUP(a, b) (((a) + (b) - 1) / (b) * (b))
61 istrcmp(const char *a, const char *b);
64 istrncmp(const char *a, const char *b, size_t n);
67 streq(const char *s1, const char *s2)
70 return strcmp(s1, s2) == 0;
74 streq_null(const char *s1, const char *s2)
76 if (s1 == NULL || s2 == NULL)
82 streq_not_null(const char *s1, const char *s2)
90 istreq(const char *s1, const char *s2)
92 return istrcmp(s1, s2) == 0;
96 istreq_prefix(const char *s1, const char *s2)
98 return istrncmp(s1, s2, strlen(s1)) == 0;
102 strdup_safe(const char *s)
104 return s ? strdup(s) : NULL;
108 strlen_safe(const char *s)
110 return s ? strlen(s) : 0;
114 isempty(const char *s)
116 return s == NULL || s[0] == '\0';
119 static inline const char *
120 strnull(const char *s)
122 return s ? s : "(null)";
125 static inline const char *
126 strempty(const char *s)
132 memdup(const void *mem, size_t nmemb, size_t size)
134 void *p = calloc(nmemb, size);
136 memcpy(p, mem, nmemb * size);
140 #if !(defined(HAVE_STRNDUP) && HAVE_STRNDUP)
142 strndup(const char *s, size_t n)
144 size_t slen = strlen(s);
145 size_t len = MIN(slen, n);
146 char *p = malloc(len + 1);
155 /* ctype.h is locale-dependent and has other oddities. */
159 return ch == ' ' || (ch >= '\t' && ch <= '\r');
165 return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
171 return ch >= '0' && ch <= '9';
177 return is_alpha(ch) || is_digit(ch);
184 (ch >= '0' && ch <= '9') ||
185 (ch >= 'a' && ch <= 'f') ||
186 (ch >= 'A' && ch <= 'F');
192 /* See table in ascii(7). */
193 return ch >= '!' && ch <= '~';
197 * Return the bit position of the most significant bit.
198 * Note: this is 1-based! It's more useful this way, and returns 0 when
201 static inline unsigned
202 msb_pos(uint32_t mask)
213 one_bit_set(uint32_t x)
215 return x && (x & (x - 1)) == 0;
219 map_file(FILE *file, char **string_out, size_t *size_out);
222 unmap_file(char *string, size_t size);
225 check_eaccess(const char *path, int mode)
227 #if defined(HAVE_EACCESS)
228 if (eaccess(path, mode) != 0)
230 #elif defined(HAVE_EUIDACCESS)
231 if (euidaccess(path, mode) != 0)
238 #if defined(HAVE_SECURE_GETENV)
239 # define secure_getenv secure_getenv
240 #elif defined(HAVE___SECURE_GETENV)
241 # define secure_getenv __secure_getenv
243 # define secure_getenv getenv
246 #if defined(HAVE___BUILTIN_EXPECT)
247 # define likely(x) __builtin_expect(!!(x), 1)
248 # define unlikely(x) __builtin_expect(!!(x), 0)
250 # define likely(x) (x)
251 # define unlikely(x) (x)
254 /* Compiler Attributes */
256 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
257 # define XKB_EXPORT __attribute__((visibility("default")))
258 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
259 # define XKB_EXPORT __global
260 #else /* not gcc >= 4 and not Sun Studio >= 8 */
264 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
265 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
266 #else /* not gcc >= 2.3 */
267 # define ATTR_PRINTF(x,y)
270 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
271 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
272 # define ATTR_NORETURN __attribute__((__noreturn__))
274 # define ATTR_NORETURN
277 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
278 #define ATTR_MALLOC __attribute__((__malloc__))
283 #if defined(__GNUC__) && (__GNUC__ >= 4)
284 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
286 # define ATTR_NULL_SENTINEL
287 #endif /* GNUC >= 4 */
289 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 295)
290 #define ATTR_PACKED __attribute__((__packed__))
295 #if !(defined(HAVE_ASPRINTF) && HAVE_ASPRINTF)
296 int asprintf(char **strp, const char *fmt, ...) ATTR_PRINTF(2, 3);
297 # if !(defined(HAVE_VASPRINTF) && HAVE_VASPRINTF)
299 int vasprintf(char **strp, const char *fmt, va_list ap);
300 # endif /* !HAVE_VASPRINTF */
301 #endif /* !HAVE_ASPRINTF */
305 snprintf_safe(char *buf, size_t sz, const char *format, ...)
310 va_start(ap, format);
311 rc = vsnprintf(buf, sz, format, ap);
314 return rc >= 0 && (size_t)rc < sz;
319 vasprintf_safe(const char *fmt, va_list args)
324 len = vasprintf(&str, fmt, args);
333 * A version of asprintf that returns the allocated string or NULL on error.
337 asprintf_safe(const char *fmt, ...)
343 str = vasprintf_safe(fmt, args);