]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/linux_version.c
Merge branch 'fixes' of https://github.com/rudimeier/util-linux
[thirdparty/util-linux.git] / lib / linux_version.c
CommitLineData
5d2c98e1
SK
1#include <stdio.h>
2#include <sys/utsname.h>
3
d404065a 4#include "c.h"
5d2c98e1
SK
5#include "linux_version.h"
6
85d5acb8 7int get_linux_version (void)
5d2c98e1
SK
8{
9 static int kver = -1;
10 struct utsname uts;
d404065a 11 int x = 0, y = 0, z = 0;
85d5acb8 12 int n;
5d2c98e1
SK
13
14 if (kver != -1)
15 return kver;
d404065a 16 if (uname(&uts))
85d5acb8 17 return kver = 0;
5d2c98e1 18
d404065a 19 n = sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
85d5acb8
KZ
20 if (n < 1 || n > 3)
21 return kver = 0;
22
6b95593d 23 return kver = KERNEL_VERSION(x, y, z);
5d2c98e1 24}
5b9403a6
KZ
25
26#ifdef TEST_PROGRAM
27# include <stdlib.h>
28 int main(int argc, char *argv[])
29 {
30 int rc = EXIT_FAILURE;
31
32 if (argc == 1) {
33 printf("Linux version: %d\n", get_linux_version());
34 rc = EXIT_SUCCESS;
35
36 } else if (argc == 5) {
37 const char *oper = argv[1];
38
39 int x = atoi(argv[2]),
40 y = atoi(argv[3]),
41 z = atoi(argv[4]);
42 int kver = get_linux_version();
43 int uver = KERNEL_VERSION(x, y, z);
44
45 if (strcmp(oper, "==") == 0)
46 rc = kver == uver;
47 else if (strcmp(oper, "<=") == 0)
48 rc = kver <= uver;
49 else if (strcmp(oper, ">=") == 0)
50 rc = kver >= uver;
51 else
52 errx(EXIT_FAILURE, "unsupported operator");
53
54 if (rc)
55 printf("match\n");
56 else
57 printf("not-match [%d %s %d, x.y.z: %d.%d.%d]\n",
58 kver, oper, uver, x, y, z);
59
60 rc = rc ? EXIT_SUCCESS : EXIT_FAILURE;
61
62 } else
63 fprintf(stderr, "Usage:\n"
64 " %s [<oper> <x> <y> <z>]\n"
65 "supported operators:\n"
66 " ==, <=, >=\n",
67 program_invocation_short_name);
68
69 return rc;
70 }
71#endif