home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / pdksh-4.9-src.tgz / tar.out / contrib / pdksh / std / stdc / strncmp.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  833b  |  42 lines

  1. #include <string.h>
  2. /* $Id: strncmp.c,v 1.3 93/05/05 21:18:47 sjg Exp $ */
  3.  
  4. /*
  5.  * strncmp - compare at most n characters of string s1 to s2
  6.  */
  7.  
  8. int                /* <0 for <, 0 for ==, >0 for > */
  9. strncmp(s1, s2, n)
  10. const char *s1;
  11. const char *s2;
  12. size_t n;
  13. {
  14.     register const char *scan1;
  15.     register const char *scan2;
  16.     register size_t count;
  17.  
  18.     scan1 = s1;
  19.     scan2 = s2;
  20.     count = n;
  21.     while (--count >= 0 && *scan1 != '\0' && *scan1 == *scan2) {
  22.         scan1++;
  23.         scan2++;
  24.     }
  25.     if (count < 0)
  26.         return(0);
  27.  
  28.     /*
  29.      * The following case analysis is necessary so that characters
  30.      * which look negative collate low against normal characters but
  31.      * high against the end-of-string NUL.
  32.      */
  33.     if (*scan1 == '\0' && *scan2 == '\0')
  34.         return(0);
  35.     else if (*scan1 == '\0')
  36.         return(-1);
  37.     else if (*scan2 == '\0')
  38.         return(1);
  39.     else
  40.         return(*scan1 - *scan2);
  41. }
  42.