From 8c7d9334ddb44cb027ddb4527f2eb25deefd2e86 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Fri, 17 Nov 2017 16:23:39 +1100 Subject: [PATCH] Replace uses of sindex() with constant accept strings with strpbrk() / strchr() sindex(), where neither argument is NULL and the accept string does not start with ^, is exactly equivalent to the standard function strpbrk(). Further, strpbrk() where the accept string is only one character long, is exactly equivalent to strchr(). --- source/commands.c | 16 ++++++---------- source/history.c | 4 ++-- source/ignore.c | 8 ++++---- source/ircaux.c | 28 ++++++++++++++-------------- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/source/commands.c b/source/commands.c index 30d04c3..4931bc3 100644 --- a/source/commands.c +++ b/source/commands.c @@ -4352,8 +4352,7 @@ void BX_parse_line (const char *name, char *org_line, const char *args, int hist { char *line = NULL, *stuff, - *s, - *t; + *s; int args_flag = 0, die = 0; @@ -4416,13 +4415,10 @@ void BX_parse_line (const char *name, char *org_line, const char *args, int hist { while ((s = line)) { - if ((t = sindex(line, "\r\n"))) - { - *t++ = '\0'; - line = t; - } - else - line = NULL; + line = strpbrk(line, "\r\n"); + if (line) + *line++ = '\0'; + parse_command(s, hist_flag, (char *)args); if ((will_catch_break_exceptions && break_exception) || @@ -4945,7 +4941,7 @@ BUILT_IN_COMMAND(BX_load) char *optr = start; /* Skip slashed brackets */ - while ((ptr = sindex(optr, "{};/")) && + while ((ptr = strpbrk(optr, "{};/")) && ptr != optr && ptr[-1] == '\\') optr = ptr+1; diff --git a/source/history.c b/source/history.c index ea267b1..615f23a 100644 --- a/source/history.c +++ b/source/history.c @@ -194,8 +194,8 @@ void add_to_history(char *line) { while (line && *line) { - if ((ptr = sindex(line, "\n\r")) != NULL) - *(ptr++) = '\0'; + if ((ptr = strpbrk(line, "\r\n")) != NULL) + *ptr++ = '\0'; add_to_history_list(hist_count, line); last_dir = PREV; hist_count++; diff --git a/source/ignore.c b/source/ignore.c index 6998d0b..793f376 100644 --- a/source/ignore.c +++ b/source/ignore.c @@ -832,11 +832,11 @@ static char *cut_n_fix_glob ( char *nickuserhost ) /* doh! */ user = userhost; - if ((host = sindex(userhost, "@"))) + if ((host = strchr(userhost, '@'))) /* USER IS CORRECT HERE */ *host++ = 0; - else if (sindex(userhost, ".")) + else if (strchr(userhost, '.')) { user = "*"; host = userhost; @@ -850,14 +850,14 @@ static char *cut_n_fix_glob ( char *nickuserhost ) else { user = copy; - if ((host = sindex(user, "@"))) + if ((host = strchr(user, '@'))) { nick = "*"; *host++ = 0; } else { - if (sindex(user, ".")) + if (strchr(user, '.')) { nick = "*"; host = user; diff --git a/source/ircaux.c b/source/ircaux.c index b65d9b5..5203f3a 100644 --- a/source/ircaux.c +++ b/source/ircaux.c @@ -460,25 +460,28 @@ extern int word_scount (char *str) } #endif -char *BX_next_arg (char *str, char **new_ptr) +char *BX_next_arg(char *str, char **new_ptr) { - char *ptr; + char *ptr; - /* added by Sheik (kilau@prairie.nodak.edu) -- sanity */ - if (!str || !*str) + if (!str) return NULL; if ((ptr = sindex(str, "^ ")) != NULL) { - if ((str = sindex(ptr, space)) != NULL) - *str++ = (char) 0; + if ((str = strchr(ptr, ' ')) != NULL) + *str++ = 0; else str = empty_string; } else + { str = empty_string; + } + if (new_ptr) *new_ptr = str; + return ptr; } @@ -616,7 +619,7 @@ char *safe_new_next_arg (char *str, char **new_ptr) if (*ptr == '"') { start = ++ptr; - while ((str = sindex(ptr, "\"\\")) != NULL) + while ((str = strpbrk(ptr, "\"\\")) != NULL) { switch (*str) { @@ -637,7 +640,7 @@ char *safe_new_next_arg (char *str, char **new_ptr) } else { - if ((str = sindex(ptr, " \t")) != NULL) + if ((str = strpbrk(ptr, " \t")) != NULL) *str++ = '\0'; else str = empty_string; @@ -667,14 +670,11 @@ char *BX_new_new_next_arg (char *str, char **new_ptr, char *type) { if ((*ptr == '"') || (*ptr == '\'')) { - char blah[3]; - blah[0] = *ptr; - blah[1] = '\\'; - blah[2] = '\0'; + char accept[] = { *ptr, '\\', 0 }; *type = *ptr; start = ++ptr; - while ((str = sindex(ptr, blah)) != NULL) + while ((str = strpbrk(ptr, accept)) != NULL) { switch (*str) { @@ -697,7 +697,7 @@ char *BX_new_new_next_arg (char *str, char **new_ptr, char *type) else { *type = '\"'; - if ((str = sindex(ptr, " \t")) != NULL) + if ((str = strpbrk(ptr, " \t")) != NULL) *str++ = 0; else str = empty_string;