diff options
Diffstat (limited to 'src/text.c')
-rw-r--r-- | src/text.c | 73 |
1 files changed, 38 insertions, 35 deletions
@@ -1,4 +1,4 @@ -/* $Id: text.c,v 1.4 2005-07-12 17:39:44 rjkaes Exp $ +/* $Id: text.c,v 1.5 2005-08-15 03:54:31 rjkaes Exp $ * * The functions included here are useful for text manipulation. They * replace or augment the standard C string library. These functions @@ -31,16 +31,16 @@ size_t strlcpy(char *dst, const char *src, size_t size) { - size_t len = strlen(src); - size_t ret = len; + size_t len = strlen(src); + size_t ret = len; - if (len >= size) - len = size - 1; + if (len >= size) + len = size - 1; - memcpy(dst, src, len); - dst[len] = '\0'; + memcpy(dst, src, len); + dst[len] = '\0'; - return ret; + return ret; } #endif @@ -54,18 +54,18 @@ strlcpy(char *dst, const char *src, size_t size) size_t strlcat(char *dst, const char *src, size_t size) { - size_t len1 = strlen(dst); - size_t len2 = strlen(src); - size_t ret = len1 + len2; - - if (len1 + len2 >= size) - len2 = size - len1 - 1; - if (len2 > 0) { - memcpy(dst + len1, src, len2); - dst[len1 + len2] = '\0'; - } - - return ret; + size_t len1 = strlen(dst); + size_t len2 = strlen(src); + size_t ret = len1 + len2; + + if (len1 + len2 >= size) + len2 = size - len1 - 1; + if (len2 > 0) { + memcpy(dst + len1, src, len2); + dst[len1 + len2] = '\0'; + } + + return ret; } #endif @@ -81,25 +81,28 @@ strlcat(char *dst, const char *src, size_t size) ssize_t chomp(char *buffer, size_t length) { - size_t chars; + size_t chars; - assert(buffer != NULL); - assert(length > 0); + assert(buffer != NULL); + assert(length > 0); - /* Make sure the arguments are valid */ - if (buffer == NULL) return -EFAULT; - if (length < 1) return -ERANGE; + /* Make sure the arguments are valid */ + if (buffer == NULL) + return -EFAULT; + if (length < 1) + return -ERANGE; - chars = 0; + chars = 0; - --length; - while (buffer[length] == '\r' || buffer[length] == '\n') { - buffer[length] = '\0'; - chars++; + --length; + while (buffer[length] == '\r' || buffer[length] == '\n') { + buffer[length] = '\0'; + chars++; - /* Stop once we get to zero to prevent wrap-around */ - if (length-- == 0) break; - } + /* Stop once we get to zero to prevent wrap-around */ + if (length-- == 0) + break; + } - return chars; + return chars; } |