@@ -37,4 +37,14 @@ static inline int isspace(int c)
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
}
+static inline int toupper(int c)
+{
+ return islower(c) ? c - 'a' + 'A' : c;
+}
+
+static inline int tolower(int c)
+{
+ return isupper(c) ? c - 'A' + 'a' : c;
+}
+
#endif /* _CTYPE_H_ */
@@ -54,11 +54,25 @@ int strncmp(const char *a, const char *b, size_t n)
return 0;
}
+int strncasecmp(const char *a, const char *b, size_t n)
+{
+ for (; n--; ++a, ++b)
+ if (tolower(*a) != tolower(*b) || *a == '\0')
+ return tolower(*a) - tolower(*b);
+
+ return 0;
+}
+
int strcmp(const char *a, const char *b)
{
return strncmp(a, b, SIZE_MAX);
}
+int strcasecmp(const char *a, const char *b)
+{
+ return strncasecmp(a, b, SIZE_MAX);
+}
+
char *strchr(const char *s, int c)
{
while (*s != (char)c)
@@ -15,6 +15,8 @@ extern char *strcat(char *dest, const char *src);
extern char *strcpy(char *dest, const char *src);
extern int strcmp(const char *a, const char *b);
extern int strncmp(const char *a, const char *b, size_t n);
+int strcasecmp(const char *a, const char *b);
+int strncasecmp(const char *a, const char *b, size_t n);
extern char *strchr(const char *s, int c);
extern char *strrchr(const char *s, int c);
extern char *strchrnul(const char *s, int c);
We'll soon want a case insensitive string comparison. Add toupper() and tolower() too (the latter gets used by the new string functions). Signed-off-by: Andrew Jones <andrew.jones@linux.dev> --- lib/ctype.h | 10 ++++++++++ lib/string.c | 14 ++++++++++++++ lib/string.h | 2 ++ 3 files changed, 26 insertions(+)