summaryrefslogtreecommitdiffstats
path: root/lib/linux_version.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 14:30:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 14:30:35 +0000
commit378c18e5f024ac5a8aef4cb40d7c9aa9633d144c (patch)
tree44dfb6ca500d32cabd450649b322a42e70a30683 /lib/linux_version.c
parentInitial commit. (diff)
downloadutil-linux-378c18e5f024ac5a8aef4cb40d7c9aa9633d144c.tar.xz
util-linux-378c18e5f024ac5a8aef4cb40d7c9aa9633d144c.zip
Adding upstream version 2.38.1.upstream/2.38.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/linux_version.c')
-rw-r--r--lib/linux_version.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/linux_version.c b/lib/linux_version.c
new file mode 100644
index 0000000..119869e
--- /dev/null
+++ b/lib/linux_version.c
@@ -0,0 +1,75 @@
+/*
+ * No copyright is claimed. This code is in the public domain; do with
+ * it what you wish.
+ */
+#include <stdio.h>
+#include <sys/utsname.h>
+
+#include "c.h"
+#include "linux_version.h"
+
+int get_linux_version (void)
+{
+ static int kver = -1;
+ struct utsname uts;
+ int x = 0, y = 0, z = 0;
+ int n;
+
+ if (kver != -1)
+ return kver;
+ if (uname(&uts))
+ return kver = 0;
+
+ n = sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
+ if (n < 1 || n > 3)
+ return kver = 0;
+
+ return kver = KERNEL_VERSION(x, y, z);
+}
+
+#ifdef TEST_PROGRAM_LINUXVERSION
+# include <stdlib.h>
+int main(int argc, char *argv[])
+{
+ int rc = EXIT_FAILURE;
+
+ if (argc == 1) {
+ printf("Linux version: %d\n", get_linux_version());
+ rc = EXIT_SUCCESS;
+
+ } else if (argc == 5) {
+ const char *oper = argv[1];
+
+ int x = atoi(argv[2]),
+ y = atoi(argv[3]),
+ z = atoi(argv[4]);
+ int kver = get_linux_version();
+ int uver = KERNEL_VERSION(x, y, z);
+
+ if (strcmp(oper, "==") == 0)
+ rc = kver == uver;
+ else if (strcmp(oper, "<=") == 0)
+ rc = kver <= uver;
+ else if (strcmp(oper, ">=") == 0)
+ rc = kver >= uver;
+ else
+ errx(EXIT_FAILURE, "unsupported operator");
+
+ if (rc)
+ printf("match\n");
+ else
+ printf("not-match [%d %s %d, x.y.z: %d.%d.%d]\n",
+ kver, oper, uver, x, y, z);
+
+ rc = rc ? EXIT_SUCCESS : EXIT_FAILURE;
+
+ } else
+ fprintf(stderr, "Usage:\n"
+ " %s [<oper> <x> <y> <z>]\n"
+ "supported operators:\n"
+ " ==, <=, >=\n",
+ program_invocation_short_name);
+
+ return rc;
+}
+#endif