]>
| Commit | Line | Data |
|---|---|---|
| 1 | /* | |
| 2 | * No copyright is claimed. This code is in the public domain; do with | |
| 3 | * it what you wish. | |
| 4 | */ | |
| 5 | #include <stdio.h> | |
| 6 | #include <sys/utsname.h> | |
| 7 | ||
| 8 | #include "c.h" | |
| 9 | #include "linux_version.h" | |
| 10 | ||
| 11 | int get_linux_version (void) | |
| 12 | { | |
| 13 | static int kver = -1; | |
| 14 | struct utsname uts; | |
| 15 | int x = 0, y = 0, z = 0; | |
| 16 | int n; | |
| 17 | ||
| 18 | if (kver != -1) | |
| 19 | return kver; | |
| 20 | if (uname(&uts)) | |
| 21 | return kver = 0; | |
| 22 | ||
| 23 | n = sscanf(uts.release, "%d.%d.%d", &x, &y, &z); | |
| 24 | if (n < 1 || n > 3) | |
| 25 | return kver = 0; | |
| 26 | ||
| 27 | return kver = KERNEL_VERSION(x, y, z); | |
| 28 | } | |
| 29 | ||
| 30 | #ifdef TEST_PROGRAM_LINUXVERSION | |
| 31 | # include <stdlib.h> | |
| 32 | int main(int argc, char *argv[]) | |
| 33 | { | |
| 34 | int rc = EXIT_FAILURE; | |
| 35 | ||
| 36 | if (argc == 1) { | |
| 37 | printf("Linux version: %d\n", get_linux_version()); | |
| 38 | rc = EXIT_SUCCESS; | |
| 39 | ||
| 40 | } else if (argc == 5) { | |
| 41 | const char *oper = argv[1]; | |
| 42 | ||
| 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); | |
| 48 | ||
| 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"); | |
| 57 | ||
| 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); | |
| 63 | ||
| 64 | rc = rc ? EXIT_SUCCESS : EXIT_FAILURE; | |
| 65 | ||
| 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); | |
| 72 | ||
| 73 | return rc; | |
| 74 | } | |
| 75 | #endif |