# Strlcpy and strlcat

**URL:** <https://www.ruby-forum.com/t/strlcpy-and-strlcat/89280>\
**Category:** Development\
**Created:** [March 19, 2007, 7:28am UTC](https://www.ruby-forum.com/t/strlcpy-and-strlcat/89280 "2007-03-19T07:28:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![U.Nakamura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b5ac83/32.png) [@U.Nakamura](https://www.ruby-forum.com/u/U.Nakamura)\
**Post date:** [March 19, 2007, 7:28am UTC](https://www.ruby-forum.com/t/strlcpy-and-strlcat/89280/1 "2007-03-19T07:28:01Z")

</div>

e$B$3$s$K$A$O!"$J$+$`$ie(B(e$B$&e(B)e$B$G$9!#e(B

e$B$7$P$i$/A0$NLZB\<$5$s$NF|5-e(B[_1](e$B$G$$$$$N$+$Je(B)e$B$r8+$F;W$&$H$3$me(B  
e$B$,$“$C$?$N$G!”$H$j$"$($:A0=`Hw$H$7$Fe(Bstrlcpye$B$_$h$Se(Bstrlcate$B$NB8e(B  
e$B:_$r3NG’$7$F!“$J$1$l$Pe(Bmissinge$B$N$r;H$&!”$H$$$&%Q%C%A$rMQ0U$7$Fe(B  
e$B$_$^$7$?!#e(B  
[\*1]  
[http://www.kt.rim.or.jp/~kbk/zakkicho/07/zakkicho0703.html#D20070315-4](http://www.kt.rim.or.jp/~kbk/zakkicho/07/zakkicho0703.html#D20070315-4)

e$B$3$l$rF~$l$?\>e$G!“ITMQ0U$Ke(Bstrcpye$B$H$+e(Bstrcate$B$H$+$7$F$k$H$3$m$re(B  
e$BCV$-49$($F$$$-$?$$$H;W$&$N$G$9$,!”$$$+$,$J$b$s$G$7$g$&$+!#e(B

bcc/Makefile.sube$B$H$+e(Bwince/Makefile.sube$B$H$+$b$d$s$J$$$H$$$1$Je(B  
e$B$$$s$@$1$I:#2s$O\>JN,$7$F$^$9!#e(B  
missing/strlc{at,py}.ce$B$Oe(BOpenBSDe$B$N$b$N$rGR\<Z$7$F$-$^$7$?!#e(B

# Index: configure.in

— configure.in (revision 12095)  
+++ configure.in (working copy)  
@@ -561,8 +561,8 @@ powerpc-darwin\*)  
esac  
AC\_FUNC\_MEMCMP  
AC\_REPLACE\_FUNCS(dup2 memmove strcasecmp strncasecmp strerror strftime\

- 

```
strchr strstr strtoul crypt flock\

```

- 

```
isnan finite isinf hypot acosh erf)

```

- 

```
strchr strstr strtoul crypt flock vsnprintf\

```

- 

```
isnan finite isinf hypot acosh erf strlcpy strlcat)

```

# AC\_CHECK\_FUNCS(fmod killpg wait4 waitpid fork spawnv syscall chroot fsync getcwd eaccess truncate chsize times utimes fcntl lockf lstat link symlink readlink setitimer setruid seteuid setreuid setresuid setproctitle socketpair Index: missing.h

— missing.h (revision 12095)  
+++ missing.h (working copy)  
@@ -137,4 +137,12 @@ extern int snprintf(char \*, size\_t n, ch  
extern int vsnprintf(char \*, size\_t n, char const \*, va\_list);  
#endif

+#ifndef HAVE\_STRLCPY  
+extern size\_t strlcpy(char _, const char_, size\_t);  
+#endif  
+  
+#ifndef HAVE\_STRLCAT  
+extern size\_t strlcat(char _, const char_, size\_t);  
+#endif  
+  
#endif /\* MISSING\_H _/  
— /dev/null 2007-03-19 15:23:09.279198400 +0900  
+++ missing/strlcat.c 2007-03-19 15:22:21.871028800 +0900  
@@ -0,0 +1,73 @@  
+/_ $OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $ _/  
+  
+/_

- 
  - Copyright (c) 1998 Todd C. Miller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

- 
  - 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.

- 
  - 
    1. 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.

- 
  - 
    1. 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.

- \*/
- 

+#if defined(LIBC\_SCCS) && !defined(lint)  
+static char _rcsid = “$OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15  
deraadt Exp $”;  
+#endif /_ LIBC\_SCCS and not lint _/  
+  
+#include \<sys/types.h\>  
+#include \<string.h\>  
+  
+/_

- 
  - Appends src to string dst of size siz (unlike strncat, siz is the

- 
  - full size of dst, not space left). At most siz-1 characters

- 
  - will be copied. Always NUL terminates (unless siz \<= strlen(dst)).

- 
  - Returns strlen(src) + MIN(siz, strlen(initial dst)).

- 
  - If retval \>= siz, truncation occurred.

- \*/  
+size\_t  
+strlcat(dst, src, siz)
- char \*dst;
- const char \*src;
- size\_t siz;  
+{
- register char \*d = dst;
- register const char \*s = src;
- register size\_t n = siz;
- size\_t dlen;
- 
- /\* Find the end of dst and adjust bytes left but don’t go past end \*/
- while (n-- != 0 && \*d != ‘\0’)
- d++;
- dlen = d - dst;
- n = siz - dlen;
- 
- if (n == 0)
- return(dlen + strlen(s));
- while (\*s != ‘\0’) {
- if (n != 1) {
- 

```
 *d++ = *s;

```

- 

```
 n--;

```

- }
- s++;
- }
- \*d = ‘\0’;
- 
- return(dlen + (s - src)); /\* count does not include NUL _/  
+}  
— /dev/null 2007-03-19 15:23:13.455203200 +0900  
+++ missing/strlcpy.c 2007-03-19 15:22:48.689592000 +0900  
@@ -0,0 +1,69 @@  
+/_ $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ \*/
- 

+/\*

- 
  - Copyright (c) 1998 Todd C. Miller [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

- 
  - 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.

- 
  - 
    1. 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.

- 
  - 
    1. 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.

- \*/
- 

+#if defined(LIBC\_SCCS) && !defined(lint)  
+static char _rcsid = “$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16  
deraadt Exp $”;  
+#endif /_ LIBC\_SCCS and not lint _/  
+  
+#include \<sys/types.h\>  
+#include \<string.h\>  
+  
+/_

- 
  - Copy src to string dst of size siz. At most siz-1 characters

- 
  - will be copied. Always NUL terminates (unless siz == 0).

- 
  - Returns strlen(src); if retval \>= siz, truncation occurred.

- \*/  
+size\_t  
+strlcpy(dst, src, siz)
- char \*dst;
- const char \*src;
- size\_t siz;  
+{
- register char \*d = dst;
- register const char \*s = src;
- register size\_t n = siz;
- 
- /\* Copy as many bytes as will fit \*/
- if (n != 0 && --n != 0) {
- do {
- 

```
 if ((*d++ = *s++) == 0)

```

- 

```
   break;

```

- } while (–n != 0);
- }
- 
- /\* Not enough room in dst, add NUL and traverse rest of src \*/
- if (n == 0) {
- if (siz != 0)
- 

```
 *d = '\0'; /* NUL-terminate dst */

```

- while (\*s++)
- 

```
 ;

```

- }
- 
- return(s - src - 1); /\* count does not include NUL \*/  
+}  
Index: win32/Makefile.sub  
===================================================================  
— win32/Makefile.sub (revision 12095)  
+++ win32/Makefile.sub (working copy)  
@@ -144,7 +144,7 @@ RFLAGS = -r  
EXTLIBS =  
!endif  
LIBS = oldnames.lib user32.lib advapi32.lib shell32.lib version.lib  
ws2\_32.lib $(EXTLIBS)  
-MISSING = acosh.obj crypt.obj erf.obj win32.obj  
+MISSING = acosh.obj crypt.obj erf.obj strlcpy.obj strlcat.obj win32.obj

ARFLAGS = -machine:$(MACHINE) -out:  
CC = $(CC) -nologo  
@@ -404,7 +404,7 @@ s,@ARFLAGS@,$(ARFLAGS),;t t  
s,@LN\_S@,$(LN\_S),;t t  
s,@SET\_MAKE@,$(SET\_MAKE),;t t  
s,@CP@,copy \> nul,;t t  
-s,@LIBOBJS@, acosh.obj crypt.obj erf.obj win32.obj,;t t  
+s,@LIBOBJS@, acosh.obj crypt.obj erf.obj strlcpy.obj strlcat.obj  
win32.obj,;t t  
s,@ALLOCA@,$(ALLOCA),;t t  
s,@DEFAULT\_KCODE@,$(DEFAULT\_KCODE),;t t  
s,@EXEEXT@,.exe,;t t

e$B$=$l$G$O!#e(B

---

<div class="post-metadata">

**Author:** ![U.Nakamura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b5ac83/32.png) [@U.Nakamura](https://www.ruby-forum.com/u/U.Nakamura)\
**Post date:** [March 19, 2007, 8:11am UTC](https://www.ruby-forum.com/t/strlcpy-and-strlcat/89280/2 "2007-03-19T08:11:04Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:30637] strlcpy and strlcat”  
on Mon, 19 Mar 2007 15:26:53 +0900, “U.Nakamura”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|e$B$7$P$i$/A0$NLZB\<$5$s$NF|5-e(B[_1](e$B$G$$$$$N$+$Je(B)e$B$r8+$F;W$&$H$3$me(B  
|e$B$,$“$C$?$N$G!”$H$j$"$($:A0=`Hw$H$7$Fe(Bstrlcpye$B$_$h$Se(Bstrlcate$B$NB8e(B  
|e$B:_$r3NG’$7$F!“$J$1$l$Pe(Bmissinge$B$N$r;H$&!”$H$$$&%Q%C%A$rMQ0U$7$Fe(B  
|e$B$_$^$7$?!#e(B  
|[\*1] [http://www.kt.rim.or.jp/~kbk/zakkicho/07/zakkicho0703.html#D20070315-4](http://www.kt.rim.or.jp/~kbk/zakkicho/07/zakkicho0703.html#D20070315-4)  
|  
|e$B$3$l$rF~$l$?\>e$G!“ITMQ0U$Ke(Bstrcpye$B$H$+e(Bstrcate$B$H$+$7$F$k$H$3$m$re(B  
|e$BCV$-49$($F$$$-$?$$$H;W$&$N$G$9$,!”$$$+$,$J$b$s$G$7$g$&$+!#e(B  
|  
|bcc/Makefile.sube$B$H$+e(Bwince/Makefile.sube$B$H$+$b$d$s$J$$$H$$$1$Je(B  
|e$B$$$s$@$1$I:#2s$O\>JN,$7$F$^$9!#e(B  
|missing/strlc{at,py}.ce$B$Oe(BOpenBSDe$B$N$b$N$rGR\<Z$7$F$-$^$7$?!#e(B

trunke$B$NJ}$O%3%\_%C%He(BOKe$B$G$9!#e(B

---

<div class="post-metadata">

**Author:** ![U.Nakamura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b5ac83/32.png) [@U.Nakamura](https://www.ruby-forum.com/u/U.Nakamura)\
**Post date:** [March 19, 2007, 11:14am UTC](https://www.ruby-forum.com/t/strlcpy-and-strlcat/89280/3 "2007-03-19T11:14:29Z")

</div>

e$BLZB\<$G$9!#e(B

On Mon, 19 Mar 2007 15:26:53 +0900  
“U.Nakamura” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> e$B$7$P$i$/A0$NLZB\<$5$s$NF|5-e(B[_1](e$B$G$$$$$N$+$Je(B)e$B$r8+$F;W$&$H$3$me(B  
> e$B$,$“$C$?$N$G!”$H$j$"$($:A0=`Hw$H$7$Fe(Bstrlcpye$B$_$h$Se(Bstrlcate$B$NB8e(B  
> e$B:_$r3NG’$7$F!“$J$1$l$Pe(Bmissinge$B$N$r;H$&!”$H$$$&%Q%C%A$rMQ0U$7$Fe(B  
> e$B$_$^$7$?!#e(B  
> [\*1] [http://www.kt.rim.or.jp/~kbk/zakkicho/07/zakkicho0703.html#D20070315-4](http://www.kt.rim.or.jp/~kbk/zakkicho/07/zakkicho0703.html#D20070315-4)

e$B$"!\<!"4V0c$C$F$be(Bbloge$B$G$O$J$$$G$7$g$&$+$i!"8E\<0$f$+$7$$e(Bwebe$BF|5-$He(B  
e$B$$$&$3$H$G$h$$$G$9!#e(B

e$B4WOC5YBj!#e(B

> e$B$3$l$rF~$l$?\>e$G!“ITMQ0U$Ke(Bstrcpye$B$H$+e(Bstrcate$B$H$+$7$F$k$H$3$m$re(B  
> e$BCV$-49$($F$$$-$?$$$H;W$&$N$G$9$,!”$$$+$,$J$b$s$G$7$g$&$+!#e(B

e$B$b$A$m$s%$%s%?%W%j%?K\BN$,=\*$o$k$+L=h$,$D$$$F$+$i$H$$$&$3$H$K$J$k$Ge(B  
e$B$7$g$&$,!"I8=`E:IU$5$l$k$h$&$J3HD%%i%$%V%i%j$K4X$7$F$b%,%$%I%i%$%se(B  
e$B$J$j$r:n@.$7$?\>e$G%A%'%C%/$7$F$b$$$$$s$8$c$J$$$+$J$H;W$&$N$G$9$,e(B  
e$B$I$&$G$7$g$&$+!#e(B

e$B\<B:]$K$d$k?M$N\<j4V$r9M$($F$$$J$$C1$J$k;W$$IU$-$G$9$N$G!"%9%k!\<$7$F$$$?e(B  
e$B$@$$$F$+$^$$$^$;$s!#e(B
