• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* strncmp -- compare two strings, stop after n bytes.
2    This function is in the public domain.  */
3 
4 /*
5 
6 @deftypefn Supplemental int strncmp (const char *@var{s1}, @
7   const char *@var{s2}, size_t @var{n})
8 
9 Compares the first @var{n} bytes of two strings, returning a value as
10 @code{strcmp}.
11 
12 @end deftypefn
13 
14 */
15 
16 #include <ansidecl.h>
17 #include <stddef.h>
18 
19 int
strncmp(const char * s1,const char * s2,register size_t n)20 strncmp(const char *s1, const char *s2, register size_t n)
21 {
22   register unsigned char u1, u2;
23 
24   while (n-- > 0)
25     {
26       u1 = (unsigned char) *s1++;
27       u2 = (unsigned char) *s2++;
28       if (u1 != u2)
29 	return u1 - u2;
30       if (u1 == '\0')
31 	return 0;
32     }
33   return 0;
34 }
35