lib: tst_kvercmp: Move defs to separate header
* Move definitions to separate header
(so that we can reuse them in new library)
* Remove nonexisting function prototype tst_getkver()
* Add tst_parse_kver() (to be used later in newlib)
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
diff --git a/lib/tst_kvercmp.c b/lib/tst_kvercmp.c
index abdec12..068befc 100644
--- a/lib/tst_kvercmp.c
+++ b/lib/tst_kvercmp.c
@@ -1,67 +1,85 @@
/*
+ * Copyright (c) International Business Machines Corp., 2003
+ * AUTHOR: Paul Larson <plars@linuxtestproject.org>
+ * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
*
- * Copyright (c) International Business Machines Corp., 2003
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- *
- * AUTHOR
- * Paul Larson <plars@linuxtestproject.org>
- *
- * DESCRIPTION
- * Compare a given kernel version against the current kernel version.
- * If they are the same - return 0
- * If the argument is > current kernel version - return positive int
- * If the argument is < current kernel version - return negative int
- *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+#include <limits.h>
#include <sys/utsname.h>
#include "test.h"
-void get_kver(int *k1, int *k2, int *k3)
+static char *parse_digit(const char *str, int *d)
{
- struct utsname uval;
- char *kver;
- char *r1, *r2, *r3;
+ unsigned long v;
+ char *end;
- uname(&uval);
- kver = uval.release;
- r1 = strsep(&kver, ".");
- r2 = strsep(&kver, ".");
- r3 = strsep(&kver, ".");
- *k1 = atoi(r1);
- *k2 = atoi(r2);
+ v = strtoul(str, &end, 10);
+ if (str == end)
+ return NULL;
- if (r3)
- *k3 = atoi(r3);
- else
- *k3 = 0;
+ if (v > INT_MAX)
+ return NULL;
+
+ *d = v;
+
+ if (*end != '.')
+ return NULL;
+
+ return end + 1;
+}
+
+void tst_parse_kver(const char *str_kver, int *v1, int *v2, int *v3)
+{
+ const char *str = str_kver;
+
+ *v1 = 0;
+ *v2 = 0;
+ *v3 = 0;
+
+ if (!(str = parse_digit(str, v1)))
+ goto err;
+
+ if (!(str = parse_digit(str, v2)))
+ goto err;
+
+ /*
+ * We ignore all errors here in order not to fail with versions as
+ * "2.4" or "2.6.18".
+ */
+ parse_digit(str, v3);
+
+ return;
+err:
+ tst_resm(TWARN,
+ "Invalid kernel version %s, expected %%d.%%d.%%d", str_kver);
}
int tst_kvercmp(int r1, int r2, int r3)
{
int a1, a2, a3;
int testver, currver;
+ struct utsname uval;
- get_kver(&a1, &a2, &a3);
+ uname(&uval);
+ tst_parse_kver(uval.release, &a1, &a2, &a3);
+
testver = (r1 << 16) + (r2 << 8) + r3;
currver = (a1 << 16) + (a2 << 8) + a3;