diff options
Diffstat (limited to 'payloads/libpayload/libc/string.c')
-rw-r--r-- | payloads/libpayload/libc/string.c | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/payloads/libpayload/libc/string.c b/payloads/libpayload/libc/string.c index 81cce26a61..b69fab6815 100644 --- a/payloads/libpayload/libc/string.c +++ b/payloads/libpayload/libc/string.c @@ -171,7 +171,9 @@ char *strcpy(char *d, const char *s) char *strncat(char *d, const char *s, size_t n) { char *p = d + strlen(d); - int max = n > strlen(s) ? strlen(s) : n; + int sl = strlen(s); + int max = n > sl ? sl : n; + // int max = n > strlen(s) ? strlen(s) : n; int i; for (i = 0; i < max; i++) @@ -182,6 +184,30 @@ char *strncat(char *d, const char *s, size_t n) } /** + * Concatenates two strings with a maximum length. + * + * @param d The destination string. + * @param s The source string. + * @param n Not more than n characters from s will be appended to d. + * @return A pointer to the destination string. + */ +size_t strlcat(char *d, const char *s, size_t n) +{ + int sl = strlen(s); + int dl = strlen(d); + + char *p = d + dl; + int max = n > (sl + dl) ? sl : (n - dl - 1); + int i; + + for (i = 0; i < max; i++) + p[i] = s[i]; + + p[i] = '\0'; + return max; +} + +/** * Find a character in a string. * * @param s The string. @@ -202,6 +228,27 @@ char *strchr(const char *s, int c) } /** + * Find a character in a string. + * + * @param s The string. + * @param c The character. + * @return A pointer to the last occurence of the character in the + * string, or NULL if the character was not encountered within the string. + */ + +char *strrchr(const char *s, int c) +{ + char *p = (char *)s + strlen(s); + + for (; p >= s; p--) { + if (*p == c) + return p; + } + + return NULL; +} + +/** * Duplicate a string. * * @param s The string to duplicate. |