2 * Helper function for splitting a string into an argv-like array.
5 #include <linux/kernel.h>
6 #include <linux/ctype.h>
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/export.h>
11 static int count_argc(const char *str
)
16 for (was_space
= true; *str
; str
++) {
19 } else if (was_space
) {
29 * argv_free - free an argv
30 * @argv - the argument vector to be freed
32 * Frees an argv and the strings it points to.
34 void argv_free(char **argv
)
40 EXPORT_SYMBOL(argv_free
);
43 * argv_split - split a string at whitespace, returning an argv
44 * @gfp: the GFP mask used to allocate memory
45 * @str: the string to be split
46 * @argcp: returned argument count
48 * Returns an array of pointers to strings which are split out from
49 * @str. This is performed by strictly splitting on white-space; no
50 * quote processing is performed. Multiple whitespace characters are
51 * considered to be a single argument separator. The returned array
52 * is always NULL-terminated. Returns NULL on memory allocation
55 * The source string at `str' may be undergoing concurrent alteration via
56 * userspace sysctl activity (at least). The argv_split() implementation
57 * attempts to handle this gracefully by taking a local copy to work on.
59 char **argv_split(gfp_t gfp
, const char *str
, int *argcp
)
63 char **argv
, **argv_ret
;
66 argv_str
= kstrndup(str
, KMALLOC_MAX_SIZE
- 1, gfp
);
70 argc
= count_argc(argv_str
);
71 argv
= kmalloc(sizeof(*argv
) * (argc
+ 2), gfp
);
79 for (was_space
= true; *argv_str
; argv_str
++) {
80 if (isspace(*argv_str
)) {
83 } else if (was_space
) {
94 EXPORT_SYMBOL(argv_split
);