- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of Digital Equipment Corporation not be
- * used in advertising or publicity pertaining to distribution of the
- * software without specific, written prior permission.
- \*/
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef UTILS_H
+#define UTILS_H 1
+
+#include <errno.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#else
+/* Required on Windows where unistd.h doesn't exist */
+# define R_OK 4 /* Test for read permission. */
+# define W_OK 2 /* Test for write permission. */
+# define X_OK 1 /* Test for execute permission. */
+# define F_OK 0 /* Test for existence. */
+#endif
+
+#ifdef _WIN32
+# include <direct.h>
+# include <io.h>
+# ifndef S_ISDIR
+# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
+# endif
+#endif
+
+#include "darray.h"
+
+#define STATIC_ASSERT(expr, message) do { \
+ switch (0) { case 0: case (expr): ; } \
+} while (0)
+
+#define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+/* Round up @a so it's divisible by @b. */
+#define ROUNDUP(a, b) (((a) + (b) - 1) / (b) * (b))
+
+#define STRINGIFY(x) #x
+#define STRINGIFY2(x) STRINGIFY(x)
+
+/* Check if a character is valid in a string literal */
+static inline bool
+is_valid_char(char c)
+{
+ /* Currently we only check for NULL character, but this could be extended
+ * in the future to further ASCII control characters. */
+ return c != 0;
+}
+
+char
+to_lower(char c);
+
+int
+istrcmp(const char *a, const char *b);
+
+int
+istrncmp(const char *a, const char *b, size_t n);
+
+static inline bool
+streq(const char *s1, const char *s2)
+{
+ assert(s1 && s2);
+ return strcmp(s1, s2) == 0;
+}
+
+static inline bool
+streq_null(const char *s1, const char *s2)
+{
+ if (s1 == NULL || s2 == NULL)
+ return s1 == s2;
+ return streq(s1, s2);
+}
+
+static inline bool
+streq_not_null(const char *s1, const char *s2)
+{
+ if (!s1 || !s2)
+ return false;
+ return streq(s1, s2);
+}
+
+static inline bool
+istreq(const char *s1, const char *s2)
+{
+ return istrcmp(s1, s2) == 0;
+}
+
+static inline bool
+istreq_prefix(const char *s1, const char *s2)
+{
+ return istrncmp(s1, s2, strlen(s1)) == 0;
+}
+
+static inline char *
+strdup_safe(const char *s)
+{
+ return s ? strdup(s) : NULL;
+}
+
+static inline size_t
+strlen_safe(const char *s)
+{
+ return s ? strlen(s) : 0;
+}
+
+static inline bool
+isempty(const char *s)
+{
+ return s == NULL || s[0] == '\0';
+}
+
+static inline const char *
+strnull(const char *s)
+{
+ return s ? s : "(null)";
+}
+
+static inline const char *
+strempty(const char *s)
+{
+ return s ? s : "";
+}
+
+static inline void *
+memdup(const void *mem, size_t nmemb, size_t size)
+{
+ void *p = calloc(nmemb, size);
+ if (p)
+ memcpy(p, mem, nmemb * size);
+ return p;
+}