[PATCH 20/65] stylistic changes

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Thu, 17 Apr 2014 17:02:41 +0200

- use ircfd name for "global" irc fd everywhere.
- use '\0' for null terminate char.
- add comments: fullname for commands.

Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
---
 ii.c | 111 ++++++++++++++++++++++++++++++++-----------------------------------
 1 file changed, 53 insertions(+), 58 deletions(-)
diff --git a/ii.c b/ii.c
index c193940..8814be7 100644
--- a/ii.c
+++ b/ii.c
_AT_@ -35,9 +35,9 @@ 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 msg[IRC_MSG_MAX] = ""; /* message buf used for communication */
+static char nick[32]; /* might change while running */
+static char path[_POSIX_PATH_MAX]; /* irc dir (-i) */
+static char msg[IRC_MSG_MAX]; /* message buf used for communication */
 
 static void
 usage(void) {
_AT_@ -141,7 +141,7 @@ rm_channel(Channel *c) {
 	Channel *p;
 
 	if(channels == c)
-		channels = channels->next; /* first item */
+		channels = channels->next;
 	else {
 		for(p = channels; p && p->next != c; p = p->next);
 		if(p->next == c)
_AT_@ -153,7 +153,7 @@ rm_channel(Channel *c) {
 }
 
 static void
-login(int fd, char *host, char *key, char *fullname) {
+login(int ircfd, char *host, char *key, char *fullname) {
 	if(key)
 		snprintf(msg, sizeof(msg),
 		         "PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n", key,
_AT_@ -161,7 +161,7 @@ login(int fd, char *host, char *key, char *fullname) {
 	else
 		snprintf(msg, sizeof(msg), "NICK %s\r\nUSER %s localhost %s :%s\r\n",
 		         nick, nick, host, fullname);
-	write(fd, msg, strnlen(msg, sizeof(msg)));
+	write(ircfd, msg, strnlen(msg, sizeof(msg)));
 }
 
 static int
_AT_@ -179,7 +179,6 @@ tcpopen(const char *host, unsigned short port) {
 	sin.sin_port = htons(port);
 	if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
 		eprintf("cannot create socket:");
-
 	if(connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0)
 		eprintf("cannot connect to host:");
 
_AT_@ -195,13 +194,13 @@ tokenize(char **result, size_t reslen, char *str, char delim) {
 		return 0;
 	for(n = str; *n == ' '; n++);
 	p = n;
-	while(*n != 0) {
+	while(*n != '\0') {
 		if(i >= reslen)
 			return 0;
 		if(i > TOK_CHAN - TOK_CMD && strtol(result[0], NULL, 10) > 0)
 			delim = ':'; /* workaround non-RFC compliant messages */
 		if(*n == delim) {
-			*n = 0;
+			*n = '\0';
 			result[i++] = p;
 			p = ++n;
 		} else
_AT_@ -214,16 +213,14 @@ tokenize(char **result, size_t reslen, char *str, char delim) {
 
 static void
 print_out(char *channel, char *buf) {
-	char outfile[_POSIX_PATH_MAX] = "";
+	char outfile[_POSIX_PATH_MAX];
 	char buft[18] = "";
 	FILE *out = NULL;
 	time_t t = time(NULL);
 
-	/* starts with "-!- " channel */
+	/* starts with "-!- " #channel */
 	if(channel && !strncmp(buf, "-!- ", 4) &&
-	   !strncmp(&buf[4], channel, strlen(channel)))
-	{
-		printf("channel set to empty string\n");
+	   !strncmp(&buf[4], channel, strlen(channel))) {
 		channel = "";
 	}
 	create_filepath(outfile, sizeof(outfile), channel, "out");
_AT_@ -247,11 +244,11 @@ 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[_POSIX_PATH_MAX];
 	char *p = NULL;
-	size_t buflen = 0;
+	size_t buflen;
 
-	if(buf[0] != '/' && buf[0] != 0) {
+	if(buf[0] != '/' && buf[0] != '\0') {
 		proc_channels_privmsg(fd, c->name, buf);
 		return;
 	}
_AT_@ -259,10 +256,9 @@ proc_channels_input(int fd, Channel *c, char *buf) {
 	if(buf[2] == ' ' || buf[2] == '\0') {
 		buflen = strlen(buf);
 		switch(buf[1]) {
-		case 'j':
-			p = strchr(&buf[3], ' ');
-			if(p)
-				*p = 0;
+		case 'j': /* join */
+			if((p = strchr(&buf[3], ' '))) /* password parameter */
+				*p = '\0';
 			if((buf[3] == '#') || (buf[3] == '&') || (buf[3] == '+') ||
 				(buf[3] == '!'))
 			{
_AT_@ -279,30 +275,30 @@ proc_channels_input(int fd, Channel *c, char *buf) {
 				return;
 			}
 			break;
-		case 't':
+		case 't': /* topic */
 			if(buflen >= 3)
 				snprintf(msg, sizeof(msg), "TOPIC %s :%s\r\n", c->name, &buf[3]);
 			break;
-		case 'a':
+		case 'a': /* away */
 			if(buflen >= 3) {
 				snprintf(msg, sizeof(msg), "-!- %s is away \"%s\"", nick, &buf[3]);
 				print_out(c->name, msg);
 			}
-			if(buflen < 3)
-				snprintf(msg, sizeof(msg), "AWAY\r\n");
-			else
+			if(buflen >= 3)
 				snprintf(msg, sizeof(msg), "AWAY :%s\r\n", &buf[3]);
+			else
+				snprintf(msg, sizeof(msg), "AWAY\r\n");
 			break;
-		case 'n':
+		case 'n': /* change nick */
 			if(buflen >= 3) {
 				strlcpy(nick, &buf[3], sizeof(nick));
 				snprintf(msg, sizeof(msg), "NICK %s\r\n", &buf[3]);
 			}
 			break;
-		case 'l':
-			if(c->name[0] == 0)
+		case 'l': /* leave */
+			if(c->name[0] == '\0')
 				return;
-			if(buf[2] == ' ' && buflen >= 3)
+			if(buflen >= 3)
 				snprintf(msg, sizeof(msg), "PART %s :%s\r\n", c->name, &buf[3]);
 			else
 				snprintf(msg, sizeof(msg),
_AT_@ -316,12 +312,12 @@ proc_channels_input(int fd, Channel *c, char *buf) {
 			rm_channel(c);
 			return;
 			break;
-		default:
+		default: /* raw IRC command */
 			snprintf(msg, sizeof(msg), "%s\r\n", &buf[1]);
 			break;
 		}
 	}
-	else
+	else /* raw IRC command */
 		snprintf(msg, sizeof(msg), "%s\r\n", &buf[1]);
 
 	if(msg[0] != '\0')
_AT_@ -343,24 +339,24 @@ proc_server_cmd(int fd, char *buf) {
 	if(buf[0] == ':') {
 		if (!(p = strchr(buf, ' ')))
 			return;
-		*p = 0;
+		*p = '\0';
 		for(++p; *p == ' '; p++);
 		cmd = p;
 		argv[TOK_NICKSRV] = &buf[1];
 		if((p = strchr(buf, '!'))) {
-			*p = 0;
+			*p = '\0';
 			argv[TOK_USER] = ++p;
 		}
 	} 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;
+			*p = '\0';
 
 	if((p = strchr(cmd, ':'))) {
-		*p = 0;
+		*p = '\0';
 		argv[TOK_TEXT] = ++p;
 	}
 
_AT_@ -377,7 +373,7 @@ proc_server_cmd(int fd, char *buf) {
 		snprintf(msg, sizeof(msg), "%s%s",
 				argv[TOK_ARG] ? argv[TOK_ARG] : "",
 				argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
-		print_out(0, msg);
+		print_out(NULL, msg);
 		return;
 	} else if(!strncmp("ERROR", argv[TOK_CMD], 6))
 		snprintf(msg, sizeof(msg), "-!- error %s",
_AT_@ -427,21 +423,20 @@ proc_server_cmd(int fd, char *buf) {
 static int
 read_line(int fd, char *buf, size_t bufsiz) {
 	size_t i = 0;
-	char c = 0;
+	char c = '\0';
 
-	for(i = 0; c != '\n' && i < bufsiz; i++) {
+	do {
 		if(read(fd, &c, sizeof(char)) != sizeof(char))
 			return -1;
-		buf[i] = c;
-	}
-	if(i > 0 && i < bufsiz)
-		buf[i - 1] = 0; /* eliminates '\n' */
+		buf[i++] = c;
+	} while(c != '\n' && i < bufsiz);
+	buf[i - 1] = '\0'; /* eliminates '\n' */
 	return 0;
 }
 
 static void
-handle_channels_input(int mainfd, Channel *c) {
-	char buf[BUFSIZ] = "";
+handle_channels_input(int ircfd, Channel *c) {
+	char buf[BUFSIZ];
 	int fd;
 
 	if(read_line(c->fd, buf, sizeof(buf)) == -1) {
_AT_@ -453,20 +448,20 @@ handle_channels_input(int mainfd, Channel *c) {
 			rm_channel(c);
 		return;
 	}
-	proc_channels_input(mainfd, c, buf);
+	proc_channels_input(ircfd, c, buf);
 }
 
 static void
-handle_server_output(int fd) {
-	char buf[BUFSIZ] = "";
+handle_server_output(int ircfd) {
+	char buf[BUFSIZ];
 
-	if(read_line(fd, buf, sizeof(buf)) == -1)
+	if(read_line(ircfd, buf, sizeof(buf)) == -1)
 		eprintf("remote host closed connection:");
-	proc_server_cmd(fd, buf);
+	proc_server_cmd(ircfd, buf);
 }
 
 static void
-run(int fd, char *host) {
+run(int ircfd, char *host) {
 	Channel *c;
 	int r, maxfd;
 	fd_set rd;
_AT_@ -476,8 +471,8 @@ run(int fd, char *host) {
 	snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host);
 	for(;;) {
 		FD_ZERO(&rd);
-		maxfd = fd;
-		FD_SET(fd, &rd);
+		maxfd = ircfd;
+		FD_SET(ircfd, &rd);
 		for(c = channels; c; c = c->next) {
 			if(maxfd < c->fd)
 				maxfd = c->fd;
_AT_@ -496,16 +491,16 @@ run(int fd, char *host) {
 				print_out(NULL, "-!- ii shutting down: ping timeout");
 				exit(EXIT_FAILURE);
 			}
-			write(fd, ping_msg, strnlen(ping_msg, sizeof(ping_msg)));
+			write(ircfd, ping_msg, strnlen(ping_msg, sizeof(ping_msg)));
 			continue;
 		}
-		if(FD_ISSET(fd, &rd)) {
-			handle_server_output(fd);
+		if(FD_ISSET(ircfd, &rd)) {
+			handle_server_output(ircfd);
 			last_response = time(NULL);
 		}
 		for(c = channels; c; c = c->next)
 			if(FD_ISSET(c->fd, &rd))
-				handle_channels_input(fd, c);
+				handle_channels_input(ircfd, c);
 	}
 }
 
-- 
2.4.10
--Multipart=_Mon__9_May_2016_17_21_10_+0200_I.6cpFVydhq75aaE
Content-Type: text/x-diff;
 name="0021-don-t-exit-if-a-channel-can-t-be-created.patch"
Content-Disposition: attachment;
 filename="0021-don-t-exit-if-a-channel-can-t-be-created.patch"
Content-Transfer-Encoding: 7bit
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