1: # include <useful.h> 2: # include <sccs.h> 3: 4: SCCSID(@(#)scompare.c 8.3 1/17/85) 5: 6: /* 7: ** STRING COMPARE 8: ** 9: ** The strings 'a_ptr' and 'b_ptr' are compared. Blanks are 10: ** ignored. The first string may be no longer than 'a_len' 11: ** bytes, and the second string may be no longer than 'b_len' 12: ** bytes. If either length is zero, it is taken to be very 13: ** long. A null byte also terminates the scan. 14: ** 15: ** Compares are based on the ascii ordering. 16: ** 17: ** Shorter strings are less than longer strings. 18: ** 19: ** Return value is positive one for a > b, minus one for a < b, 20: ** and zero for a == b. 21: ** 22: ** Examples: 23: ** "abc" > "ab" 24: ** " a bc " == "ab c" 25: ** "abc" < "abd" 26: */ 27: 28: scompare(a_ptr, a_len, b_ptr, b_len) 29: char *a_ptr; 30: int a_len; 31: char *b_ptr; 32: int b_len; 33: { 34: char *ap; 35: char *bp; 36: register char a; 37: char b; 38: register int al; 39: register int bl; 40: 41: ap = a_ptr; 42: bp = b_ptr; 43: al = a_len; 44: if (al == 0) 45: al = MAXI4; 46: bl = b_len; 47: if (bl == 0) 48: bl = MAXI4; 49: 50: while (1) 51: { 52: 53: /* supress blanks in both strings */ 54: while ((a = *ap) == ' ' && al > 0) 55: { 56: al--; 57: ap++; 58: } 59: if (al == 0) 60: a = 0; 61: while (*bp == ' ' && bl > 0) 62: { 63: bl--; 64: bp++; 65: } 66: if (bl == 0) 67: b = 0; 68: else 69: b = *bp; 70: 71: /* do inequality tests */ 72: if (a < b) 73: return (-1); 74: if (a > b) 75: return (1); 76: if (a == 0) 77: return (0); 78: 79: /* go on to the next character */ 80: ap++; 81: al--; 82: bp++; 83: bl--; 84: } 85: }