X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=string.c;h=78b862c933ced61396aadb6df04f43d8185eee21;hp=fba36e5fcc6f92ca1542fd9b5989f61099ad584a;hb=899a19c03fc05f6fd98687e3eaa86841a4633a10;hpb=adbf9f09c9bf2a680c61b1ef3541c04f1cde97cf diff --git a/string.c b/string.c index fba36e5f..78b862c9 100644 --- a/string.c +++ b/string.c @@ -18,13 +18,16 @@ /** \file string.c memory allocation and string handling functions */ -#include /* gettimeofday */ #include "para.h" +#include "string.h" + +#include /* gettimeofday */ #include #include #include /* uname() */ +#include + #include "error.h" -#include "string.h" /** * paraslash's version of realloc() @@ -379,32 +382,35 @@ __must_check __malloc char *para_homedir(void) * * \return The number of substrings found in \a args. */ -__must_check unsigned split_args(char *args, char ***argv_ptr, int delim) + +__must_check unsigned split_args(char *args, char ***argv_ptr, const char *delim) { char *p = args; char **argv; - ssize_t n = 0, i; + size_t n = 0, i, j; - while (p && (p = strchr(p, delim))) { - p++; + while ((i = strcspn(p, delim)) && (p += i)) { + p += strspn(p, delim); n++; } - *argv_ptr = para_calloc((n + 3) * sizeof(char *)); + *argv_ptr = para_malloc((n + 1) * sizeof(char *)); argv = *argv_ptr; i = 0; p = args; -// printf("split_args: a:%s\n", p); while (p) { argv[i] = p; - p = strchr(p, delim); - if (p) { -// printf("a:%s\n", p); + j = strcspn(p, delim); + if (!j) + break; + p += strcspn(p, delim); + if (*p) { *p = '\0'; -// printf("b:\n"); p++; - i++; + p += strspn(p, delim); } + i++; } + argv[n] = NULL; return n; }