]> 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 8da355e3e015b0c390cb0869e53a96d1e2bb01e1..c35f4c88955f3147271d86c1c3113140aeafb496 100644 (file)
--- a/src/x.c
+++ b/src/x.c
@@ -117,6 +117,22 @@ xrealloc (void *ptr, size_t size)
     return ret;
 }
 
+FILE *
+xfopen (const char *path, const char *mode)
+{
+    FILE *ret;
+
+    ret = fopen (path, mode);
+
+    if (ret == NULL) {
+       fprintf (stderr, "Error: fopen of %s failed: %s. Aborting.\n",
+                path, strerror (errno));
+       exit (1);
+    }
+
+    return ret;
+}
+
 FILE *
 xfdopen (int filedes, const char *mode)
 {
@@ -134,14 +150,14 @@ xfdopen (int filedes, const char *mode)
 }
 
 void
-xfreopen (const char *path, const char *mode, FILE *stream)
+xdup2 (int oldfd, int newfd)
 {
-    FILE *ret;
+    int ret;
 
-    ret = freopen (path, mode, stream);
-    if (ret == NULL) {
-       fprintf (stderr, "Error: freopen of %s failed: %s. Aborting.\n",
-                path, strerror (errno));
+    ret = dup2 (oldfd, newfd);
+    if (ret == -1) {
+       printf ("Error: dup2 failed: %s. Aborting.\n",
+               strerror (errno));
        exit (1);
     }
 }
@@ -151,6 +167,9 @@ xstrdup (const char *s)
 {
     char *ret;
 
+    if (s == NULL)
+       return NULL;
+
     ret = strdup (s);
 
     if (ret == NULL) {
@@ -202,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)
 {