]> pd.if.org Git - zpackage/blob - zpm-vercmp.c
cleanup atree
[zpackage] / zpm-vercmp.c
1 #define _POSIX_C_SOURCE 2 
2
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #include <unistd.h>
7
8 #include "zpm.h"
9
10 /*
11  * -g greater than
12  * -l less than
13  * -e equal
14  * -L least : print least
15  * -G greatest : print greatest
16  * -q quiet: no print
17  */
18 int main(int ac, char *av[]) {
19         int cmp;
20         char *a = 0, *b = 0;
21         int print = 1;
22         int argn, opt, pass, failed = 0;
23
24         /* 0x1 = less than, 0x2 = equal, 0x4 = greater */
25         unsigned testmask = 0;
26         unsigned testcases = 0;
27
28         char *greatest = 0;
29         char *least = 0;
30
31         if (ac < 2) return 1;
32
33         while ((opt = getopt(ac, av, "qgleLG")) != -1) {
34                 switch (opt) {
35                         case 'q': print = 0; break;
36                         case 'l': testmask |= 0x1; break;
37                         case 'e': testmask |= 0x2; break;
38                         case 'g': testmask |= 0x4; break;
39                         case 'L': print = 2; break;
40                         case 'G': print = 3; break;
41                         default:
42                                   exit(EXIT_FAILURE);
43                                   break;
44                 }
45         }
46         argn = optind;
47
48         if (ac > argn) {
49                 a = av[argn++];
50                 greatest = a;
51                 least = a;
52         }
53
54         while (ac > argn) {
55                 b = av[argn++];
56
57                 cmp = zpm_vercmp(a, b) + 1;
58                 testcases |= (1<<cmp);
59                 
60                 if (testmask) {
61                         pass = (testmask & (1 << cmp));
62                         if (!pass) {
63                                 failed++;
64                         }
65                 }
66
67                 cmp = zpm_vercmp(greatest, b);
68                 if (cmp > 0) {
69                         greatest = b;
70                 }
71
72                 cmp = zpm_vercmp(least, b);
73                 if (cmp < 0) {
74                         least = b;
75                 }
76         }
77
78         switch (testcases) {
79                 case 0: /* no tests */
80                         cmp = 0; break;
81                 case 1: /* only less than */
82                         cmp = 2; break;
83                 case 2: /* only equal */
84                         cmp = 0; break;
85                 case 4: /* only greater than */
86                         cmp = 1; break;
87                 default: /* mixed */
88                         cmp = 3; break;
89         }
90
91         switch (print) {
92                 case 1:
93                         printf("%d\n", cmp == 2 ? -1 : cmp);
94                         break;
95                 case 2:
96                         if (greatest) printf("%s\n", greatest);
97                         break;
98                 case 3:
99                         if (least) printf("%s\n", least);
100                         break;
101                 default:
102                         break;
103         }
104         fflush(stdout);
105
106         if (testmask == 0) {
107                 return cmp;
108         } else {
109                 return failed ? 1 : 0;
110         }
111 }