fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / lib / argv_split.c
blob4096ed42f490869c252b1e33ea76050fe8f1ec92
1 /*
2 * Helper function for splitting a string into an argv-like array.
3 */
5 #include <linux/kernel.h>
6 #include <linux/ctype.h>
7 #include <linux/bug.h>
9 static const char *skip_sep(const char *cp)
11 while (*cp && isspace(*cp))
12 cp++;
14 return cp;
17 static const char *skip_arg(const char *cp)
19 while (*cp && !isspace(*cp))
20 cp++;
22 return cp;
25 static int count_argc(const char *str)
27 int count = 0;
29 while (*str) {
30 str = skip_sep(str);
31 if (*str) {
32 count++;
33 str = skip_arg(str);
37 return count;
40 /**
41 * argv_free - free an argv
42 * @argv - the argument vector to be freed
44 * Frees an argv and the strings it points to.
46 void argv_free(char **argv)
48 char **p;
49 for (p = argv; *p; p++)
50 kfree(*p);
52 kfree(argv);
54 EXPORT_SYMBOL(argv_free);
56 /**
57 * argv_split - split a string at whitespace, returning an argv
58 * @gfp: the GFP mask used to allocate memory
59 * @str: the string to be split
60 * @argcp: returned argument count
62 * Returns an array of pointers to strings which are split out from
63 * @str. This is performed by strictly splitting on white-space; no
64 * quote processing is performed. Multiple whitespace characters are
65 * considered to be a single argument separator. The returned array
66 * is always NULL-terminated. Returns NULL on memory allocation
67 * failure.
69 char **argv_split(gfp_t gfp, const char *str, int *argcp)
71 int argc = count_argc(str);
72 char **argv = kzalloc(sizeof(*argv) * (argc+1), gfp);
73 char **argvp;
75 if (argv == NULL)
76 goto out;
78 *argcp = argc;
79 argvp = argv;
81 while (*str) {
82 str = skip_sep(str);
84 if (*str) {
85 const char *p = str;
86 char *t;
88 str = skip_arg(str);
90 t = kstrndup(p, str-p, gfp);
91 if (t == NULL)
92 goto fail;
93 *argvp++ = t;
96 *argvp = NULL;
98 out:
99 return argv;
101 fail:
102 argv_free(argv);
103 return NULL;
105 EXPORT_SYMBOL(argv_split);