]> git.armaanb.net Git - sic.git/commitdiff
added eprint()
authorAnselm R. Garbe <arg@suckless.org>
Fri, 9 Feb 2007 15:04:49 +0000 (16:04 +0100)
committerAnselm R. Garbe <arg@suckless.org>
Fri, 9 Feb 2007 15:04:49 +0000 (16:04 +0100)
sic.c

diff --git a/sic.c b/sic.c
index 6580bdf3d128cf6600568ce1e687808e985f1031..ec66f19c4d4af5aae80b2f64a0cc7c2a731d9281 100644 (file)
--- a/sic.c
+++ b/sic.c
@@ -5,6 +5,7 @@
 #include <errno.h>
 #include <netdb.h>
 #include <netinet/in.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -27,6 +28,16 @@ static char channel[256];
 static int srv;
 static time_t trespond;
 
+static void
+eprint(const char *errstr, ...) {
+       va_list ap;
+
+       va_start(ap, errstr);
+       vfprintf(stderr, errstr, ap);
+       va_end(ap);
+       exit(EXIT_FAILURE);
+}
+
 static int
 getline(int fd, unsigned int len, char *buf) {
        unsigned int i = 0;
@@ -166,32 +177,23 @@ main(int argc, char *argv[]) {
                else if(!strncmp(argv[i], "-f", 3)) {
                        if(++i < argc) fullname = argv[i];
                }
-               else if(!strncmp(argv[i], "-v", 3)) {
-                       fputs("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
-                       exit(EXIT_SUCCESS);
-               }
-               else {
-                       fputs("usage: sic [-h host] [-p port] [-n nick]"
-                                       " [-k keyword] [-f fullname] [-v]\n", stderr);
-                       exit(EXIT_FAILURE);
-               }
+               else if(!strncmp(argv[i], "-v", 3))
+                       eprint("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n");
+               else
+                       eprint("usage: sic [-h host] [-p port] [-n nick]"
+                               " [-k keyword] [-f fullname] [-v]\n");
 
        /* init */
-       if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-               fprintf(stderr, "sic: cannot connect host '%s'\n", host);
-               exit(EXIT_FAILURE);
-       }
-       if (NULL == (hp = gethostbyname(host))) {
-               fprintf(stderr, "sic: cannot resolve hostname '%s'\n", host);
-               exit(EXIT_FAILURE);
-       }
+       if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+               eprint("sic: cannot connect host '%s'\n", host);
+       if(NULL == (hp = gethostbyname(host)))
+               eprint("sic: cannot resolve hostname '%s'\n", host);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
        if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
                close(srv);
-               fprintf(stderr, "sic: cannot connect host '%s'\n", host);
-               exit(EXIT_FAILURE);
+               eprint("sic: cannot connect host '%s'\n", host);
        }
        /* login */
        if(password)
@@ -216,29 +218,23 @@ main(int argc, char *argv[]) {
                if(i < 0) {
                        if(errno == EINTR)
                                continue;
-                       perror("sic: error on select()");
-                       exit(EXIT_FAILURE);
-               } else if(i == 0) {
-                       if(time(NULL) - trespond >= PINGTIMEOUT) {
-                               pout(host, "-!- sic shutting down: parse timeout");
-                               exit(EXIT_FAILURE);
-                       }
+                       eprint("sic: error on select()");
+               }
+               else if(i == 0) {
+                       if(time(NULL) - trespond >= PINGTIMEOUT)
+                               eprint("sic shutting down: parse timeout");
                        write(srv, ping, strlen(ping));
                        continue;
                }
                if(FD_ISSET(srv, &rd)) {
-                       if(getline(srv, sizeof bufin, bufin) == -1) {
-                               perror("sic: remote host closed connection");
-                               exit(EXIT_FAILURE);
-                       }
+                       if(getline(srv, sizeof bufin, bufin) == -1)
+                               eprint("sic: remote host closed connection");
                        parsesrv(bufin);
                        trespond = time(NULL);
                }
                if(FD_ISSET(0, &rd)) {
-                       if(getline(0, sizeof bufin, bufin) == -1) {
-                               perror("sic: broken pipe");
-                               exit(EXIT_FAILURE);
-                       }
+                       if(getline(0, sizeof bufin, bufin) == -1)
+                               eprint("sic: broken pipe");
                        parsein(bufin);
                }
        }