]> git.cworth.org Git - ttt/blobdiff - src/x.c
2005-12-08 Carl Worth <cworth@cworth.org>
[ttt] / src / x.c
diff --git a/src/x.c b/src/x.c
index 54566423e03f29c45c0d65f190e448a2ae18c79b..c35f4c88955f3147271d86c1c3113140aeafb496 100644 (file)
--- a/src/x.c
+++ b/src/x.c
@@ -118,15 +118,15 @@ xrealloc (void *ptr, size_t size)
 }
 
 FILE *
-xfdopen (int filedes, const char *mode)
+xfopen (const char *path, const char *mode)
 {
     FILE *ret;
 
-    ret = fdopen (filedes, mode);
+    ret = fopen (path, mode);
 
     if (ret == NULL) {
-       fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n",
-                strerror (errno));
+       fprintf (stderr, "Error: fopen of %s failed: %s. Aborting.\n",
+                path, strerror (errno));
        exit (1);
     }
 
@@ -134,25 +134,42 @@ xfdopen (int filedes, const char *mode)
 }
 
 FILE *
-xfreopen (const char *path, const char *mode, FILE *stream)
+xfdopen (int filedes, const char *mode)
 {
     FILE *ret;
 
-    ret = freopen (path, mode, stream);
+    ret = fdopen (filedes, mode);
+
     if (ret == NULL) {
-       fprintf (stderr, "Error: freopen of %s failed: %s. Aborting.\n",
-                path, strerror (errno));
+       fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n",
+                strerror (errno));
        exit (1);
     }
 
     return ret;
 }
 
+void
+xdup2 (int oldfd, int newfd)
+{
+    int ret;
+
+    ret = dup2 (oldfd, newfd);
+    if (ret == -1) {
+       printf ("Error: dup2 failed: %s. Aborting.\n",
+               strerror (errno));
+       exit (1);
+    }
+}
+
 char *
 xstrdup (const char *s)
 {
     char *ret;
 
+    if (s == NULL)
+       return NULL;
+
     ret = strdup (s);
 
     if (ret == NULL) {
@@ -204,6 +221,29 @@ xbind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
     }
 }
 
+ttt_status_t
+xconnect (int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
+{
+    int ret;
+
+    ret = connect (sockfd, serv_addr, addrlen);
+    if (ret == -1) {
+       switch (errno) {
+       case ECONNREFUSED:
+           return TTT_STATUS_CONNECTION_REFUSED;
+       case EHOSTUNREACH:
+       case ENETUNREACH:
+           return TTT_STATUS_NETWORK_UNREACHABLE;
+       default:
+       fprintf (stderr, "Error: connect failed (errno = %d): %s. Aborting.\n",
+                errno, strerror (errno));
+       exit (1);
+       }
+    }
+
+    return TTT_STATUS_SUCCESS;
+}
+
 void
 xlisten (int s, int backlog)
 {
@@ -250,3 +290,33 @@ xselect (int                n,
 
     return ret;
 }
+
+ssize_t
+xread (int fd, void *buf, size_t count)
+{
+    int ret;
+
+    ret = read (fd, buf, count);
+    if (ret == -1) {
+       fprintf (stderr, "Error: read failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
+ssize_t
+xwrite (int fd, const void *buf, size_t count)
+{
+    int ret;
+
+    ret = write (fd, buf, count);
+    if (ret == -1) {
+       fprintf (stderr, "Error: write failed: %s. Aborting.\n",
+                strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}