libc: Implement strlcpy(), and make strncpy() standard-compliant
This commit is contained in:
parent
48f38bdcef
commit
42b6b927c9
@ -33,9 +33,8 @@ extern "C"
|
||||
/* Returns the length of the string str, while examining at most max bytes of str. */
|
||||
size_t strnlen(const char* str, size_t max);
|
||||
|
||||
/* Copies the string src into dest. This function is unsafe, use strncpy instead. */
|
||||
__lc_deprecated("strcpy is unsafe and should not be used; use strncpy instead") char* strcpy(char* dest,
|
||||
const char* src);
|
||||
/* Copies at most size-1 bytes from the string src into dest, null-terminating the result. */
|
||||
size_t strlcpy(char* dst, const char* src, size_t size);
|
||||
|
||||
/* Copies at most max bytes from the string src into dest. */
|
||||
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. */
|
||||
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. */
|
||||
char* strncat(char* dest, const char* src, size_t max);
|
||||
|
||||
@ -68,6 +63,14 @@ extern "C"
|
||||
/* Clears n bytes of buf. */
|
||||
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
|
||||
}
|
||||
#endif
|
||||
|
@ -70,15 +70,32 @@ extern "C"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
memcpy(dest, src, src_len > max ? max : src_len);
|
||||
return dest;
|
||||
size_t len = strlen(src);
|
||||
if (size == 0) return len;
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user