perf_counter/x86: Remove the IRQ (non-NMI) handling bits
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / lib / argv_split.c
blob5205a8dae5bc08c51a4f0f283dd812e2fab25a04
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/slab.h>
8 #include <linux/module.h>
10 static const char *skip_sep(const char *cp)
12 while (*cp && isspace(*cp))
13 cp++;
15 return cp;
18 static const char *skip_arg(const char *cp)
20 while (*cp && !isspace(*cp))
21 cp++;
23 return cp;
26 static int count_argc(const char *str)
28 int count = 0;
30 while (*str) {
31 str = skip_sep(str);
32 if (*str) {
33 count++;
34 str = skip_arg(str);
38 return count;
41 /**
42 * argv_free - free an argv
43 * @argv - the argument vector to be freed
45 * Frees an argv and the strings it points to.
47 void argv_free(char **argv)
49 char **p;
50 for (p = argv; *p; p++)
51 kfree(*p);
53 kfree(argv);
55 EXPORT_SYMBOL(argv_free);
57 /**
58 * argv_split - split a string at whitespace, returning an argv
59 * @gfp: the GFP mask used to allocate memory
60 * @str: the string to be split
61 * @argcp: returned argument count
63 * Returns an array of pointers to strings which are split out from
64 * @str. This is performed by strictly splitting on white-space; no
65 * quote processing is performed. Multiple whitespace characters are
66 * considered to be a single argument separator. The returned array
67 * is always NULL-terminated. Returns NULL on memory allocation
68 * failure.
70 char **argv_split(gfp_t gfp, const char *str, int *argcp)
72 int argc = count_argc(str);
73 char **argv = kzalloc(sizeof(*argv) * (argc+1), gfp);
74 char **argvp;
76 if (argv == NULL)
77 goto out;
79 if (argcp)
80 *argcp = argc;
82 argvp = argv;
84 while (*str) {
85 str = skip_sep(str);
87 if (*str) {
88 const char *p = str;
89 char *t;
91 str = skip_arg(str);
93 t = kstrndup(p, str-p, gfp);
94 if (t == NULL)
95 goto fail;
96 *argvp++ = t;
99 *argvp = NULL;
101 out:
102 return argv;
104 fail:
105 argv_free(argv);
106 return NULL;
108 EXPORT_SYMBOL(argv_split);