X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fx.c;h=a95fedfbc5e8a142606698cf4ab629368e1e2e8d;hp=3e46abdd03fb0c388c60d5494cce359cdcaf2cc6;hb=7de44ed398081656115dce6db7d0a04c1a50bb34;hpb=49f7f9e3dc198c3593021bff5514f19090a4834c diff --git a/src/x.c b/src/x.c index 3e46abd..a95fedf 100644 --- a/src/x.c +++ b/src/x.c @@ -19,9 +19,7 @@ * Author: Carl Worth */ -#include "ttt.h" - -#include +#include "x.h" void xasprintf (char **strp, const char *fmt, ...) @@ -135,7 +133,7 @@ xfdopen (int filedes, const char *mode) return ret; } -FILE * +void xfreopen (const char *path, const char *mode, FILE *stream) { FILE *ret; @@ -146,8 +144,6 @@ xfreopen (const char *path, const char *mode, FILE *stream) path, strerror (errno)); exit (1); } - - return ret; } char * @@ -155,6 +151,9 @@ xstrdup (const char *s) { char *ret; + if (s == NULL) + return NULL; + ret = strdup (s); if (ret == NULL) { @@ -206,6 +205,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) { @@ -252,3 +274,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; +}