added vercmp()
authorewt <devnull@localhost>
Sun, 25 Feb 1996 22:10:25 +0000 (22:10 +0000)
committerewt <devnull@localhost>
Sun, 25 Feb 1996 22:10:25 +0000 (22:10 +0000)
CVS patchset: 419
CVS date: 1996/02/25 22:10:25

lib/misc.c
lib/misc.h

index f4472d6..6a0910f 100644 (file)
@@ -1,4 +1,5 @@
 #include <errno.h>
+#include <ctype.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/utsname.h>
@@ -117,3 +118,70 @@ static void init_arch_os(void)
        /* XXX unknown os - how should we handle this? */
     }
 }
+
+int vercmp(char * one, char * two) {
+    int num1, num2;
+    char oldch1, oldch2;
+    char * str1, * str2;
+    int rc;
+    int isnum;
+    
+    if (!strcmp(one, two)) return 0;
+
+    str1 = alloca(strlen(one) + 1);
+    str2 = alloca(strlen(two) + 1);
+
+    strcpy(str1, one);
+    strcpy(str2, two);
+
+    one = str1;
+    two = str2;
+
+    while (*one && *two) {
+       while (*one && !isalnum(*one)) one++;
+       while (*two && !isalnum(*two)) two++;
+
+       str1 = one;
+       str2 = two;
+
+       if (isdigit(*str1)) {
+           while (*str1 && isdigit(*str1)) str1++;
+           while (*str2 && isdigit(*str2)) str2++;
+           isnum = 1;
+       } else {
+           while (*str1 && isalpha(*str1)) str1++;
+           while (*str2 && isalpha(*str2)) str2++;
+           isnum = 0;
+       }
+               
+       oldch1 = *str1;
+       *str1 = '\0';
+       oldch2 = *str2;
+       *str2 = '\0';
+
+       if (one == str1) return -1;     /* arbitrary */
+       if (two == str2) return -1;
+
+       if (isnum) {
+           num1 = atoi(one);
+           num2 = atoi(two);
+
+           if (num1 < num2) 
+               return -1;
+           else if (num1 > num2)
+               return 1;
+       } else {
+           rc = strcmp(one, two);
+           if (rc) return rc;
+       }
+       
+       *str1 = oldch1;
+       one = str1;
+       *str2 = oldch2;
+       two = str2;
+    }
+
+    if ((!*one) && (!*two)) return 0;
+
+    if (!*one) return 1; else return -1;
+}
index 341dd8f..903f886 100644 (file)
@@ -11,4 +11,6 @@ int getArchNum(void);
 char *getOsName(void);
 char *getArchName(void);
 
+int vercmp(char * one, char * two);
+
 #endif