home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / pdc / libsrc / stringlib / strncmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-07  |  776 b   |  41 lines

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