]>
Commit | Line | Data |
---|---|---|
3836cd2d KZ |
1 | /* |
2 | * No copyright is claimed. This code is in the public domain; do with | |
3 | * it what you wish. | |
4 | */ | |
5d2c98e1 SK |
5 | #include <stdio.h> |
6 | #include <sys/utsname.h> | |
7 | ||
d404065a | 8 | #include "c.h" |
5d2c98e1 SK |
9 | #include "linux_version.h" |
10 | ||
85d5acb8 | 11 | int get_linux_version (void) |
5d2c98e1 SK |
12 | { |
13 | static int kver = -1; | |
14 | struct utsname uts; | |
d404065a | 15 | int x = 0, y = 0, z = 0; |
85d5acb8 | 16 | int n; |
5d2c98e1 SK |
17 | |
18 | if (kver != -1) | |
19 | return kver; | |
d404065a | 20 | if (uname(&uts)) |
85d5acb8 | 21 | return kver = 0; |
5d2c98e1 | 22 | |
d404065a | 23 | n = sscanf(uts.release, "%d.%d.%d", &x, &y, &z); |
85d5acb8 KZ |
24 | if (n < 1 || n > 3) |
25 | return kver = 0; | |
26 | ||
6b95593d | 27 | return kver = KERNEL_VERSION(x, y, z); |
5d2c98e1 | 28 | } |
5b9403a6 | 29 | |
e8f7acb0 | 30 | #ifdef TEST_PROGRAM_LINUXVERSION |
5b9403a6 | 31 | # include <stdlib.h> |
e33da967 KZ |
32 | int main(int argc, char *argv[]) |
33 | { | |
34 | int rc = EXIT_FAILURE; | |
5b9403a6 | 35 | |
e33da967 KZ |
36 | if (argc == 1) { |
37 | printf("Linux version: %d\n", get_linux_version()); | |
38 | rc = EXIT_SUCCESS; | |
5b9403a6 | 39 | |
e33da967 KZ |
40 | } else if (argc == 5) { |
41 | const char *oper = argv[1]; | |
5b9403a6 | 42 | |
e33da967 KZ |
43 | int x = atoi(argv[2]), |
44 | y = atoi(argv[3]), | |
45 | z = atoi(argv[4]); | |
46 | int kver = get_linux_version(); | |
47 | int uver = KERNEL_VERSION(x, y, z); | |
5b9403a6 | 48 | |
e33da967 KZ |
49 | if (strcmp(oper, "==") == 0) |
50 | rc = kver == uver; | |
51 | else if (strcmp(oper, "<=") == 0) | |
52 | rc = kver <= uver; | |
53 | else if (strcmp(oper, ">=") == 0) | |
54 | rc = kver >= uver; | |
55 | else | |
56 | errx(EXIT_FAILURE, "unsupported operator"); | |
5b9403a6 | 57 | |
e33da967 KZ |
58 | if (rc) |
59 | printf("match\n"); | |
60 | else | |
61 | printf("not-match [%d %s %d, x.y.z: %d.%d.%d]\n", | |
62 | kver, oper, uver, x, y, z); | |
5b9403a6 | 63 | |
e33da967 | 64 | rc = rc ? EXIT_SUCCESS : EXIT_FAILURE; |
5b9403a6 | 65 | |
e33da967 KZ |
66 | } else |
67 | fprintf(stderr, "Usage:\n" | |
68 | " %s [<oper> <x> <y> <z>]\n" | |
69 | "supported operators:\n" | |
70 | " ==, <=, >=\n", | |
71 | program_invocation_short_name); | |
5b9403a6 | 72 | |
e33da967 KZ |
73 | return rc; |
74 | } | |
5b9403a6 | 75 | #endif |