strnicmp.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
  3. ** Distributed under the terms of the NewOS License.
  4. */
  5. /*
  6. * Copyright (c) 2008 Travis Geiselbrecht
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files
  10. * (the "Software"), to deal in the Software without restriction,
  11. * including without limitation the rights to use, copy, modify, merge,
  12. * publish, distribute, sublicense, and/or sell copies of the Software,
  13. * and to permit persons to whom the Software is furnished to do so,
  14. * subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <sys/types.h>
  30. int
  31. strnicmp(char const *s1, char const *s2, size_t len)
  32. {
  33. unsigned char c1 = '\0';
  34. unsigned char c2 = '\0';
  35. if(len > 0) {
  36. do {
  37. c1 = *s1; c2 = *s2;
  38. s1++; s2++;
  39. if(!c1)
  40. break;
  41. if(!c2)
  42. break;
  43. if(c1 == c2)
  44. continue;
  45. c1 = tolower(c1);
  46. c2 = tolower(c2);
  47. if (c1 != c2)
  48. break;
  49. } while(--len);
  50. }
  51. return (int)c1 - (int)c2;
  52. }
  53. #pragma weak strncasecmp=strnicmp