summaryrefslogtreecommitdiff
path: root/src/libudev
diff options
context:
space:
mode:
authorklondike <klondike@xiscosoft.es>2013-04-01 20:15:48 +0200
committerIan Stakenvicius <axs@gentoo.org>2013-04-03 14:22:40 -0400
commitd8d626c7df9d352d123fad7187ccb1bb0a4d7806 (patch)
tree9a86d4348c566de050e0a68c2d8a3fedcad4d65b /src/libudev
parentcdd665a9214ab9c0dd8ffb69749ed8bb6a005cf0 (diff)
Clean up the string functions
Avoid two evaluations of src in util_strpcpy clean the code in the ones where the pointer passed is not modified and merge the main loop of util_strpcpyl and util_strscpyl into util_strpcpyv. Also make strpcpy more compact by removing some unnecessary arithmetic. Cleanup util_strpcpyv by shortcutting size == 0 case Do some style fixups Signed-off-by: Ian Stakenvicius <axs@gentoo.org>
Diffstat (limited to 'src/libudev')
-rw-r--r--src/libudev/libudev-util.c51
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;