libc: Implement strlcpy(), and make strncpy() standard-compliant

This commit is contained in:
apio 2022-10-15 17:24:22 +02:00
parent 48f38bdcef
commit 42b6b927c9
2 changed files with 32 additions and 12 deletions

View File

@ -33,9 +33,8 @@ extern "C"
/* Returns the length of the string str, while examining at most max bytes of str. */ /* Returns the length of the string str, while examining at most max bytes of str. */
size_t strnlen(const char* str, size_t max); size_t strnlen(const char* str, size_t max);
/* Copies the string src into dest. This function is unsafe, use strncpy instead. */ /* Copies at most size-1 bytes from the string src into dest, null-terminating the result. */
__lc_deprecated("strcpy is unsafe and should not be used; use strncpy instead") char* strcpy(char* dest, size_t strlcpy(char* dst, const char* src, size_t size);
const char* src);
/* Copies at most max bytes from the string src into dest. */ /* Copies at most max bytes from the string src into dest. */
char* strncpy(char* dest, const char* src, size_t max); char* strncpy(char* dest, const char* src, size_t max);
@ -46,10 +45,6 @@ extern "C"
/* Returns a pointer to the last occurrence of the character c in str, or NULL if it is not found. */ /* Returns a pointer to the last occurrence of the character c in str, or NULL if it is not found. */
char* strrchr(const char* str, int c); char* strrchr(const char* str, int c);
/* Concatenates the string src into dest. This function is unsafe, use strncat instead. */
__lc_deprecated("strcat is unsafe and should not be used; use strncat instead") char* strcat(char* dest,
const char* src);
/* Concatenates at most max bytes of the string src into dest. */ /* Concatenates at most max bytes of the string src into dest. */
char* strncat(char* dest, const char* src, size_t max); char* strncat(char* dest, const char* src, size_t max);
@ -68,6 +63,14 @@ extern "C"
/* Clears n bytes of buf. */ /* Clears n bytes of buf. */
void* bzero(void* buf, size_t n); void* bzero(void* buf, size_t n);
/* Copies the string src into dest. This function is unsafe, use strlcpy instead. */
__lc_deprecated("strcpy is unsafe and should not be used; use strlcpy instead") char* strcpy(char* dest,
const char* src);
/* Concatenates the string src into dest. This function is unsafe, use strncat instead. */
__lc_deprecated("strcat is unsafe and should not be used; use strncat instead") char* strcat(char* dest,
const char* src);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -70,15 +70,32 @@ extern "C"
char* strcpy(char* dest, const char* src) char* strcpy(char* dest, const char* src)
{ {
memcpy(dest, src, strlen(src) + 1); return (char*)memcpy(dest, src, strlen(src) + 1);
}
char* strncpy(char* dest, const char* src, size_t max)
{
size_t i;
for (i = 0; i < max && src[i] != 0; i++) dest[i] = src[i];
for (; i < max; i++) dest[i] = 0;
return dest; return dest;
} }
char* strncpy(char* dest, const char* src, size_t max) // FIXME: Implement strncpy according to the specification. size_t strlcpy(char* dest, const char* src, size_t size)
{ {
size_t src_len = strlen(src) + 1; // NULL byte size_t len = strlen(src);
memcpy(dest, src, src_len > max ? max : src_len); if (size == 0) return len;
return dest; if (len < (size - 1))
{
memcpy(dest, src, len);
dest[len] = 0;
}
else
{
memcpy(dest, src, size - 1);
dest[size - 1] = 0;
}
return len;
} }
int strcmp(const char* a, const char* b) int strcmp(const char* a, const char* b)