OSDN Git Service

8e182a313ed2cd160bb08ccac9e2a720348ec961
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / kernel_version.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright (C) 1999,2000 by Lineo, inc.  Written by Erik Andersen
4  * <andersen@lineo.com>, <andersee@debian.org>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Library General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/utsname.h>
25
26 static int __linux_kernel_version = -1;
27
28 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
29  * so, for example,  to check if the kernel is greater than 2.2.11:
30  *     if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
31  */
32 static int find_kernel_revision(void)
33 {
34         struct utsname name;
35         int major = 0, minor = 0, patch = 0;
36
37         if (uname(&name) == -1) {
38                 return (0);
39         }
40         sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
41         return major * 65536 + minor * 256 + patch;
42 }
43
44
45 int
46 __get_linux_kernel_version (void)
47 {
48   if (__linux_kernel_version != -1)
49     return __linux_kernel_version;
50     
51   return find_kernel_revision ();
52 }