X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;ds=sidebyside;f=string.c;h=0b929688a2496d5a5f57c8a9e5590b17dd5d697a;hb=4c71ff7f9b6afe7c15331a1cc9e134c2db65cd73;hp=8b6ea5a90e466f67064ad2ef9d755b6fd7ee783a;hpb=9ca6c5e46bfbfeb6341a54703a21e25c15ef0f7e;p=adu.git diff --git a/string.c b/string.c index 8b6ea5a..0b92968 100644 --- a/string.c +++ b/string.c @@ -129,6 +129,34 @@ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...) return msg; } +/** + * adu's version of strcat(). + * + * \param a String to be appended to. + * \param b String to append. + * + * Append \p b to \p a. + * + * \return If \a a is \p NULL, return a pointer to a copy of \a b, i.e. + * para_strcat(NULL, b) is equivalent to para_strdup(b). If \a b is \p NULL, + * return \a a without making a copy of \a a. Otherwise, construct the + * concatenation \a c, free \a a (but not \a b) and return \a c. + * + * \sa strcat(3). + */ +__must_check __malloc char *adu_strcat(char *a, const char *b) +{ + char *tmp; + + if (!a) + return adu_strdup(b); + if (!b) + return a; + tmp = make_message("%s%s", a, b); + free(a); + return tmp; +} + /** \cond LLONG_MAX and LLONG_LIN might not be defined. */ #ifndef LLONG_MAX #define LLONG_MAX (1 << (sizeof(long) - 1)) @@ -167,6 +195,56 @@ __must_check int atoi64(const char *str, int64_t *result) return 1; } +/** + * Split string and return pointers to its parts. + * + * \param args The string to be split. + * \param argv_ptr Pointer to the list of substrings. + * \param delim Delimiter. + * + * This function modifies \a args by replacing each occurance of \a delim by + * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically + * and these pointers are initialized to point to the broken-up substrings + * within \a args. A pointer to this array is returned via \a argv_ptr. + * + * \return The number of substrings found in \a args. + */ +__must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim) +{ + char *p = args; + char **argv; + size_t n = 0, i, j; + + p = args + strspn(args, delim); + for (;;) { + i = strcspn(p, delim); + if (!i) + break; + p += i; + n++; + p += strspn(p, delim); + } + *argv_ptr = adu_malloc((n + 1) * sizeof(char *)); + argv = *argv_ptr; + i = 0; + p = args + strspn(args, delim); + while (p) { + argv[i] = p; + j = strcspn(p, delim); + if (!j) + break; + p += strcspn(p, delim); + if (*p) { + *p = '\0'; + p++; + p += strspn(p, delim); + } + i++; + } + argv[n] = NULL; + return n; +} + static int check_uid_arg(const char *arg, uint32_t *uid) { const uint32_t max = ~0U; @@ -222,3 +300,31 @@ out: return ret; } +int parse_uid_arg(const char *orig_arg, struct uid_range **ur) +{ + char *arg, **argv; + unsigned n; + int i, ret = 1; + + if (!orig_arg) + return 0; + arg = adu_strdup(orig_arg); + n = split_args(arg, &argv, ","); + if (!n) + return -E_SYNTAX; + *ur = adu_malloc((n + 1) * sizeof(struct uid_range)); + for (i = 0; i < n; i++) { + ret = parse_uid_range(argv[i], *ur + i); + if (ret < 0) + break; + } + free(arg); + if (ret < 0) { + free(*ur); + *ur = NULL; + } + /* an empty range indicates the end of the list */ + (*ur)[n].low = 1; + (*ur)[n].high = 0; + return n; +}