* Author: Carl Worth <carl@theworths.org>
*/
-#include "ttt.h"
-
-#include <stdarg.h>
+#include "x.h"
void
xasprintf (char **strp, const char *fmt, ...)
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)
{
char *ret;
+ if (s == NULL)
+ return NULL;
+
ret = strdup (s);
if (ret == NULL) {
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;
+}
+
+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;
+}