eel

formatter for ii(1) logs
Log | Files | Refs | README

commit dc4645b5fbfed87f8a7b7fa8f6925c81aeba0737
parent b3561dcdca973303853402b7233c69b912848c99
Author: Josuah Demangeon <mail@josuah.net>
Date:   Mon, 28 May 2018 11:30:49 +0200

do not alloc: irc messages are 512 char max

Diffstat:
Meel.c | 35+++++++++++++++++------------------
1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/eel.c b/eel.c @@ -3,10 +3,11 @@ #include <time.h> #include <string.h> -#define NICK_MAX 256 -#define NICK_COL 10 -#define TIME_COL 6 -#define COLS_MAX 80 +#define RFC1459_LEN 512 +#define NICK_MAX 256 +#define NICK_COL 10 +#define TIME_COL 6 +#define COLS_MAX 80 char * print_row(char *time, char *nick, char *message) @@ -51,23 +52,21 @@ print_message(time_t time, char *nick, int same, char *message) strftime(time_str, TIME_COL, "%H:%M", localtime(&time)); while ((message = print_row(time_str, nick, message))) { time_str[0] = '\0'; - nick = ""; + nick = ""; } } int parse_message(char *str, time_t *time, char **nick, char **message) { - for (*time = atoi(str); *str != ' '; str++) { + for (*time = atoi(str); *str != ' '; str++) if (!*str) return -1; - } *str++ = '\0'; - for (*nick = str; *str != ' '; str++) { + for (*nick = str; *str != ' '; str++) if (!*str) return -1; - } *str = '\0'; *message = str + 1; @@ -78,20 +77,20 @@ parse_message(char *str, time_t *time, char **nick, char **message) int main(void) { - size_t len; time_t time; - char *nick, *message, *line, last[NICK_MAX]; + char *nick, *message, line[RFC1459_LEN + 1], last[NICK_MAX]; - len = time = 0; - nick = message = line = NULL; - last[0] = '\0'; - while (getline(&line, &len, stdin) > 0) { - if (parse_message(line, &time, &nick, &message) < 0) + time = 0; + nick = message = NULL; + memset(last, 0, sizeof(last)); + while (fgets(line, sizeof(line), stdin) != NULL) { + if (parse_message(line, &time, &nick, &message) < 0) { + fprintf(stderr, "%s\n", line); continue; + } print_message(time, nick, strcmp(nick, last) == 0, message); - strcpy(last, nick); + strncpy(last, nick, sizeof(last) - 1); } - free(line); return 0; }