tst_kvercmp.c revision 88e7b183e5584538b89721580ee81aa49bc731f4
1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2003
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 *
22 *    AUTHOR
23 *    	Paul Larson <plars@linuxtestproject.org>
24 *
25 *    DESCRIPTION
26 * 	Compare a given kernel version against the current kernel version.
27 * 	If they are the same - return 0
28 * 	If the argument is > current kernel version - return positive int
29 * 	If the argument is < current kernel version - return negative int
30 *
31 */
32
33
34#include <stdlib.h>
35#include <unistd.h>
36#include <string.h>
37#include <sys/utsname.h>
38
39void get_kver(int *k1, int *k2, int *k3)
40{
41	struct utsname uval;
42	char *kver;
43	char *r1, *r2, *r3;
44
45	uname(&uval);
46	kver = uval.release;
47	r1 = strsep(&kver, ".");
48	r2 = strsep(&kver, ".");
49	r3 = strsep(&kver, ".");
50
51	*k1 = atoi(r1);
52	*k2 = atoi(r2);
53	*k3 = atoi(r3);
54}
55
56int tst_kvercmp(int r1, int r2, int r3) {
57	int a1, a2, a3;
58	int testver, currver;
59
60	get_kver(&a1, &a2, &a3);
61	testver = (r1 << 16) + (r2 << 8) + r3;
62	currver = (a1 << 16) + (a2 << 8) + a3;
63
64	return currver - testver;
65}
66