summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2011-05-19 18:04:43 +0200
committerStephen Gallagher <sgallagh@redhat.com>2011-06-15 15:56:12 -0400
commit977f74dc1da8325e8622be33fa2b6f105c99011a (patch)
tree4f86abe9e66b5e3e911ced843a124c1aea490cb0 /src/util
parent9df7cbe4007edeb71477fd5647e26b839f4f5813 (diff)
downloadsssd-977f74dc1da8325e8622be33fa2b6f105c99011a.tar.gz
sssd-977f74dc1da8325e8622be33fa2b6f105c99011a.tar.bz2
sssd-977f74dc1da8325e8622be33fa2b6f105c99011a.zip
Make parse_args skip extra spaces
https://fedorahosted.org/sssd/ticket/871
Diffstat (limited to 'src/util')
-rw-r--r--src/util/util.c40
1 files changed, 24 insertions, 16 deletions
diff --git a/src/util/util.c b/src/util/util.c
index 39275ef8..6c4a6033 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -159,46 +159,54 @@ char **parse_args(const char *str)
char **ret, **r;
char *tmp;
int num;
- int i, e;
+ int i;
+ bool e, w;
tmp = malloc(strlen(str) + 1);
if (!tmp) return NULL;
ret = NULL;
num = 0;
- e = 0;
i = 0;
+ e = false;
+ w = false;
p = str;
while (*p) {
- switch (*p) {
- case '\\':
+ if (*p == '\\') {
+ w = false;
if (e) {
+ /* if we were already escaping, add a '\' literal */
tmp[i] = '\\';
i++;
- e = 0;
+ e = false;
} else {
- e = 1;
+ /* otherwise just start escaping */
+ e = true;
}
- break;
- case ' ':
+ } else if (isspace(*p)) {
if (e) {
- tmp[i] = ' ';
+ /* Add escaped whitespace literally */
+ tmp[i] = *p;
i++;
- e = 0;
- } else {
+ e = false;
+ } else if (w == false) {
+ /* If previous character was non-whitespace, arg break */
tmp[i] = '\0';
i++;
+ w = true;
}
- break;
- default:
+ /* previous char was whitespace as well, skip it */
+ } else {
+ w = false;
if (e) {
+ /* Prepend escaped chars with a literal \ */
tmp[i] = '\\';
i++;
- e = 0;
+ e = false;
}
+ /* Copy character from the source string */
tmp[i] = *p;
i++;
- break;
}
p++;
@@ -208,7 +216,7 @@ char **parse_args(const char *str)
if (e) {
tmp[i] = '\\';
i++;
- e = 0;
+ e = false;
}
tmp[i] = '\0';
i++;