* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*
- * Copyright (c) 2012, 2015, Intel Corporation.
+ * Copyright (c) 2012, 2017, Intel Corporation.
*/
/*
* This file is part of Lustre, http://www.lustre.org/
* Author: Nathan Rutman <nathan.rutman@sun.com>
*/
+#include <linux/ctype.h>
#include <libcfs/libcfs.h>
+#include <libcfs/libcfs_string.h>
char *cfs_strrstr(const char *haystack, const char *needle)
{
}
EXPORT_SYMBOL(cfs_firststr);
-char *
-cfs_trimwhite(char *str)
-{
- char *end;
-
- while (isspace(*str))
- str++;
-
- end = str + strlen(str);
- while (end > str) {
- if (!isspace(end[-1]))
- break;
- end--;
- }
-
- *end = 0;
- return str;
-}
-EXPORT_SYMBOL(cfs_trimwhite);
-
/**
* Extracts tokens from strings.
*
cfs_str2num_check(char *str, int nob, unsigned *num,
unsigned min, unsigned max)
{
- char *endp;
-
- *num = simple_strtoul(str, &endp, 0);
- if (endp == str)
- return 0;
+ bool all_numbers = true;
+ char *endp, cache;
+ int len;
+ int rc;
+
+ endp = strim(str);
+ /**
+ * kstrouint can only handle strings composed
+ * of only numbers. We need to scan the string
+ * passed in for the first non-digit character
+ * and end the string at that location. If we
+ * don't find any non-digit character we still
+ * need to place a '\0' at position len since
+ * we are not interested in the rest of the
+ * string which is longer than len in size.
+ * After we are done the character at the
+ * position we placed '\0' must be restored.
+ */
+ len = min((int)strlen(endp), nob);
+ for (; endp < str + len; endp++) {
+ if (!isxdigit(*endp) && *endp != '-' &&
+ *endp != '+') {
+ all_numbers = false;
+ break;
+ }
+ }
- for (; endp < str + nob; endp++) {
- if (!isspace(*endp))
- return 0;
+ /* Eat trailing space */
+ if (!all_numbers && isspace(*endp)) {
+ all_numbers = true;
+ endp--;
}
+ cache = *endp;
+ *endp = '\0';
+
+ rc = kstrtouint(str, 0, num);
+ *endp = cache;
+ if (rc || !all_numbers)
+ return 0;
+
return (*num >= min && *num <= max);
}
EXPORT_SYMBOL(cfs_str2num_check);
}
EXPORT_SYMBOL(cfs_expr_list_values);
+void
+cfs_expr_list_values_free(__u32 *values, int num)
+{
+ /* This array is allocated by LIBCFS_ALLOC(), so it shouldn't be freed
+ * by OBD_FREE() if it's called by module other than libcfs & LNet,
+ * otherwise we will see fake memory leak */
+ LIBCFS_FREE(values, num * sizeof(values[0]));
+}
+EXPORT_SYMBOL(cfs_expr_list_values_free);
+
/**
* Frees cfs_range_expr structures of \a expr_list.
*