]> git.cworth.org Git - ttt/blobdiff - src/x.c
2005-11-05 Carl Worth <cworth@cworth.org>
[ttt] / src / x.c
diff --git a/src/x.c b/src/x.c
new file mode 100644 (file)
index 0000000..4062294
--- /dev/null
+++ b/src/x.c
@@ -0,0 +1,163 @@
+/* x.c - Some wrappers for various functions to check and exit on out-of-memory
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Carl Worth <carl@theworths.org>
+ */
+
+#include "ttt.h"
+
+#include <stdarg.h>
+
+void
+xasprintf (char **strp, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start (ap, fmt);
+    xvasprintf (strp, fmt, ap);
+    va_end (ap);
+}
+
+void
+xvasprintf (char **strp, const char *fmt, va_list ap)
+{
+    int ret;
+
+    ret = vasprintf (strp, fmt, ap);
+
+    if (ret == -1) {
+       fprintf (stderr, "Error: vasprintf failed: Out of memory? Aborting.\n");
+       exit (1);
+    }
+}
+
+void
+xpipe (int filedes[2])
+{
+    int ret;
+
+    ret = pipe (filedes);
+
+    if (ret == -1) {
+       fprintf (stderr, "Error: pipe failed: %s. Aborting.\n", strerror (errno));
+       exit (1);
+    }
+}
+
+pid_t
+xfork (void)
+{
+    pid_t pid;
+
+    pid = fork ();
+
+    if (pid == -1) {
+       fprintf (stderr, "Error: fork failed: %s. Aborting.\n", strerror (errno));
+       exit (1);
+    }
+
+    return pid;
+}
+
+void *
+xmalloc (size_t size)
+{
+    void *ret;
+
+    ret = malloc (size);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: malloc failed. Out of memory.  Aborting.\n");
+       exit (1);
+    }
+
+    return ret;
+}
+
+void *
+xcalloc (size_t nmemb, size_t size)
+{
+    void *ret;
+
+    ret = calloc (nmemb, size);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: calloc failed. Out of memory. Aborting.\n");
+       exit (1);
+    }
+
+    return ret;
+}
+
+void *
+xrealloc (void *ptr, size_t size)
+{
+    void *ret;
+
+    ret = realloc (ptr, size);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: realloc failed: Out of memory. Aborting.\n");
+       exit (1);
+    }
+
+    return ret;
+}
+
+FILE *
+xfdopen (int filedes, const char *mode)
+{
+    FILE *ret;
+
+    ret = fdopen (filedes, mode);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n", strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
+char *
+xstrdup (const char *s)
+{
+    char *ret;
+
+    ret = strdup (s);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: strdup failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
+void
+xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
+{
+    int ret;
+
+    ret = fwrite (ptr, size, nmemb, stream);
+    if (ret != nmemb) {
+       fprintf (stderr, "Error: error occured during fwrite. Aborting.\n");
+       exit (1);
+    }
+}