]> oss.titaniummirror.com Git - repo_shell.git/blobdiff - utility.c
Code for supporting git ACLs. acl_test is useful.
[repo_shell.git] / utility.c
diff --git a/utility.c b/utility.c
new file mode 100644 (file)
index 0000000..ba7373b
--- /dev/null
+++ b/utility.c
@@ -0,0 +1,84 @@
+/*
+ * Copyright © 2012, Titanium Mirror, Inc.
+ * All Rights Reserved.
+ *
+ * This document is the proprietary and confidential property of
+ * Titanium Mirror, Inc.  All use, distribution, reproduction or re-distribution
+ * is disallowed without the prior express written consent of
+ * Titanium Mirror, Inc.
+ */
+
+/*
+ * Common functions for command line utility programs
+ *
+ * @author R. Steve McKown <rsmckown@gmail.com>
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include "utility.h"
+
+void debug(const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start(ap, fmt);
+  fprintf(stderr, "debug: ");
+  vfprintf(stderr, fmt, ap);
+  fprintf(stderr, "\n" );
+  va_end(ap);
+}
+
+void die(const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start(ap, fmt);
+  fprintf(stderr, "error: ");
+  vfprintf(stderr, fmt, ap);
+  fprintf(stderr, "\n" );
+  va_end(ap);
+  exit(1);
+}
+
+void *xmalloc(size_t size)
+{
+  void *ret;
+
+  if (!size)
+    die("xmalloc: size cannot be zero");
+  ret = malloc(size);
+  if (!ret)
+    die("out of memory (malloc)");
+  return ret;
+}
+
+void *xrealloc(void *ptr, size_t size)
+{
+  void *ret;
+
+  if (!size)
+    die("xrealloc: size cannot be zero");
+  ret = realloc(ptr, size);
+  if (!ret)
+    die("out of memory (realloc)");
+  return ret;
+}
+
+char *xstrdup(const char *str)
+{
+  char *ret = strdup(str);
+  if (!ret)
+    die("out of memory (strdup)");
+  return ret;
+}
+
+char *xstrndup(const char *str, size_t n)
+{
+  char *ret = strndup(str, n);
+  if (!ret)
+    die("out of memory (strndup)");
+  return ret;
+}