aboutsummaryrefslogtreecommitdiff
path: root/src/shared/helpers.h
diff options
context:
space:
mode:
authorWilliam Hubbs <w.d.hubbs@gmail.com>2022-04-06 10:51:55 -0500
committerWilliam Hubbs <w.d.hubbs@gmail.com>2022-04-06 10:51:55 -0500
commit391d12db48754861b5cecac92ee3321597ee02c1 (patch)
treeb42fad5a31ca342de7b7ecf1fb78784194c1400c /src/shared/helpers.h
parent0efc1b133e4182bd53cde78153bd8b5cc2e99448 (diff)
migrate fully to meson build system
- drop old build system - move shared include and source files to common directory - drop "rc-" prefix from shared include and source files - move executable-specific code to individual directories under src - adjust top-level .gitignore file for new build system This closes #489.
Diffstat (limited to 'src/shared/helpers.h')
-rw-r--r--src/shared/helpers.h174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/shared/helpers.h b/src/shared/helpers.h
new file mode 100644
index 00000000..44d76552
--- /dev/null
+++ b/src/shared/helpers.h
@@ -0,0 +1,174 @@
+/*
+ * helpers.h
+ * This is private to us and not for user consumption
+ */
+
+/*
+ * Copyright (c) 2007-2015 The OpenRC Authors.
+ * See the Authors file at the top-level directory of this distribution and
+ * https://github.com/OpenRC/openrc/blob/HEAD/AUTHORS
+ *
+ * This file is part of OpenRC. It is subject to the license terms in
+ * the LICENSE file found in the top-level directory of this
+ * distribution and at https://github.com/OpenRC/openrc/blob/HEAD/LICENSE
+ * This file may not be copied, modified, propagated, or distributed
+ * except according to the terms contained in the LICENSE file.
+ */
+
+#ifndef __HELPERS_H__
+#define __HELPERS_H__
+
+#define ERRX fprintf (stderr, "out of memory\n"); exit (1)
+
+#define UNCONST(a) ((void *)(unsigned long)(const void *)(a))
+
+#ifdef lint
+# define _unused
+#endif
+#if __GNUC__ > 2 || defined(__INTEL_COMPILER)
+# define _dead __attribute__((__noreturn__))
+# define _unused __attribute__((__unused__))
+# define _xasprintf(a, b) __attribute__((__format__(__printf__, a, b)))
+#else
+# define _dead
+# define _unused
+# define _xasprintf(a, b)
+#endif
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+#ifdef __GLIBC__
+# if !defined (__UCLIBC__) && !defined (__dietlibc__)
+# define strlcpy(dst, src, size) snprintf(dst, size, "%s", src)
+# endif
+#endif
+
+#ifndef timespecsub
+#define timespecsub(tsp, usp, vsp) \
+ do { \
+ (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
+ (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
+ if ((vsp)->tv_nsec < 0) { \
+ (vsp)->tv_sec--; \
+ (vsp)->tv_nsec += 1000000000L; \
+ } \
+ } while (/* CONSTCOND */ 0)
+#endif
+
+#include <stdarg.h>
+#include <stdbool.h>
+#include <sys/stat.h>
+
+_unused static void *xmalloc (size_t size)
+{
+ void *value = malloc(size);
+
+ if (value)
+ return (value);
+
+ ERRX;
+ /* NOTREACHED */
+}
+
+_unused static void *xrealloc(void *ptr, size_t size)
+{
+ void *value = realloc(ptr, size);
+
+ if (value)
+ return (value);
+
+ ERRX;
+ /* NOTREACHED */
+}
+
+_unused static char *xstrdup(const char *str)
+{
+ char *value;
+
+ if (!str)
+ return (NULL);
+
+ value = strdup(str);
+
+ if (value)
+ return (value);
+
+ ERRX;
+ /* NOTREACHED */
+}
+
+#undef ERRX
+
+/*
+ * basename_c never modifies the argument. As such, if there is a trailing
+ * slash then an empty string is returned.
+ */
+_unused static const char *basename_c(const char *path)
+{
+ const char *slash = strrchr(path, '/');
+
+ if (slash)
+ return (++slash);
+ return (path);
+}
+
+_unused static bool exists(const char *pathname)
+{
+ struct stat buf;
+
+ return (stat(pathname, &buf) == 0);
+}
+
+_unused static bool existss(const char *pathname)
+{
+ struct stat buf;
+
+ return (stat(pathname, &buf) == 0 && buf.st_size != 0);
+}
+
+/*
+ * This is an OpenRC specific version of the asprintf() function.
+ * We do this to avoid defining the _GNU_SOURCE feature test macro on
+ * glibc systems and to insure that we have a consistent function across
+ * platforms. This also allows us to call our xmalloc and xrealloc
+ * functions to handle memory allocation.
+ * this function was originally written by Mike Frysinger.
+ */
+_unused _xasprintf(2,3) static int xasprintf(char **strp, const char *fmt, ...)
+{
+ va_list ap;
+ int len;
+ int memlen;
+ char *ret;
+
+ /*
+ * Start with a buffer size that should cover the vast majority of uses
+ * (path construction).
+ */
+ memlen = 4096;
+ ret = xmalloc(memlen);
+
+ va_start(ap, fmt);
+ len = vsnprintf(ret, memlen, fmt, ap);
+ va_end(ap);
+ if (len >= memlen) {
+ /*
+ * Output was truncated, so increase buffer to exactly what we need.
+ */
+ memlen = len + 1;
+ ret = xrealloc(ret, memlen);
+ va_start(ap, fmt);
+ len = vsnprintf(ret, len + 1, fmt, ap);
+ va_end(ap);
+ }
+ if (len < 0 || len >= memlen) {
+ /* Give up! */
+ fprintf(stderr, "xasprintf: unable to format a buffer\n");
+ free(ret);
+ exit(1);
+ }
+ *strp = ret;
+ return len;
+}
+
+#endif