diff options
-rw-r--r-- | src/libudev/libudev-util.c | 51 |
1 files changed, 24 insertions, 27 deletions
diff --git a/src/libudev/libudev-util.c b/src/libudev/libudev-util.c index ac906bfec4..7d5cd30662 100644 --- a/src/libudev/libudev-util.c +++ b/src/libudev/libudev-util.c @@ -317,21 +317,19 @@ void util_remove_trailing_chars(char *path, char c) */ size_t util_strpcpy(char **dest, size_t size, const char *src) { - size_t len; - - len = strlen(src); - if (len >= size) { - if (size > 1) - *dest = mempcpy(*dest, src, size-1); - size = 0; + char *dstend = *dest + (size - 1); + + if (!size) return 0; /*Nothing to do if size is 0*/ + + *dest = memccpy(*dest, src, '\0', size); + + if (*dest) {/*Terminator character found*/ + (*dest)--; /*memccpy points to the element after the one with '\0'*/ + return (dstend - *dest) + 1;/*For some odd reason they are not taking into account the \0 in the capacity...*/ } else { - if (len > 0) { - *dest = mempcpy(*dest, src, len); - size -= len; - } + *(*dest = dstend) = '\0'; /*Restore dest and add terminator*/ + return 0; /*But here they do*/ } - *dest[0] = '\0'; - return size; } size_t util_strpcpyf(char **dest, size_t size, const char *src, ...) @@ -354,15 +352,22 @@ size_t util_strpcpyf(char **dest, size_t size, const char *src, ...) } /* concatenates list of strings, moves dest forward */ +/* Uses a va_list */ +size_t util_strpcpyv(char **dest, size_t size, const char *src, va_list va) +{ + do { + size = util_strpcpy(dest, size, src); + } while (size && (src = va_arg(va, char *))); + return size; +} + +/* concatenates list of strings, moves dest forward */ size_t util_strpcpyl(char **dest, size_t size, const char *src, ...) { va_list va; va_start(va, src); - do { - size = util_strpcpy(dest, size, src); - src = va_arg(va, char *); - } while (src != NULL); + size=util_strpcpyv(dest, size, src, va); va_end(va); return size; @@ -371,24 +376,16 @@ size_t util_strpcpyl(char **dest, size_t size, const char *src, ...) /* copies string */ size_t util_strscpy(char *dest, size_t size, const char *src) { - char *s; - - s = dest; - return util_strpcpy(&s, size, src); + return util_strpcpy(&dest, size, src); } /* concatenates list of strings */ size_t util_strscpyl(char *dest, size_t size, const char *src, ...) { va_list va; - char *s; va_start(va, src); - s = dest; - do { - size = util_strpcpy(&s, size, src); - src = va_arg(va, char *); - } while (src != NULL); + size=util_strpcpyv(&dest, size, src, va); va_end(va); return size; |