X-Git-Url: https://git.cworth.org/git?p=ttt;a=blobdiff_plain;f=src%2Fx.c;h=18c69a47457bf786aa9899999ed419ea96cdb4dd;hp=4062294d1f37e043b40e427fe70a73d694b8ac61;hb=ded32923a25488449be27687013845d7fa0e9e5e;hpb=cce891bb05118330ca609ac989491ad8a2fa7f71 diff --git a/src/x.c b/src/x.c index 4062294..18c69a4 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, ...) @@ -127,13 +125,27 @@ 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; } +void +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); + } +} + char * xstrdup (const char *s) { @@ -161,3 +173,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; +}