diff --git a/ChangeLog b/ChangeLog index c707d025..8fd64778 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,10 @@ CVS code - align() - Tweak to avoid a potential problem when strp is non-NULL but *strp is NULL. (David Benbennick) + nstricmp(), nstrnicmp() + - Add these functions, equivalent to strcasecmp() and + strncasecmp(), and convert nano to use them when strcasecmp() + and/or strncasecmp() are unavailable. (DLR) - winio.c: do_help() - Get rid of keypad_on() call for bottomwin, which should not be diff --git a/nano.h b/nano.h index 0b9b767b..b442503b 100644 --- a/nano.h +++ b/nano.h @@ -80,11 +80,11 @@ #endif #ifndef HAVE_STRCASECMP -#define strcasecmp strcmp +#define strcasecmp nstricmp #endif #ifndef HAVE_STRNCASECMP -#define strncasecmp strncmp +#define strncasecmp nstrnicmp #endif /* HP-UX 10 & 11 do not seem to support KEY_HOME and KEY_END */ diff --git a/proto.h b/proto.h index aad13e07..9ad6a42b 100644 --- a/proto.h +++ b/proto.h @@ -399,6 +399,12 @@ void align(char **strp); void null_at(char **data, size_t index); void unsunder(char *str, size_t true_len); void sunder(char *str); +#ifndef HAVE_STRCASECMP +int nstricmp(const char *s1, const char *s2); +#endif +#ifndef HAVE_STRNCASECMP +int nstrnicmp(const char *s1, const char *s2, size_t n); +#endif #ifndef NANO_SMALL const char *revstrstr(const char *haystack, const char *needle, const char *rev_start); diff --git a/utils.c b/utils.c index 8e3a6017..f18f86a3 100644 --- a/utils.c +++ b/utils.c @@ -99,6 +99,37 @@ void sunder(char *str) *str = '\0'; } +#ifndef HAVE_STRCASECMP +/* This function is equivalent to strcasecmp(). */ +int nstricmp(const char *s1, const char *s2) +{ + assert(s1 != NULL && s2 != NULL); + for (; *s1 != '\0' && *s2 != '\0'; s1++, s2++) { + if (tolower(*s1) != tolower(*s2)) + break; + } + return (tolower(*s1) - tolower(*s2)); +} +#endif + +#ifndef HAVE_STRNCASECMP +/* This function is equivalent to strncasecmp(). */ +int nstrnicmp(const char *s1, const char *s2, size_t n) +{ + assert(s1 != NULL && s2 != NULL); + for (; n > 0 && *s1 != '\0' && *s2 != '\0'; n--, s1++, s2++) { + if (tolower(*s1) != tolower(*s2)) + break; + } + if (n > 0) + return (tolower(*s1) - tolower(*s2)); + else if (n == 0) + return 0; + else if (n < 0) + return -1; +} +#endif + /* None of this is needed if we're using NANO_SMALL! */ #ifndef NANO_SMALL const char *revstrstr(const char *haystack, const char *needle,