[PATCH 40/65] simplify some more

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sat, 23 Aug 2014 16:27:23 +0000

Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
---
 ii.c | 63 +++++++++++++++++++++++++++++++++------------------------------
 1 file changed, 33 insertions(+), 30 deletions(-)
diff --git a/ii.c b/ii.c
index 283a415..760cc99 100644
--- a/ii.c
+++ b/ii.c
_AT_@ -35,7 +35,7 @@ struct Channel {
 static time_t last_response = 0;
 static Channel *channels = NULL;
 static char nick[32]; /* might change while running */
-static char path[_POSIX_PATH_MAX]; /* irc dir (-i) */
+static char path[PATH_MAX]; /* irc dir (-i) */
 static char msg[IRC_MSG_MAX]; /* message buf used for communication */
 
 static void
_AT_@ -46,7 +46,7 @@ usage(void) {
 }
 
 static char *
-striplower(char *s) {
+normalizechannel(char *s) {
 	char *p = NULL;
 
 	for(p = s; p && *p; p++) {
_AT_@ -60,7 +60,7 @@ striplower(char *s) {
 /* creates directories bottom-up, if necessary */
 static void
 create_dirtree(const char *dir) {
-	char tmp[_POSIX_PATH_MAX];
+	char tmp[PATH_MAX];
 	char *p = NULL;
 	struct stat st = { 0 };
 	size_t len;
_AT_@ -88,7 +88,7 @@ create_filepath(char *filepath, size_t len, char *channel, char *suffix) {
 	const char *e = "ii: path to irc directory too long\n";
 
 	if(channel && channel[0]) {
-		striplower(channel);
+		normalizechannel(channel);
 		if(snprintf(filepath, len, "%s/%s", path, channel) <= 0)
 			eprintf(e);
 		create_dirtree(filepath);
_AT_@ -100,7 +100,7 @@ create_filepath(char *filepath, size_t len, char *channel, char *suffix) {
 
 static int
 open_channel(char *name) {
-	char infile[_POSIX_PATH_MAX];
+	char infile[PATH_MAX];
 
 	create_filepath(infile, sizeof(infile), name, "in");
 	if(access(infile, F_OK) == -1)
_AT_@ -114,7 +114,7 @@ add_channel(char *cname) {
 	int fd;
 	char *name;
 
-	name = striplower(cname);
+	name = normalizechannel(cname);
 	for(c = channels; c; c = c->next)
 		if(!strcmp(name, c->name))
 			return; /* already handled */
_AT_@ -127,12 +127,9 @@ add_channel(char *cname) {
 	if(!(c = calloc(1, sizeof(Channel))))
 		eprintf("ii: cannot allocate memory:");
 
-	if(!channels)
-		channels = c;
-	else {
+	if(channels)
 		c->next = channels;
-		channels = c;
-	}
+	channels = c;
 	c->fd = fd;
 	c->name = strdup(name);
 }
_AT_@ -141,9 +138,9 @@ static void
 rm_channel(Channel *c) {
 	Channel *p;
 
-	if(channels == c)
+	if(channels == c) {
 		channels = channels->next;
-	else {
+	} else {
 		for(p = channels; p && p->next != c; p = p->next);
 		if(p->next == c)
 			p->next = c->next;
_AT_@ -192,6 +189,15 @@ tcpopen(const char *host, const char *service) {
 	return fd;
 }
 
+static int
+isnumeric(const char *s) {
+	if(!s)
+		return 0;
+	errno = 0;
+	strtol(s, NULL, 10);
+	return errno == 0;
+}
+
 static size_t
 tokenize(char **result, size_t reslen, char *str, char delim) {
 	char *p = NULL, *n = NULL;
_AT_@ -204,14 +210,15 @@ tokenize(char **result, size_t reslen, char *str, char delim) {
 	while(*n != '\0') {
 		if(i >= reslen)
 			return 0;
-		if(i > TOK_CHAN - TOK_CMD && strtol(result[0], NULL, 10) > 0)
+		if(i > TOK_CHAN - TOK_CMD && isnumeric(result[0]))
 			delim = ':'; /* workaround non-RFC compliant messages */
 		if(*n == delim) {
 			*n = '\0';
 			result[i++] = p;
 			p = ++n;
-		} else
+		} else {
 			n++;
+		}
 	}
 	if(i < reslen && p < n && p && *p)
 		result[i++] = p;
_AT_@ -220,8 +227,7 @@ tokenize(char **result, size_t reslen, char *str, char delim) {
 
 static void
 print_out(char *channel, char *buf) {
-	char outfile[_POSIX_PATH_MAX];
-	char buft[18] = "";
+	char outfile[PATH_MAX], buft[18] = "";
 	FILE *out = NULL;
 	time_t t = time(NULL);
 
_AT_@ -251,7 +257,7 @@ proc_channels_privmsg(int fd, char *channel, char *buf) {
 
 static void
 proc_channels_input(int fd, Channel *c, char *buf) {
-	char infile[_POSIX_PATH_MAX];
+	char infile[PATH_MAX];
 	char *p = NULL;
 	size_t buflen;
 
_AT_@ -309,7 +315,7 @@ proc_channels_input(int fd, Channel *c, char *buf) {
 				snprintf(msg, sizeof(msg), "PART %s :%s\r\n", c->name, &buf[3]);
 			else
 				snprintf(msg, sizeof(msg),
-				         "PART %s :ii - 500 SLOC are too much\r\n", c->name);
+				         "PART %s :leaving\r\n", c->name);
 			write(fd, msg, strnlen(msg, sizeof(msg)));
 			close(c->fd);
 			/* remove "in" file on leaving the channel */
_AT_@ -355,13 +361,15 @@ proc_server_cmd(int fd, char *buf) {
 			*p = '\0';
 			argv[TOK_USER] = ++p;
 		}
-	} else
+	} else {
 		cmd = buf;
+	}
 
 	/* remove CRLFs */
-	for(p = cmd; p && *p != '\0'; p++)
+	for(p = cmd; p && *p != '\0'; p++) {
 		if(*p == '\r' || *p == '\n')
 			*p = '\0';
+	}
 
 	if((p = strchr(cmd, ':'))) {
 		*p = '\0';
_AT_@ -452,8 +460,7 @@ handle_channels_input(int ircfd, Channel *c) {
 
 	if(read_line(c->fd, buf, sizeof(buf)) == -1) {
 		close(c->fd);
-		fd = open_channel(c->name);
-		if(fd != -1)
+		if((fd = open_channel(c->name)) != -1)
 			c->fd = fd;
 		else
 			rm_channel(c);
_AT_@ -489,7 +496,6 @@ run(int ircfd, char *host) {
 				maxfd = c->fd;
 			FD_SET(c->fd, &rd);
 		}
-
 		tv.tv_sec = 120;
 		tv.tv_usec = 0;
 		r = select(maxfd + 1, &rd, 0, 0, &tv);
_AT_@ -509,9 +515,10 @@ run(int ircfd, char *host) {
 			handle_server_output(ircfd);
 			last_response = time(NULL);
 		}
-		for(c = channels; c; c = c->next)
+		for(c = channels; c; c = c->next) {
 			if(FD_ISSET(c->fd, &rd))
 				handle_channels_input(ircfd, c);
+		}
 	}
 }
 
_AT_@ -519,7 +526,7 @@ int
 main(int argc, char *argv[]) {
 	struct passwd *spw;
 	char *key = NULL, *fullname = NULL, *host = "";
-	char prefix[_POSIX_PATH_MAX], *service = "6667";
+	char prefix[PATH_MAX], *service = "6667";
 	int ircfd;
 
 	/* use nickname and home dir of user for ii by default */
_AT_@ -529,10 +536,6 @@ main(int argc, char *argv[]) {
 	snprintf(prefix, sizeof(prefix), "%s/irc", spw->pw_dir);
 
 	ARGBEGIN {
-		case 'h':
-		case 'v':
-			usage();
-			break;
 		case 'i':
 			strlcpy(prefix, EARGF(usage()), sizeof(prefix));
 			break;
-- 
2.4.10
--Multipart=_Mon__9_May_2016_17_21_10_+0200_I.6cpFVydhq75aaE
Content-Type: text/x-diff;
 name="0041-overhaul-v2.patch"
Content-Disposition: attachment;
 filename="0041-overhaul-v2.patch"
Content-Transfer-Encoding: quoted-printable
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon May 09 2016 - 17:24:22 CEST