added eprint()
This commit is contained in:
parent
81238e16fe
commit
75f0af7c42
62
sic.c
62
sic.c
@ -5,6 +5,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -27,6 +28,16 @@ static char channel[256];
|
|||||||
static int srv;
|
static int srv;
|
||||||
static time_t trespond;
|
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
|
static int
|
||||||
getline(int fd, unsigned int len, char *buf) {
|
getline(int fd, unsigned int len, char *buf) {
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
@ -166,32 +177,23 @@ main(int argc, char *argv[]) {
|
|||||||
else if(!strncmp(argv[i], "-f", 3)) {
|
else if(!strncmp(argv[i], "-f", 3)) {
|
||||||
if(++i < argc) fullname = argv[i];
|
if(++i < argc) fullname = argv[i];
|
||||||
}
|
}
|
||||||
else if(!strncmp(argv[i], "-v", 3)) {
|
else if(!strncmp(argv[i], "-v", 3))
|
||||||
fputs("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
|
eprint("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n");
|
||||||
exit(EXIT_SUCCESS);
|
else
|
||||||
}
|
eprint("usage: sic [-h host] [-p port] [-n nick]"
|
||||||
else {
|
" [-k keyword] [-f fullname] [-v]\n");
|
||||||
fputs("usage: sic [-h host] [-p port] [-n nick]"
|
|
||||||
" [-k keyword] [-f fullname] [-v]\n", stderr);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* init */
|
/* init */
|
||||||
if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||||
fprintf(stderr, "sic: cannot connect host '%s'\n", host);
|
eprint("sic: cannot connect host '%s'\n", host);
|
||||||
exit(EXIT_FAILURE);
|
if(NULL == (hp = gethostbyname(host)))
|
||||||
}
|
eprint("sic: cannot resolve hostname '%s'\n", host);
|
||||||
if (NULL == (hp = gethostbyname(host))) {
|
|
||||||
fprintf(stderr, "sic: cannot resolve hostname '%s'\n", host);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(port);
|
addr.sin_port = htons(port);
|
||||||
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
|
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
|
||||||
if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
|
if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
|
||||||
close(srv);
|
close(srv);
|
||||||
fprintf(stderr, "sic: cannot connect host '%s'\n", host);
|
eprint("sic: cannot connect host '%s'\n", host);
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
/* login */
|
/* login */
|
||||||
if(password)
|
if(password)
|
||||||
@ -216,29 +218,23 @@ main(int argc, char *argv[]) {
|
|||||||
if(i < 0) {
|
if(i < 0) {
|
||||||
if(errno == EINTR)
|
if(errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
perror("sic: error on select()");
|
eprint("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);
|
|
||||||
}
|
}
|
||||||
|
else if(i == 0) {
|
||||||
|
if(time(NULL) - trespond >= PINGTIMEOUT)
|
||||||
|
eprint("sic shutting down: parse timeout");
|
||||||
write(srv, ping, strlen(ping));
|
write(srv, ping, strlen(ping));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(FD_ISSET(srv, &rd)) {
|
if(FD_ISSET(srv, &rd)) {
|
||||||
if(getline(srv, sizeof bufin, bufin) == -1) {
|
if(getline(srv, sizeof bufin, bufin) == -1)
|
||||||
perror("sic: remote host closed connection");
|
eprint("sic: remote host closed connection");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
parsesrv(bufin);
|
parsesrv(bufin);
|
||||||
trespond = time(NULL);
|
trespond = time(NULL);
|
||||||
}
|
}
|
||||||
if(FD_ISSET(0, &rd)) {
|
if(FD_ISSET(0, &rd)) {
|
||||||
if(getline(0, sizeof bufin, bufin) == -1) {
|
if(getline(0, sizeof bufin, bufin) == -1)
|
||||||
perror("sic: broken pipe");
|
eprint("sic: broken pipe");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
parsein(bufin);
|
parsein(bufin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user