ii: Add a die() function to replace fprintf(3) + exit(3) calls

This commit is contained in:
Tom Schwindl 2022-08-09 15:52:36 +00:00 committed by Hiltjo Posthuma
parent 01537d014b
commit 71c1e50da0

92
ii.c
View File

@ -56,6 +56,7 @@ static int channel_reopen(Channel *);
static void channel_rm(Channel *); static void channel_rm(Channel *);
static void create_dirtree(const char *); static void create_dirtree(const char *);
static void create_filepath(char *, size_t, const char *, const char *, const char *); static void create_filepath(char *, size_t, const char *, const char *, const char *);
static void die(const char *, ...);
static void ewritestr(int, const char *); static void ewritestr(int, const char *);
static void handle_channels_input(int, Channel *); static void handle_channels_input(int, Channel *);
static void handle_server_output(int); static void handle_server_output(int);
@ -83,13 +84,23 @@ static char _nick[32]; /* nickname at startup */
static char ircpath[PATH_MAX]; /* irc dir (-i) */ static char ircpath[PATH_MAX]; /* irc dir (-i) */
static char msg[IRC_MSG_MAX]; /* message buf used for communication */ static char msg[IRC_MSG_MAX]; /* message buf used for communication */
static void
die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "usage: %s <-s host> [-i <irc dir>] [-p <port>] " die("usage: %s <-s host> [-i <irc dir>] [-p <port>] "
"[-u <sockname>] [-n <nick>] [-k <password>] " "[-u <sockname>] [-n <nick>] [-k <password>] "
"[-f <fullname>]\n", argv0); "[-f <fullname>]\n", argv0);
exit(1);
} }
static void static void
@ -103,10 +114,8 @@ ewritestr(int fd, const char *s)
if ((w = write(fd, s + off, len - off)) == -1) if ((w = write(fd, s + off, len - off)) == -1)
break; break;
} }
if (w == -1) { if (w == -1)
fprintf(stderr, "%s: write: %s\n", argv0, strerror(errno)); die("%s: write: %s\n", argv0, strerror(errno));
exit(1);
}
} }
/* creates directories bottom-up, if necessary */ /* creates directories bottom-up, if necessary */
@ -184,8 +193,7 @@ create_filepath(char *filepath, size_t len, const char *path,
return; return;
error: error:
fprintf(stderr, "%s: path to irc directory too long\n", argv0); die("%s: path to irc directory too long\n", argv0);
exit(1);
} }
static int static int
@ -229,10 +237,8 @@ channel_new(const char *name)
strlcpy(channelpath, name, sizeof(channelpath)); strlcpy(channelpath, name, sizeof(channelpath));
channel_normalize_path(channelpath); channel_normalize_path(channelpath);
if (!(c = calloc(1, sizeof(Channel)))) { if (!(c = calloc(1, sizeof(Channel))))
fprintf(stderr, "%s: calloc: %s\n", argv0, strerror(errno)); die("%s: calloc: %s\n", argv0, strerror(errno));
exit(1);
}
strlcpy(c->name, name, sizeof(c->name)); strlcpy(c->name, name, sizeof(c->name));
channel_normalize_name(c->name); channel_normalize_name(c->name);
@ -341,21 +347,17 @@ udsopen(const char *uds)
size_t len; size_t len;
int fd; int fd;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
fprintf(stderr, "%s: socket: %s\n", argv0, strerror(errno)); die("%s: socket: %s\n", argv0, strerror(errno));
exit(1);
}
sun.sun_family = AF_UNIX; sun.sun_family = AF_UNIX;
if (strlcpy(sun.sun_path, uds, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) { if (strlcpy(sun.sun_path, uds, sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
fprintf(stderr, "%s: UNIX domain socket path truncation\n", argv0); die("%s: UNIX domain socket path truncation\n", argv0);
exit(1);
}
len = strlen(sun.sun_path) + 1 + sizeof(sun.sun_family); len = strlen(sun.sun_path) + 1 + sizeof(sun.sun_family);
if (connect(fd, (struct sockaddr *)&sun, len) == -1) { if (connect(fd, (struct sockaddr *)&sun, len) == -1)
fprintf(stderr, "%s: connect: %s\n", argv0, strerror(errno)); die("%s: connect: %s\n", argv0, strerror(errno));
exit(1);
}
return fd; return fd;
} }
@ -370,10 +372,8 @@ tcpopen(const char *host, const char *service)
hints.ai_flags = AI_NUMERICSERV; /* avoid name lookup for port */ hints.ai_flags = AI_NUMERICSERV; /* avoid name lookup for port */
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
if ((e = getaddrinfo(host, service, &hints, &res))) { if ((e = getaddrinfo(host, service, &hints, &res)))
fprintf(stderr, "%s: getaddrinfo: %s\n", argv0, gai_strerror(e)); die("%s: getaddrinfo: %s\n", argv0, gai_strerror(e));
exit(1);
}
for (rp = res; rp; rp = rp->ai_next) { for (rp = res; rp; rp = rp->ai_next) {
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
@ -386,11 +386,9 @@ tcpopen(const char *host, const char *service)
} }
break; /* success */ break; /* success */
} }
if (fd == -1) { if (fd == -1)
fprintf(stderr, "%s: could not connect to %s:%s: %s\n", die("%s: could not connect to %s:%s: %s\n",
argv0, host, service, strerror(errno)); argv0, host, service, strerror(errno));
exit(1);
}
freeaddrinfo(res); freeaddrinfo(res);
return fd; return fd;
@ -705,11 +703,9 @@ handle_server_output(int ircfd)
{ {
char buf[IRC_MSG_MAX]; char buf[IRC_MSG_MAX];
if (read_line(ircfd, buf, sizeof(buf)) == -1) { if (read_line(ircfd, buf, sizeof(buf)) == -1)
fprintf(stderr, "%s: remote host closed connection: %s\n", die("%s: remote host closed connection: %s\n", argv0, strerror(errno));
argv0, strerror(errno));
exit(1);
}
fprintf(stdout, "%lu %s\n", (unsigned long)time(NULL), buf); fprintf(stdout, "%lu %s\n", (unsigned long)time(NULL), buf);
fflush(stdout); fflush(stdout);
proc_server_cmd(ircfd, buf); proc_server_cmd(ircfd, buf);
@ -758,8 +754,7 @@ run(int ircfd, const char *host)
if (r < 0) { if (r < 0) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
fprintf(stderr, "%s: select: %s\n", argv0, strerror(errno)); die("%s: select: %s\n", argv0, strerror(errno));
exit(1);
} else if (r == 0) { } else if (r == 0) {
if (time(NULL) - last_response >= PING_TIMEOUT) { if (time(NULL) - last_response >= PING_TIMEOUT) {
channel_print(channelmaster, "-!- ii shutting down: ping timeout"); channel_print(channelmaster, "-!- ii shutting down: ping timeout");
@ -791,10 +786,9 @@ main(int argc, char *argv[])
int ircfd, r; int ircfd, r;
/* use nickname and home dir of user by default */ /* use nickname and home dir of user by default */
if (!(spw = getpwuid(getuid()))) { if (!(spw = getpwuid(getuid())))
fprintf(stderr, "%s: getpwuid: %s\n", argv0, strerror(errno)); die("%s: getpwuid: %s\n", argv0, strerror(errno));
exit(1);
}
strlcpy(nick, spw->pw_name, sizeof(nick)); strlcpy(nick, spw->pw_name, sizeof(nick));
snprintf(prefix, sizeof(prefix), "%s/irc", spw->pw_dir); snprintf(prefix, sizeof(prefix), "%s/irc", spw->pw_dir);
@ -835,17 +829,13 @@ main(int argc, char *argv[])
#ifdef __OpenBSD__ #ifdef __OpenBSD__
/* OpenBSD pledge(2) support */ /* OpenBSD pledge(2) support */
if (pledge("stdio rpath wpath cpath dpath", NULL) == -1) { if (pledge("stdio rpath wpath cpath dpath", NULL) == -1)
fprintf(stderr, "%s: pledge: %s\n", argv0, strerror(errno)); die("%s: pledge: %s\n", argv0, strerror(errno));
exit(1);
}
#endif #endif
r = snprintf(ircpath, sizeof(ircpath), "%s/%s", prefix, host); r = snprintf(ircpath, sizeof(ircpath), "%s/%s", prefix, host);
if (r < 0 || (size_t)r >= sizeof(ircpath)) { if (r < 0 || (size_t)r >= sizeof(ircpath))
fprintf(stderr, "%s: path to irc directory too long\n", argv0); die("%s: path to irc directory too long\n", argv0);
exit(1);
}
create_dirtree(ircpath); create_dirtree(ircpath);
channelmaster = channel_add(""); /* master channel */ channelmaster = channel_add(""); /* master channel */