]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/linux_version.c
bash-completion/umount: shell charaters escape
[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 25
e8f7acb0 26#ifdef TEST_PROGRAM_LINUXVERSION
5b9403a6 27# include <stdlib.h>
e33da967
KZ
28int main(int argc, char *argv[])
29{
30 int rc = EXIT_FAILURE;
5b9403a6 31
e33da967
KZ
32 if (argc == 1) {
33 printf("Linux version: %d\n", get_linux_version());
34 rc = EXIT_SUCCESS;
5b9403a6 35
e33da967
KZ
36 } else if (argc == 5) {
37 const char *oper = argv[1];
5b9403a6 38
e33da967
KZ
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);
5b9403a6 44
e33da967
KZ
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");
5b9403a6 53
e33da967
KZ
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);
5b9403a6 59
e33da967 60 rc = rc ? EXIT_SUCCESS : EXIT_FAILURE;
5b9403a6 61
e33da967
KZ
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);
5b9403a6 68
e33da967
KZ
69 return rc;
70}
5b9403a6 71#endif