strcmp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * This file is shared between libc and the kernel, so don't put anything
  31. * in here that won't work in both contexts.
  32. */
  33. #ifdef _KERNEL
  34. #include <types.h>
  35. #include <lib.h>
  36. #else
  37. #include <string.h>
  38. #endif
  39. /*
  40. * Standard C string function: compare two strings and return their
  41. * sort order.
  42. */
  43. int
  44. strcmp(const char *a, const char *b)
  45. {
  46. size_t i;
  47. /*
  48. * Walk down both strings until either they're different
  49. * or we hit the end of A.
  50. *
  51. * If A and B strings are not the same length, when the
  52. * shorter one ends, the two will be different, and we'll
  53. * stop before running off the end of either.
  54. *
  55. * If they *are* the same length, it's sufficient to check
  56. * that we haven't run off the end of A, because that's the
  57. * same as checking to make sure we haven't run off the end of
  58. * B.
  59. */
  60. for (i=0; a[i]!=0 && a[i]==b[i]; i++) {
  61. /* nothing */
  62. }
  63. /*
  64. * If A is greater than B, return 1. If A is less than B,
  65. * return -1. If they're the same, return 0. Since we have
  66. * stopped at the first character of difference (or the end of
  67. * both strings) checking the character under I accomplishes
  68. * this.
  69. *
  70. * Note that strcmp does not handle accented characters,
  71. * internationalization, or locale sort order; strcoll() does
  72. * that.
  73. *
  74. * The rules say we compare order in terms of *unsigned* char.
  75. */
  76. if ((unsigned char)a[i] > (unsigned char)b[i]) {
  77. return 1;
  78. }
  79. else if (a[i] == b[i]) {
  80. return 0;
  81. }
  82. return -1;
  83. }