blob: cc237120d97fb3fbb69fb71431d231882d0a0c82 [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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020011/* After libbb.h, since it needs sys/types.h on some systems */
12#include <sys/utsname.h> /* for uname(2) */
13
Eric Andersenaad1a882001-03-16 22:47:14 +000014
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +000015/* Returns current kernel version encoded as major*65536 + minor*256 + patch,
Eric Andersenaad1a882001-03-16 22:47:14 +000016 * so, for example, to check if the kernel is greater than 2.2.11:
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +000017 *
18 * if (get_linux_version_code() > KERNEL_VERSION(2,2,11)) { <stuff> }
Eric Andersenaad1a882001-03-16 22:47:14 +000019 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020int FAST_FUNC get_linux_version_code(void)
Eric Andersenaad1a882001-03-16 22:47:14 +000021{
22 struct utsname name;
23 char *s;
24 int i, r;
25
26 if (uname(&name) == -1) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010027 bb_perror_msg("can't get system information");
Denis Vlasenkocf704332006-10-27 15:12:50 +000028 return 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000029 }
30
31 s = name.release;
32 r = 0;
Denis Vlasenkocf704332006-10-27 15:12:50 +000033 for (i = 0; i < 3; i++) {
Eric Andersenaad1a882001-03-16 22:47:14 +000034 r = r * 256 + atoi(strtok(s, "."));
35 s = NULL;
36 }
37 return r;
38}