1 /*
2  * $smu-mark$
3  * $name: strLcpy.c$
4  * $author: Nicolas Jombart <ecureuil@hsc.fr>$
5  * $copyright: Copyright (C) 1999 by Salvatore Sanfilippo$
6  * *copyright: (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>$
7  * $license: This software is under GPL version 2 of license$
8  * $date: Wed Feb 13 17:02:32 CET 2002$
9  * $rev:$
10  */
11 
12 /*      $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $     */
13 
14 /*
15  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. The name of the author may not be used to endorse or promote products
27  *    derived from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
30  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
31  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
38  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /* $Id: strlcpy.c,v 1.1.1.1 2003/08/31 17:23:55 antirez Exp $ */
42 
43 /* This function comes from BSD */
44 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && \
45     !defined(__bsdi__) && !defined(__APPLE__)
46 #include <sys/types.h>
47 #include <string.h>
48 
49 /*
50  * Copy src to string dst of size siz.  At most siz-1 characters
51  * will be copied.  Always NUL terminates (unless siz == 0).
52  * Returns strlen(src); if retval >= siz, truncation occurred.
53  */
strlcpy(dst,src,siz)54 size_t strlcpy(dst, src, siz)
55         char *dst;
56         const char *src;
57         size_t siz;
58 {
59         register char *d = dst;
60         register const char *s = src;
61         register size_t n = siz;
62 
63         /* Copy as many bytes as will fit */
64         if (n != 0 && --n != 0) {
65                 do {
66                         if ((*d++ = *s++) == 0)
67                                 break;
68                 } while (--n != 0);
69         }
70 
71         /* Not enough room in dst, add NUL and traverse rest of src */
72         if (n == 0) {
73                 if (siz != 0)
74                         *d = '\0';              /* NUL-terminate dst */
75                 while (*s++)
76                         ;
77         }
78 
79         return(s - src - 1);    /* count does not include NUL */
80 }
81 
82 #endif /* ! __*BSD__ */
83