home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / strncmp.c < prev    next >
C/C++ Source or Header  |  1992-09-17  |  825b  |  45 lines

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