From 2f4d78286d0c9ec4c98c73cdb13a7c0ad75f1562 Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Fri, 13 Feb 2004 14:43:35 +0000 Subject: [PATCH] split_words() --- lib/misc/lvm-string.c | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/misc/lvm-string.h | 2 ++ 2 files changed, 45 insertions(+) diff --git a/lib/misc/lvm-string.c b/lib/misc/lvm-string.c index 253a35fc6..a16a76221 100644 --- a/lib/misc/lvm-string.c +++ b/lib/misc/lvm-string.c @@ -8,6 +8,8 @@ #include "lvm-types.h" #include "lvm-string.h" +#include + /* * On error, up to glibc 2.0.6, snprintf returned -1 if buffer was too small; * From glibc 2.1 it returns number of chars (excl. trailing null) that would @@ -46,3 +48,44 @@ int emit_to_buffer(char **buffer, size_t *size, const char *fmt, ...) *size -= n; return 1; } + +/* + * consume characters while they match the predicate function. + */ +static char *_consume(char *buffer, int (*fn) (int)) +{ + while (*buffer && fn(*buffer)) + buffer++; + + return buffer; +} + +static int _isword(int c) +{ + return !isspace(c); +} + +/* + * Split buffer into NULL-separated words in argv. + * Returns number of words. + */ +int split_words(char *buffer, unsigned max, char **argv) +{ + unsigned arg; + + for (arg = 0; arg < max; arg++) { + buffer = _consume(buffer, isspace); + if (!*buffer) + break; + + argv[arg] = buffer; + buffer = _consume(buffer, _isword); + + if (*buffer) { + *buffer = '\0'; + buffer++; + } + } + + return arg; +} diff --git a/lib/misc/lvm-string.h b/lib/misc/lvm-string.h index 6e26f389c..c20efd3df 100644 --- a/lib/misc/lvm-string.h +++ b/lib/misc/lvm-string.h @@ -21,4 +21,6 @@ int lvm_snprintf(char *buf, size_t bufsize, const char *format, ...); int emit_to_buffer(char **buffer, size_t *size, const char *fmt, ...); +int split_words(char *buffer, unsigned max, char **argv); + #endif