aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJeremy Fitzhardinge <jeremy@xensource.com>2007-07-17 21:37:02 -0400
committerJeremy Fitzhardinge <jeremy@goop.org>2007-07-18 11:47:40 -0400
commitd84d1cc7647c7e4f77d517e2d87b4a106a0420d9 (patch)
treeb6ccc40d323998d4ad013c7b05613bc727a8f4e0 /lib
parent1e66df3ee301209f4a38df097d7cc5cb9b367a3f (diff)
add argv_split()
argv_split() is a helper function which takes a string, splits it at whitespace, and returns a NULL-terminated argv vector. This is deliberately simple - it does no quote processing of any kind. [ Seems to me that this is something which is already being done in the kernel, but I couldn't find any other implementations, either to steal or replace. Keep an eye out. ] Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com> Signed-off-by: Chris Wright <chrisw@sous-sol.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Randy Dunlap <randy.dunlap@oracle.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile2
-rw-r--r--lib/argv_split.c105
2 files changed, 106 insertions, 1 deletions
diff --git a/lib/Makefile b/lib/Makefile
index da68b2ca0606..614966387402 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -5,7 +5,7 @@
5lib-y := ctype.o string.o vsprintf.o cmdline.o \ 5lib-y := ctype.o string.o vsprintf.o cmdline.o \
6 rbtree.o radix-tree.o dump_stack.o \ 6 rbtree.o radix-tree.o dump_stack.o \
7 idr.o int_sqrt.o bitmap.o extable.o prio_tree.o \ 7 idr.o int_sqrt.o bitmap.o extable.o prio_tree.o \
8 sha1.o irq_regs.o reciprocal_div.o 8 sha1.o irq_regs.o reciprocal_div.o argv_split.o
9 9
10lib-$(CONFIG_MMU) += ioremap.o 10lib-$(CONFIG_MMU) += ioremap.o
11lib-$(CONFIG_SMP) += cpumask.o 11lib-$(CONFIG_SMP) += cpumask.o
diff --git a/lib/argv_split.c b/lib/argv_split.c
new file mode 100644
index 000000000000..4096ed42f490
--- /dev/null
+++ b/lib/argv_split.c
@@ -0,0 +1,105 @@
1/*
2 * Helper function for splitting a string into an argv-like array.
3 */
4
5#include <linux/kernel.h>
6#include <linux/ctype.h>
7#include <linux/bug.h>
8
9static const char *skip_sep(const char *cp)
10{
11 while (*cp && isspace(*cp))
12 cp++;
13
14 return cp;
15}
16
17static const char *skip_arg(const char *cp)
18{
19 while (*cp && !isspace(*cp))
20 cp++;
21
22 return cp;
23}
24
25static int count_argc(const char *str)
26{
27 int count = 0;
28
29 while (*str) {
30 str = skip_sep(str);
31 if (*str) {
32 count++;
33 str = skip_arg(str);
34 }
35 }
36
37 return count;
38}
39
40/**
41 * argv_free - free an argv
42 * @argv - the argument vector to be freed
43 *
44 * Frees an argv and the strings it points to.
45 */
46void argv_free(char **argv)
47{
48 char **p;
49 for (p = argv; *p; p++)
50 kfree(*p);
51
52 kfree(argv);
53}
54EXPORT_SYMBOL(argv_free);
55
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
61 *
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.
68 */
69char **argv_split(gfp_t gfp, const char *str, int *argcp)
70{
71 int argc = count_argc(str);
72 char **argv = kzalloc(sizeof(*argv) * (argc+1), gfp);
73 char **argvp;
74
75 if (argv == NULL)
76 goto out;
77
78 *argcp = argc;
79 argvp = argv;
80
81 while (*str) {
82 str = skip_sep(str);
83
84 if (*str) {
85 const char *p = str;
86 char *t;
87
88 str = skip_arg(str);
89
90 t = kstrndup(p, str-p, gfp);
91 if (t == NULL)
92 goto fail;
93 *argvp++ = t;
94 }
95 }
96 *argvp = NULL;
97
98 out:
99 return argv;
100
101 fail:
102 argv_free(argv);
103 return NULL;
104}
105EXPORT_SYMBOL(argv_split);