]> git.cworth.org Git - ttt/blobdiff - src/x.c
2005-11-11 Carl Worth <cworth@cworth.org>
[ttt] / src / x.c
diff --git a/src/x.c b/src/x.c
index 4062294d1f37e043b40e427fe70a73d694b8ac61..54566423e03f29c45c0d65f190e448a2ae18c79b 100644 (file)
--- a/src/x.c
+++ b/src/x.c
@@ -19,9 +19,7 @@
  * Author: Carl Worth <carl@theworths.org>
  */
 
-#include "ttt.h"
-
-#include <stdarg.h>
+#include "x.h"
 
 void
 xasprintf (char **strp, const char *fmt, ...)
@@ -127,7 +125,23 @@ xfdopen (int filedes, const char *mode)
     ret = fdopen (filedes, mode);
 
     if (ret == NULL) {
-       fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n", strerror (errno));
+       fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
+FILE *
+xfreopen (const char *path, const char *mode, FILE *stream)
+{
+    FILE *ret;
+
+    ret = freopen (path, mode, stream);
+    if (ret == NULL) {
+       fprintf (stderr, "Error: freopen of %s failed: %s. Aborting.\n",
+                path, strerror (errno));
        exit (1);
     }
 
@@ -161,3 +175,78 @@ xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
        exit (1);
     }
 }
+
+int
+xsocket (int domain, int type, int protocol)
+{
+    int ret;
+
+    ret = socket (domain, type, protocol);
+    if (ret == -1) {
+       fprintf (stderr, "Error: socket failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
+void
+xbind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
+{
+    int ret;
+
+    ret = bind (sockfd, my_addr, addrlen);
+    if (ret == -1) {
+       fprintf (stderr, "Error: bind failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+}
+
+void
+xlisten (int s, int backlog)
+{
+    int ret;
+
+    ret = listen (s, backlog);
+    if (ret == -1) {
+       fprintf (stderr, "Error: listen failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+}
+
+int
+xfcntl (int fd, int cmd, long arg)
+{
+    int ret;
+
+    ret = fcntl (fd, cmd, arg);
+    if (ret == -1) {
+       fprintf (stderr, "Error: fcntl failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
+int
+xselect (int            n,
+        fd_set         *readfds,
+        fd_set         *writefds,
+        fd_set         *exceptfds,
+        struct timeval *timeout)
+{
+    int ret;
+
+    ret = select (n, readfds, writefds, exceptfds, timeout);
+    if (ret == -1) {
+       fprintf (stderr, "Error: select failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}