본문 바로가기
Code/C

[C언어] 내장함수 비교 - strlcpy, strlcat

by 코드포휴먼 2020. 10. 1.

앞서 <string.h>의 strcpy, strncpy, strlcpy의 차이를 정리했다.

그 중 strlcpy는 strlcat과 유사하다. 실제로 man 가이드는 이 둘을 같이 안내한다.

간단히 차이점을 정리해본다. 

 

 

strlcpy

PROTOTYPE #include <string.h>
size_t strlcpy(char *restrict dst, const char *restrict src, size_t dstsize);
DESCRIPTION - strlcpy() takes the full size of the destination buffer
- strlcpy() copies up to dstsize - 1 characters from the string src to dst, NUL-terminating the result if dstsize is not 0.
- It is designed to be safer, more consistent, and less error prone replacements for the easilymisused functions strncpy().  
RETURN the length of src
: the total length of the string they tried to create.
CONSIDERATIONS - strlcpy() guarantees NUL-termination if there is room.   
Note that room for the NUL should be included in dstsize
- If return value is >= dstsize, output string has been truncated.

 

 

strlcat

PROTOTYPE #include <string.h>
size_t strlcat(char *restrict dst, const char *restrict src, size_t dstsize);
DESCRIPTION - strlcat() takes the full size of the destination buffer.
- strlcat() appends string src to the end of dst. It will append at most dstsize - strlen(dst) - 1 characters.
- It will then NUL-terminate, unless dstsize is 0 or the original dst string was longer than dstsize (in practice this should not happen as it means that either dstsize is incorrect or that dst is not a proper string).
- It is designed to be safer, more consistent, and less error prone replacements for the easilymisused functions strncat().   
RETURN - the initial length of dst + the length of src
: the total length of the string they tried to create.
- the length of src
: if dstsize == 0 
- the parameter dstsize + the length of src
: if dstsize <= the initial length of dst
CONSIDERATIONS - strlcat() guarantees NUL-termination if there is room.   
Note that room for the NUL should be included in dstsize
- If return value is >= dstsize, output string has been truncated.

 

 

strlcpy와 strlcat 비교

PROTOTYPE size_t strlcpy(char *restrict dst, const char *restrict src, size_t dstsize); size_t strlcat(char *restrict dst, const char *restrict src, size_t dstsize);
NAME size-bounded string copying and concatenation

DESCRIPTION
copies and concatenate strings with the same input parameters and output result as snprintf().
- replacements for the easilymisused functions strncpy(). - replacements for the easilymisused functions strncat().   

PARAMETER
&

BEHAVIOR

takes the full size of the destination buffer.
copies up to dstsize - 1 characters from the string src to dst, NUL-terminating the result if dstsize is not 0. appends string src to the end of dst. It will append at most dstsize - strlen(dst) - 1 characters.
If return value is >= dstsize, output string has been truncated.
RETURN the length of src - the initial length of dst plus the length of src
- the length of src
- the parameter dstsize + the length of src
NUL Assurance guarantees NUL-termination if there is room.
room for the NUL should be included in dstsize

 

댓글