1 // SPDX-License-Identifier: GPL-2.0
3 * Helper function for splitting a string into an argv-like array.
6 #include <linux/kernel.h>
7 #include <linux/ctype.h>
8 #include <linux/string.h>
9 #include <linux/slab.h>
10 #include <linux/export.h>
12 static int count_argc(const char *str)
17 for (was_space = true; *str; str++) {
20 } else if (was_space) {
30 * argv_free - free an argv
31 * @argv - the argument vector to be freed
33 * Frees an argv and the strings it points to.
35 void argv_free(char **argv)
41 EXPORT_SYMBOL(argv_free);
44 * argv_split - split a string at whitespace, returning an argv
45 * @gfp: the GFP mask used to allocate memory
46 * @str: the string to be split
47 * @argcp: returned argument count
49 * Returns an array of pointers to strings which are split out from
50 * @str. This is performed by strictly splitting on white-space; no
51 * quote processing is performed. Multiple whitespace characters are
52 * considered to be a single argument separator. The returned array
53 * is always NULL-terminated. Returns NULL on memory allocation
56 * The source string at `str' may be undergoing concurrent alteration via
57 * userspace sysctl activity (at least). The argv_split() implementation
58 * attempts to handle this gracefully by taking a local copy to work on.
60 char **argv_split(gfp_t gfp, const char *str, int *argcp)
64 char **argv, **argv_ret;
67 argv_str = kstrndup(str, KMALLOC_MAX_SIZE - 1, gfp);
71 argc = count_argc(argv_str);
72 argv = kmalloc_array(argc + 2, sizeof(*argv), gfp);
80 for (was_space = true; *argv_str; argv_str++) {
81 if (isspace(*argv_str)) {
84 } else if (was_space) {
95 EXPORT_SYMBOL(argv_split);