forked from linux-test-project/ltp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tst_kvcmp.c
191 lines (166 loc) · 3.62 KB
/
tst_kvcmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define TST_NO_DEFAULT_MAIN
#include <stdio.h>
#include <stdlib.h>
#include <tst_test.h>
enum op {
EQ,
NE,
GE,
GT,
LE,
LT,
AND,
OR,
ERR,
};
static enum op strtop(const char *op)
{
if (!strcmp(op, "-eq"))
return EQ;
if (!strcmp(op, "-ne"))
return NE;
if (!strcmp(op, "-ge"))
return GE;
if (!strcmp(op, "-gt"))
return GT;
if (!strcmp(op, "-le"))
return LE;
if (!strcmp(op, "-lt"))
return LT;
if (!strcmp(op, "-a"))
return AND;
if (!strcmp(op, "-o"))
return OR;
return ERR;
}
static void help(const char *fname)
{
printf("usage: %s -eq|-ne|-gt|-ge|-lt|-le kver [-a|-o] ...\n\n", fname);
printf("-eq kver\tReturns true if kernel version is equal\n");
printf("-ne kver\tReturns true if kernel version is not equal\n");
printf("-gt kver\tReturns true if kernel version is greater\n");
printf("-ge kver\tReturns true if kernel version is greater or equal\n");
printf("-lt kver\tReturns true if kernel version is lesser\n");
printf("-le kver\tReturns true if kernel version is lesser or equal\n");
printf("-a \t\tDoes logical and between two expressions\n");
printf("-o \t\tDoes logical or between two expressions\n\n");
printf("Kernel version format has either one or two dots: ");
printf("'2.6' or '4.8.1'\n");
}
int main(int argc, char *argv[])
{
int i = 1;
int ret = -1;
int v1, v2, v3;
enum op prev_op = ERR;
if (argc <= 1 || !strcmp(argv[1], "-h")) {
help(argv[0]);
return 0;
}
while (i < argc) {
const char *strop = argv[i++];
const char *strkver;
int res;
enum op op = strtop(strop);
switch (op) {
case EQ:
case NE:
case GE:
case GT:
case LE:
case LT:
if (ret != -1 && prev_op == ERR) {
fprintf(stderr, "Expected -a or -o\n");
return 2;
}
if (i >= argc) {
fprintf(stderr,
"Expected kernel version after '%s'\n",
strop);
return 2;
}
strkver = argv[i++];
if (tst_parse_kver(strkver, &v1, &v2, &v3)) {
fprintf(stderr,
"Invalid kernel version '%s'\n",
strkver);
return 2;
}
break;
case AND:
case OR:
if (ret == -1) {
fprintf(stderr,
"The %s must follow expression\n",
strop);
return 2;
}
prev_op = op;
continue;
break;
case ERR:
fprintf(stderr, "Invalid operation %s\n", argv[i]);
return 2;
}
res = tst_kvercmp(v1, v2, v3);
switch (op) {
case EQ:
res = (res == 0);
break;
case NE:
res = (res != 0);
break;
case GE:
res = (res >= 0);
break;
case GT:
res = (res > 0);
break;
case LE:
res = (res <= 0);
break;
case LT:
res = (res < 0);
break;
default:
break;
}
switch (prev_op) {
case ERR:
ret = res;
break;
case AND:
ret = ret && res;
prev_op = ERR;
break;
case OR:
ret = ret || res;
prev_op = ERR;
break;
default:
break;
}
}
if (prev_op != ERR) {
fprintf(stderr, "Useless -a or -o at the end\n");
return 2;
}
return !ret;
}