Back to index

glibc  2.9
Functions
strncmp.c File Reference
#include <string.h>
#include <memcopy.h>

Go to the source code of this file.

Functions

int strncmp (char *s1, const char *s2, size_t n) const

Function Documentation

int strncmp ( char *  s1,
const char *  s2,
size_t  n 
) const

Definition at line 29 of file strncmp.c.

{
  unsigned reg_char c1 = '\0';
  unsigned reg_char c2 = '\0';

  if (n >= 4)
    {
      size_t n4 = n >> 2;
      do
       {
         c1 = (unsigned char) *s1++;
         c2 = (unsigned char) *s2++;
         if (c1 == '\0' || c1 != c2)
           return c1 - c2;
         c1 = (unsigned char) *s1++;
         c2 = (unsigned char) *s2++;
         if (c1 == '\0' || c1 != c2)
           return c1 - c2;
         c1 = (unsigned char) *s1++;
         c2 = (unsigned char) *s2++;
         if (c1 == '\0' || c1 != c2)
           return c1 - c2;
         c1 = (unsigned char) *s1++;
         c2 = (unsigned char) *s2++;
         if (c1 == '\0' || c1 != c2)
           return c1 - c2;
       } while (--n4 > 0);
      n &= 3;
    }

  while (n > 0)
    {
      c1 = (unsigned char) *s1++;
      c2 = (unsigned char) *s2++;
      if (c1 == '\0' || c1 != c2)
       return c1 - c2;
      n--;
    }

  return c1 - c2;
}