From efbb7fcd1fc143a046cfa4b44dbb527b248e110b Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 7 Jan 2014 00:25:38 +0000 Subject: [PATCH 1/1] Remove chop(). In curses_log() we currently we call chop(), which calls strlen() to find the last character of the given string. This is unnecessary since xvasprintf() returns this information anyway, we just have to remember it. With this change the last user of chop() is gone, so this patch removes chop() from string.c. --- gui.c | 6 ++++-- string.c | 18 ------------------ string.h | 1 - 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/gui.c b/gui.c index a6bc54a7..96132589 100644 --- a/gui.c +++ b/gui.c @@ -519,6 +519,7 @@ static __printf_2_3 void curses_log(int ll, const char *fmt,...) int color; char *msg; va_list ap; + unsigned bytes; if (ll < loglevel || !curses_active) return; @@ -532,9 +533,10 @@ static __printf_2_3 void curses_log(int ll, const char *fmt,...) color = COLOR_ERRMSG; } va_start(ap, fmt); - xvasprintf(&msg, fmt, ap); + bytes = xvasprintf(&msg, fmt, ap); va_end(ap); - chop(msg); + if (bytes > 0 && msg[bytes - 1] == '\n') + msg[bytes - 1] = '\0'; /* cut trailing newline */ rb_add_entry(color, msg); wrefresh(bot.win); } diff --git a/string.c b/string.c index 8cc7d5d7..10b6073f 100644 --- a/string.c +++ b/string.c @@ -298,24 +298,6 @@ __must_check char *para_basename(const char *name) return ret; } -/** - * Cut trailing newline. - * - * \param buf The string to be chopped. - * - * Replace the last character in \p buf by zero if it is equal to - * the newline character. - */ -void chop(char *buf) -{ - int n = strlen(buf); - - if (!n) - return; - if (buf[n - 1] == '\n') - buf[n - 1] = '\0'; -} - /** * Get the logname of the current user. * diff --git a/string.h b/string.h index b13a0d86..11ba1283 100644 --- a/string.h +++ b/string.h @@ -84,7 +84,6 @@ __must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...); __must_check __malloc char *para_strcat(char *a, const char *b); __must_check __malloc char *para_dirname(const char *name); __must_check char *para_basename(const char *name); -void chop(char *buf); __must_check __malloc char *para_logname(void); __must_check __malloc char *para_homedir(void); __malloc char *para_hostname(void); -- 2.39.2