Ruby  2.0.0p247(2013-06-27revision41674)
missing/strlcat.c
Go to the documentation of this file.
00001 /*      $OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $     */
00002 
00003 /*
00004  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. The name of the author may not be used to endorse or promote products
00016  *    derived from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00019  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00020  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00021  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #if defined(LIBC_SCCS) && !defined(lint)
00031 static char *rcsid = "$OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $";
00032 #endif /* LIBC_SCCS and not lint */
00033 
00034 #include "ruby/missing.h"
00035 #include <sys/types.h>
00036 #include <string.h>
00037 
00038 /*
00039  * Appends src to string dst of size siz (unlike strncat, siz is the
00040  * full size of dst, not space left).  At most siz-1 characters
00041  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
00042  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
00043  * If retval >= siz, truncation occurred.
00044  */
00045 size_t
00046 strlcat(dst, src, siz)
00047         char *dst;
00048         const char *src;
00049         size_t siz;
00050 {
00051         register char *d = dst;
00052         register const char *s = src;
00053         register size_t n = siz;
00054         size_t dlen;
00055 
00056         /* Find the end of dst and adjust bytes left but don't go past end */
00057         while (n-- != 0 && *d != '\0')
00058                 d++;
00059         dlen = d - dst;
00060         n = siz - dlen;
00061 
00062         if (n == 0)
00063                 return(dlen + strlen(s));
00064         while (*s != '\0') {
00065                 if (n != 1) {
00066                         *d++ = *s;
00067                         n--;
00068                 }
00069                 s++;
00070         }
00071         *d = '\0';
00072 
00073         return(dlen + (s - src));       /* count does not include NUL */
00074 }
00075