]> git.saurik.com Git - apt.git/blame - test/rpmver.cc
Add -n synonym for --names-only for apt-cache.
[apt.git] / test / rpmver.cc
CommitLineData
c0a00c82
AL
1#include <apt-pkg/debversion.h>
2#include <rpm/rpmio.h>
3#include <rpm/misc.h>
4#include <stdlib.h>
11e97c45
AL
5#include <ctype.h>
6
7#define xisdigit(x) isdigit(x)
8#define xisalpha(x) isalpha(x)
9#define xisalnum(x) (isdigit(x) || isalpha(x))
10
11int rpmvercmp(const char * a, const char * b)
12{
13 char oldch1, oldch2;
14 char * str1, * str2;
15 char * one, * two;
16 int rc;
17 int isnum;
18
19 /* easy comparison to see if versions are identical */
20 if (!strcmp(a, b)) return 0;
21
22 str1 = (char *)alloca(strlen(a) + 1);
23 str2 = (char *)alloca(strlen(b) + 1);
24
25 strcpy(str1, a);
26 strcpy(str2, b);
27
28 one = str1;
29 two = str2;
30
31 /* loop through each version segment of str1 and str2 and compare them */
32 while (*one && *two) {
33 while (*one && !xisalnum(*one)) one++;
34 while (*two && !xisalnum(*two)) two++;
35
36 str1 = one;
37 str2 = two;
38
39 /* grab first completely alpha or completely numeric segment */
40 /* leave one and two pointing to the start of the alpha or numeric */
41 /* segment and walk str1 and str2 to end of segment */
42 if (xisdigit(*str1)) {
43 while (*str1 && xisdigit(*str1)) str1++;
44 while (*str2 && xisdigit(*str2)) str2++;
45 isnum = 1;
46 } else {
47 while (*str1 && xisalpha(*str1)) str1++;
48 while (*str2 && xisalpha(*str2)) str2++;
49 isnum = 0;
50 }
51
52 /* save character at the end of the alpha or numeric segment */
53 /* so that they can be restored after the comparison */
54 oldch1 = *str1;
55 *str1 = '\0';
56 oldch2 = *str2;
57 *str2 = '\0';
58
59 /* take care of the case where the two version segments are */
60 /* different types: one numeric, the other alpha (i.e. empty) */
61 if (one == str1) return -1; /* arbitrary */
62 if (two == str2) return 1;
63
64 if (isnum) {
65 /* this used to be done by converting the digit segments */
66 /* to ints using atoi() - it's changed because long */
67 /* digit segments can overflow an int - this should fix that. */
68
69 /* throw away any leading zeros - it's a number, right? */
70 while (*one == '0') one++;
71 while (*two == '0') two++;
72
73 /* whichever number has more digits wins */
74 if (strlen(one) > strlen(two)) return 1;
75 if (strlen(two) > strlen(one)) return -1;
76 }
77
78 /* strcmp will return which one is greater - even if the two */
79 /* segments are alpha or if they are numeric. don't return */
80 /* if they are equal because there might be more segments to */
81 /* compare */
82 rc = strcmp(one, two);
83 if (rc) return rc;
84
85 /* restore character that was replaced by null above */
86 *str1 = oldch1;
87 one = str1;
88 *str2 = oldch2;
89 two = str2;
90 }
91
92 /* this catches the case where all numeric and alpha segments have */
93 /* compared identically but the segment sepparating characters were */
94 /* different */
95 if ((!*one) && (!*two)) return 0;
96
97 /* whichever version still has characters left over wins */
98 if (!*one) return -1; else return 1;
99}
c0a00c82
AL
100
101int main(int argc,const char *argv[])
102{
11e97c45
AL
103 printf("%i\n",strcmp(argv[1],argv[2]));
104
c0a00c82
AL
105 printf("'%s' <> '%s': ",argv[1],argv[2]);
106 printf("rpm: %i deb: %i\n",rpmvercmp(argv[1],argv[2]),
107 debVS.CmpFragment(argv[1],argv[1]+strlen(argv[1]),
108 argv[2],argv[2]+strlen(argv[2])));
109
110 printf("'%s' <> '%s': ",argv[2],argv[1]);
111 printf("rpm: %i deb: %i\n",rpmvercmp(argv[2],argv[1]),
112 debVS.CmpFragment(argv[2],argv[2]+strlen(argv[2]),
113 argv[1],argv[1]+strlen(argv[1])));
114 return 0;
115}