]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/linux_version.c
scriptreplay: cleanup usage()
[thirdparty/util-linux.git] / lib / linux_version.c
1 #include <stdio.h>
2 #include <sys/utsname.h>
3
4 #include "c.h"
5 #include "linux_version.h"
6
7 int get_linux_version (void)
8 {
9 static int kver = -1;
10 struct utsname uts;
11 int x = 0, y = 0, z = 0;
12 int n;
13
14 if (kver != -1)
15 return kver;
16 if (uname(&uts))
17 return kver = 0;
18
19 n = sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
20 if (n < 1 || n > 3)
21 return kver = 0;
22
23 return kver = KERNEL_VERSION(x, y, z);
24 }
25
26 #ifdef TEST_PROGRAM_LINUXVERSION
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