blob: 6bb32ce5f0d75f3a3a5c3263daff532c408d2f9a [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
Eric Andersenaad1a882001-03-16 22:47:14 +00009#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020010/* After libbb.h, since it needs sys/types.h on some systems */
11#include <sys/utsname.h> /* for uname(2) */
12
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +000013/* Returns current kernel version encoded as major*65536 + minor*256 + patch,
Eric Andersenaad1a882001-03-16 22:47:14 +000014 * so, for example, to check if the kernel is greater than 2.2.11:
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +000015 *
16 * if (get_linux_version_code() > KERNEL_VERSION(2,2,11)) { <stuff> }
Eric Andersenaad1a882001-03-16 22:47:14 +000017 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000018int FAST_FUNC get_linux_version_code(void)
Eric Andersenaad1a882001-03-16 22:47:14 +000019{
20 struct utsname name;
Denys Vlasenko00da72b2015-10-23 18:43:16 +020021 char *t;
Denys Vlasenko24966162020-10-06 02:36:47 +020022 int r;
Eric Andersenaad1a882001-03-16 22:47:14 +000023
Andreas Oberritter556ac362012-05-05 17:47:23 +020024 uname(&name); /* never fails */
Denys Vlasenko24966162020-10-06 02:36:47 +020025 t = name.release - 1;
26 r = 1;
27 do {
28 r <<= 8;
29 if (t) {
30 r += atoi(++t);
31 t = strchr(t, '.');
32 }
33 } while (r < 0x1000000);
34 return r - 0x1000000;
Eric Andersenaad1a882001-03-16 22:47:14 +000035}