/contrib/groff/src/libs/libgroff/strcasecmp.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 66 lines · 35 code · 11 blank · 20 comment · 10 complexity · 55c9b299ebcf0cd0247fc4ebdfeb3801 MD5 · raw file

  1. /* strcasecmp.c -- case insensitive string comparator
  2. Copyright (C) 1998, 1999 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #if HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. #ifdef LENGTH_LIMIT
  18. # define STRXCASECMP_FUNCTION strncasecmp
  19. # define STRXCASECMP_DECLARE_N , size_t n
  20. # define LENGTH_LIMIT_EXPR(Expr) Expr
  21. #else
  22. # define STRXCASECMP_FUNCTION strcasecmp
  23. # define STRXCASECMP_DECLARE_N /* empty */
  24. # define LENGTH_LIMIT_EXPR(Expr) 0
  25. #endif
  26. #include <stddef.h>
  27. #include <ctype.h>
  28. #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
  29. /* Compare {{no more than N characters of }}strings S1 and S2,
  30. ignoring case, returning less than, equal to or
  31. greater than zero if S1 is lexicographically less
  32. than, equal to or greater than S2. */
  33. int
  34. STRXCASECMP_FUNCTION (const char *s1, const char *s2 STRXCASECMP_DECLARE_N)
  35. {
  36. register const unsigned char *p1 = (const unsigned char *) s1;
  37. register const unsigned char *p2 = (const unsigned char *) s2;
  38. unsigned char c1, c2;
  39. if (p1 == p2 || LENGTH_LIMIT_EXPR (n == 0))
  40. return 0;
  41. do
  42. {
  43. c1 = TOLOWER (*p1);
  44. c2 = TOLOWER (*p2);
  45. if (LENGTH_LIMIT_EXPR (--n == 0) || c1 == '\0')
  46. break;
  47. ++p1;
  48. ++p2;
  49. }
  50. while (c1 == c2);
  51. return c1 - c2;
  52. }