Do You Coding?
[library] strlcat ๊ตฌํํ๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (Linux)
NAME
strlcat — size-bounded string concatenation
: size๋ก ์ ํํ ๋ฌธ์์ด ์ฐ๊ฒฐ
SYNOPSIS
#include <string.h>
size_t strlcat(char *dst, const char *src, size_t size);
DESCRIPTION
The strlcpy() and strlcat() functions copy and concatenate strings respectively. They are deโ
signed to be safer, more consistent, and less error prone replacements for strncpy(3) and
strncat(3). Unlike those functions, strlcpy() and strlcat() take the full size of the buffer
(not just the length) and guarantee to NUL-terminate the result (as long as size is larger than
0 or, in the case of strlcat(), as long as there is at least one byte free in dst). Note that a
byte for the NUL should be included in size. Also note that strlcpy() and strlcat() only operโ
ate on true “C” strings. This means that for strlcpy() src must be NUL-terminated and for
strlcat() both src and dst must be NUL-terminated.
The strlcpy() function copies up to size - 1 characters from the NUL-terminated string src to
dst, NUL-terminating the result.
The strlcat() function appends the NUL-terminated string src to the end of dst. It will append
at most size - strlen(dst) - 1 bytes, NUL-terminating the result.
: strlcpy() ๋ฐ strlcat() ๊ธฐ๋ฅ์ ๊ฐ๊ฐ ๋ฌธ์์ด์ ๋ณต์ฌํ๊ณ ์ฐ๊ฒฐํฉ๋๋ค. ์ด๋ฌํ ๊ธฐ๋ฅ์ strncpy(3) ๋ฐ strncat(3)์ ๋ํด ๋ ์์ ํ๊ณ ์ผ๊ด์ฑ์ด ์์ผ๋ฉฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ธฐ ์ฌ์ด ๋์ฒด ๊ธฐ๋ฅ์ผ๋ก ์ค๊ณ๋์์ต๋๋ค. ์ด๋ฌํ ๊ธฐ๋ฅ๊ณผ ๋ฌ๋ฆฌ strlcpy() ๋ฐ strlcat()์ ๋ฒํผ์ ์ ์ฒด ํฌ๊ธฐ(๊ธธ์ด๋ฟ๋ง ์๋๋ผ)๋ฅผ ๊ฐ์ ธ์ NUL ์ข
๋ฃ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฅํฉ๋๋ค(ํฌ๊ธฐ๊ฐ 0๋ณด๋ค ํฌ๋ค๋ฉด ๋๋ strlcat()์ ๊ฒฝ์ฐ dst์ ํ๋ ์ด์์ ๋ฐ์ดํธ ํ๋ฆฌ๊ฐ ์์ผ๋ฉด). NUL์ฉ ๋ฐ์ดํธ๋ ํฌ๊ธฐ์ ํฌํจ๋์ด์ผ ํฉ๋๋ค. ๋ํ strlcpy() ๋ฐ strlcat()์ ์ค์ "C" ๋ฌธ์์ด์์๋ง ์๋ํฉ๋๋ค. ์ด๋ strlcpy()์ ๊ฒฝ์ฐ src๊ฐ NUL ์ข
๋ฃ๋์ด์ผ ํ๋ฉฐ strlcat()์ ๊ฒฝ์ฐ src ๋ฐ dst ๋ชจ๋ NUL ์ข
๋ฃ๋์ด์ผ ํจ์ ์๋ฏธํฉ๋๋ค.
strlcpy() ํจ์๋ NUL ์ข
๋จ ๋ฌธ์์ด src์์ dst๋ก ์ต๋ 1์ ํฌ๊ธฐ๊น์ง ๋ณต์ฌํ์ฌ NUL ์ข
๋จ ๊ฒฐ๊ณผ๋ฅผ ์์ฑํฉ๋๋ค.
strlcat() ํจ์๋ NUL ์ข
๋ฃ ๋ฌธ์์ด src๋ฅผ dst์ ๋์ ์ถ๊ฐํฉ๋๋ค. ๊ทธ๊ฒ์ ์ต๋ ํฌ๊ธฐ - strlen(dst) - 1 ๋ฐ์ดํธ, NUL ์ข
๋ฃ ๊ฒฐ๊ณผ๋ฅผ ์ถ๊ฐํฉ๋๋ค. (ํด์๋ณธ)
RETURN VALUES
The strlcpy() and strlcat() functions return the total length of the string they tried to creโ
ate. For strlcpy() that means the length of src. For strlcat() that means the initial length
of dst plus the length of src. While this may seem somewhat confusing, it was done to make
truncation detection simple.
Note, however, that if strlcat() traverses size characters without finding a NUL, the length of
the string is considered to be size and the destination string will not be NUL-terminated (since
there was no space for the NUL). This keeps strlcat() from running off the end of a string. In
practice this should not happen (as it means that either size is incorrect or that dst is not a
proper “C” string). The check exists to prevent potential security problems in incorrect code.
: strlcpy()์ strlcat() ํจ์๋ ๊ทธ๋ค์ด ๋ง๋ค๋ ค๊ณ ํ๋ ๋ฌธ์์ด์ ์ ์ฒด ๊ธธ์ด๋ฅผ ๋ฐํํฉ๋๋ค. strlcpy()์ ๊ฒฝ์ฐ src์ ๊ธธ์ด๋ฅผ ์๋ฏธํฉ๋๋ค. dst์ ์ด๊ธฐ ๊ธธ์ด์ src์ ๊ธธ์ด๋ฅผ ๋ํ ๊ฒ์ ์๋ฏธํ๋ strlcat()์ ๊ฒฝ์ฐ. ๋ค์ ํผ๋์ค๋ฌ์ ๋ณด์ผ ์ ์์ง๋ง ์ ๋จ ๊ฐ์ง๋ฅผ ์ฝ๊ฒ ํ๊ธฐ ์ํด ์ํ๋์์ต๋๋ค.
๊ทธ๋ฌ๋ strlcat()์ด NUL์ ์ฐพ์ง ๋ชปํ๊ณ ํฌ๊ธฐ ๋ฌธ์๋ฅผ ํต๊ณผํ๋ฉด ๋ฌธ์์ด์ ๊ธธ์ด๋ ํฌ๊ธฐ๋ก ๊ฐ์ฃผ๋๋ฉฐ ๋์ ๋ฌธ์์ด์ NUL๋ก ์ข
๋ฃ๋์ง ์์ต๋๋ค(NUL์ ์ํ ๊ณต๊ฐ์ด ์์๊ธฐ ๋๋ฌธ). ์ด๋ก ์ธํด strlcat()์ด ๋ฌธ์์ด ๋์์ ์คํ๋์ง ์์ต๋๋ค. ์ค์ ๋ก๋ ์ด๋ฐ ์ผ์ด ๋ฐ์ํด์๋ ์ ๋ฉ๋๋ค(ํฌ๊ธฐ๊ฐ ์๋ชป๋์๊ฑฐ๋ dst๊ฐ ์ฌ๋ฐ๋ฅธ "C" ๋ฌธ์์ด์ด ์๋๊ธฐ ๋๋ฌธ). ์ด ๊ฒ์ฌ๋ ์๋ชป๋ ์ฝ๋์์ ๋ฐ์ํ ์ ์๋ ๋ณด์ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ์กด์ฌํฉ๋๋ค. (ํด์๋ณธ)
๐ ์์ฑ ์ฝ๋
size_t do_strlcat(char *dst, const char *src, size_t size)
{
size_t dst_len;
size_t src_len;
size_t i;
if (size == 0 && dst == NULL)
return (do_strlen(src));
src_len = do_strlen(src);
dst_len = do_strlen(dst);
i = 0;
if (dst_len >= size)
return (src_len + size);
while (i + dst_len + 1 < size && src[i] != '\0')
{
dst[dst_len + i] = src[i];
i ++;
}
dst[dst_len + i] = '\0';
return (src_len + dst_len);
}
๐ ์ฝ๋ ๋ฆฌ๋ทฐ
strlcat ์ size๋งํผ ๋ฌธ์์ด์ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ํจ์์ด๋ค. dst ๋ค์ src๋ฅผ ๋ถ์ฌ์ฃผ๋๋ฐ, ์ฌ๋ฌ ์ํฉ์ด ์กด์ฌํ๋ค.
ํนํ ๋ฐํ๊ฐ์, ๋ ๋ฌธ์์ด์ด ํฉ์ณ์ง ๊ณต๊ฐ์ ๊ธธ์ด๋ฅผ ๋ฐํํ๋๋ฐ, ์ด ๋ฐํ๊ฐ์ด ํน์ดํ๊ฒ ๋์จ๋ค.
1) size๊ฐ 0์ด๊ฑฐ๋ dst๊ฐ ๊ฐ๋ฆฌํค๋ ๋ฌธ์์ด์ด NULL์ธ ๊ฒฝ์ฐ, ๋์์ ์ํํ์ง ์๊ณ src์ ๊ธธ์ด๋ง ๋ฐํํ๋ค.
2) size ๋ณด๋ค dst์ ๊ธธ์ด๊ฐ ํฌ๊ฑฐ๋ ๊ฐ์ ๊ฒฝ์ฐ, ๋์์ ์ํํ์ง ์๊ณ src ๊ธธ์ด + size๋ฅผ ๋ฐํํ๋ค.
3) size ๋ณด๋ค dst์ ๊ธธ์ด๊ฐ ์์ ๊ฒฝ์ฐ, ๋ฐ๋ณต๋ฌธ์ ํตํด src๋ฅผ dst์ ๋ถ์ด๋ ๋์์ ์ํํ๋ค.
4) src๊ฐ ๊ฐ๋ฆฌํค๋ ๋ฌธ์์ด์ด NULL์ธ ๊ฒฝ์ฐ, ๋ฐ๋ณต๋ฌธ์ ์ํํ์ง ์๊ณ , dst๋์ \0์ ๋ฃ๋ ๋์๋ง ์ํํ๋ค.
(3, 4 ๋ฒ ์ํฉ์์์ ๋ฐํ๊ฐ์ src_len + dst_len ์ด๋ค.)
์ฐ์ 1๋ฒ ์ํฉ์ size๊ฐ 0์ด๋ผ์ ๋ถ์ด๋ ๋์์ ํ์ง ์๋ ๊ฒ์ด๊ณ , dst๊ฐ NULL์ด๋ฉด ๋ถ์ผ ๊ณต๊ฐ์ด ์ด๋ฏธ ๋ฌธ์์ด์ด ์ข ๋ฃ๋์ด
๋ถ์ด๋ ๋์์ ํ์ง ์๋๋ค. ๋ฐ๋ผ์, ๋ฐํ๊ฐ์ dst์ ๊ธธ์ด์ size๋ฅผ ์ ์ธํ src์ ๊ธธ์ด๋ง ๋ฐํํ๊ฒ ๋๋ค.
2๋ฒ ์ํฉ์, dst๊ฐ size๋ณด๋ค ๋ ์ปค์ ๋ถ์ด๋ ๋์์ ํ ์ ์๋ ์ํฉ์ด๋ค. ๋ฐ๋ผ์ size์ ๊ธธ์ด์ src ๊ธธ์ด๋ง ๋ํด์
๋ฐํํ๋ฉด ํฉ์ณ์ง ๊ณต๊ฐ์ ๊ธธ์ด๊ฐ ๋ฐํ๋๋ค๊ณ ๋ณผ ์ ์๋ค. ํฉ์ณ์ง์ง ์์ง๋ง ํฉ์ณ์ก์ ๋์ ๊ธธ์ด๋ฅผ ๊ฐ์ ํ๋ค.
3๋ฒ ์ํฉ์, ์ผ๋ฐ์ ์ธ ์ํฉ์ธ๋ฐ, src๋ฅผ ๋ถ์ด๋ ์ํฉ์ด ์จ ๊ฒ์ด๊ณ , ์ด ๋๋ ์ด์ size๊ฐ ์๋ dst ๊ธธ์ด๋ฅผ ๋ฃ์ด์ค์ผ
ํฉ์ณ์ง ๊ณต๊ฐ์ ํฌ๊ธฐ๊ฐ ๋ง๋ จ๋์ด ๋ฐํํ๋ฉด ์ํ๋ ๊ธธ์ด๊ฐ ๋ฐํ๋ ์ ์๋ค.
4๋ฒ ์ํฉ์์๋ ๋จ์ํ 3๋ฒ ์ํฉ์์ src ๋ด์ฉ์ด ์๋ ๊ฒฝ์ฐ์ธ๋ฐ, dst๊ฐ ๊ทธ๋๋ก ๋์ค๊ณ ๋ฐํ๋ ๊ฒฐ๊ตญ dst๊ธธ์ด๊ฐ ๋ฐํ๋๋ค.
๊ฒฐ๋ก ์, src๊ฐ ํ๋๋ผ๋ dst์ ๋ฃ์ด์ง๋ ์ํฉ์ด ์จ๋ค๋ฉด, ๋ฐํ๊ฐ์ด src๊ธธ์ด + dst๊ธธ์ด๋ก ๋ฐ๋๋ค.
์ด strlcat ํจ์๋ 'src์ ๊ธธ์ด๋ฅผ ํญ์ ํฌํจํ ํฉ์ณ์ง ๊ณต๊ฐ์ ๊ฐ๋ฅํ ๊ธธ์ด'๋ฅผ ๋ฐํํ๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [study] ๊ฐ๋ณ์ธ์ (variable argument) (0) | 2024.04.03 |
|---|---|
| [library] strchr ๊ตฌํํ๊ธฐ (0) | 2024.03.30 |
| [library] strlcpy ๊ตฌํํ๊ธฐ (0) | 2024.03.19 |
| [library] strlen ๊ตฌํํ๊ธฐ (0) | 2024.03.18 |
| [library] isprint ๊ตฌํํ๊ธฐ (0) | 2024.03.18 |