blob: e927ed0e18a8382f049e23646a99e1bbce8231ef [file] [log] [blame]
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -07001/*
2 * Helper function for splitting a string into an argv-like array.
3 */
4
5#include <linux/kernel.h>
6#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -08007#include <linux/string.h>
Robert P. J. Day5a56db12007-10-20 00:25:12 +02008#include <linux/slab.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070010
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070011static int count_argc(const char *str)
12{
13 int count = 0;
Oleg Nesterov095d1412013-04-29 16:18:10 -070014 bool was_space;
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070015
Oleg Nesterov095d1412013-04-29 16:18:10 -070016 for (was_space = true; *str; str++) {
17 if (isspace(*str)) {
18 was_space = true;
19 } else if (was_space) {
20 was_space = false;
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070021 count++;
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070022 }
23 }
24
25 return count;
26}
27
28/**
29 * argv_free - free an argv
30 * @argv - the argument vector to be freed
31 *
32 * Frees an argv and the strings it points to.
33 */
34void argv_free(char **argv)
35{
Oleg Nesterov095d1412013-04-29 16:18:10 -070036 argv--;
37 kfree(argv[0]);
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070038 kfree(argv);
39}
40EXPORT_SYMBOL(argv_free);
41
42/**
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
47 *
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
53 * failure.
Oleg Nesterov095d1412013-04-29 16:18:10 -070054 *
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.
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070058 */
59char **argv_split(gfp_t gfp, const char *str, int *argcp)
60{
Oleg Nesterov095d1412013-04-29 16:18:10 -070061 char *argv_str;
62 bool was_space;
63 char **argv, **argv_ret;
64 int argc;
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070065
Oleg Nesterov095d1412013-04-29 16:18:10 -070066 argv_str = kstrndup(str, KMALLOC_MAX_SIZE - 1, gfp);
67 if (!argv_str)
68 return NULL;
69
70 argc = count_argc(argv_str);
71 argv = kmalloc(sizeof(*argv) * (argc + 2), gfp);
72 if (!argv) {
73 kfree(argv_str);
74 return NULL;
75 }
76
77 *argv = argv_str;
78 argv_ret = ++argv;
79 for (was_space = true; *argv_str; argv_str++) {
80 if (isspace(*argv_str)) {
81 was_space = true;
82 *argv_str = 0;
83 } else if (was_space) {
84 was_space = false;
85 *argv++ = argv_str;
86 }
87 }
88 *argv = NULL;
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070089
Neil Horman8e2b7052007-10-16 23:26:33 -070090 if (argcp)
91 *argcp = argc;
Oleg Nesterov095d1412013-04-29 16:18:10 -070092 return argv_ret;
Jeremy Fitzhardinged84d1cc2007-07-17 18:37:02 -070093}
94EXPORT_SYMBOL(argv_split);