⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strlcpy.3

📁 一个C语言写的快速贝叶斯垃圾邮件过滤工具
💻 3
字号:
.\"	$NetBSD: strlcpy.3,v 1.9 2002/02/07 07:00:32 ross Exp $.\" from OpenBSD: strlcpy.3,v 1.11 2000/11/16 23:27:41 angelos Exp.\".\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com>.\" All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\"    notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\"    notice, this list of conditions and the following disclaimer in the.\"    documentation and/or other materials provided with the distribution..\" 3. The name of the author may not be used to endorse or promote products.\"    derived from this software without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE..\".Dd March 1, 2001.Dt STRLCPY 3.Os.Sh NAME.Nm strlcpy ,.Nm strlcat.Nd size-bounded string copying and concatenation.Sh LIBRARY.Lb libc.Sh SYNOPSIS.Fd #include \*[Lt]string.h\*[Gt].Ft size_t.Fn strlcpy "char *dst" "const char *src" "size_t size".Ft size_t.Fn strlcat "char *dst" "const char *src" "size_t size".Sh DESCRIPTIONThe.Fn strlcpyand.Fn strlcatfunctions copy and concatenate strings respectively.They are designedto be safer, more consistent, and less error prone replacements for.Xr strncpy 3and.Xr strncat 3 .Unlike those functions,.Fn strlcpyand.Fn strlcattake the full size of the buffer (not just the length) and guarantee toNUL-terminate the result (as long as.Fa sizeis larger than 0 or, in the case of.Fn strlcat ,as long as there is at least one byte free in.Fa dst ) .Note that you should include a byte for the NUL in.Fa size .Also note that.Fn strlcpyand.Fn strlcatonly operate on true.Dq Cstrings.This means that for.Fn strlcpy.Fa srcmust be NUL-terminated and for.Fn strlcatboth.Fa srcand.Fa dstmust be NUL-terminated..PpThe.Fn strlcpyfunction copies up to.Fa size- 1 characters from the NUL-terminated string.Fa srcto.Fa dst ,NUL-terminating the result..PpThe.Fn strlcatfunction appends the NUL-terminated string.Fa srcto the end of.Fa dst .It will append at most.Fa size- strlen(dst) - 1 bytes, NUL-terminating the result..Sh RETURN VALUESThe.Fn strlcpyand.Fn strlcatfunctions return the total length of the string they tried to create.For.Fn strlcpythat means the length of.Fa src .For.Fn strlcatthat means the initial length of.Fa dstplusthe length of.Fa src .While this may seem somewhat confusing it was done to maketruncation detection simple..PpNote however, that if.Fn strlcattraverses.Fa sizecharacters without finding a NUL, the length of the string is consideredto be.Fa sizeand the destination string will not be NUL-terminated (since there wasno space for the NUL).This keeps.Fn strlcatfrom running off the end of a string.In practice this should not happen (as it means that either.Fa sizeis incorrect or that.Fa dstis not a proper.Dq Cstring).The check exists to prevent potential security problems in incorrect code..Sh EXAMPLESThe following code fragment illustrates the simple case:.Bd -literal -offset indentchar *s, *p, buf[BUFSIZ];\&...(void)strlcpy(buf, s, sizeof(buf));(void)strlcat(buf, p, sizeof(buf));.Ed.PpTo detect truncation, perhaps while building a pathname, somethinglike the following might be used:.Bd -literal -offset indentchar *dir, *file, pname[MAXPATHLEN];\&...if (strlcpy(pname, dir, sizeof(pname)) \*[Ge] sizeof(pname))	goto toolong;if (strlcat(pname, file, sizeof(pname)) \*[Ge] sizeof(pname))	goto toolong;.Ed.PpSince we know how many characters we copied the first time, we canspeed things up a bit by using a copy instead of an append:.Bd -literal -offset indentchar *dir, *file, pname[MAXPATHLEN];size_t n;\&...n = strlcpy(pname, dir, sizeof(pname));if (n \*[Ge] sizeof(pname))	goto toolong;if (strlcpy(pname + n, file, sizeof(pname) - n) \*[Ge] sizeof(pname) - n)	goto toolong;.Ed.PpHowever, one may question the validity of such optimizations, as theydefeat the whole purpose of.Fn strlcpyand.Fn strlcat ..Sh SEE ALSO.Xr snprintf 3 ,.Xr strncat 3 ,.Xr strncpy 3.Sh HISTORY.Fn strlcpyand.Fn strlcatfirst appeared in.Ox 2.4 ,then in.Nx 1.4.3and.Fx 3.3.0 .

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -