home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 109.lha / PD_C / lib / strncmp.c < prev    next >
Text File  |  1986-11-20  |  1KB  |  39 lines

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