Back to index

php5  5.3.10
strlcpy.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Author:                                                              |
00016   +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: strlcpy.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "php.h"
00022 
00023 #ifndef HAVE_STRLCPY
00024 
00025 /*     $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $    */
00026 
00027 /*
00028  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
00029  * All rights reserved.
00030  *
00031  * Redistribution and use in source and binary forms, with or without
00032  * modification, are permitted provided that the following conditions
00033  * are met:
00034  * 1. Redistributions of source code must retain the above copyright
00035  *    notice, this list of conditions and the following disclaimer.
00036  * 2. Redistributions in binary form must reproduce the above copyright
00037  *    notice, this list of conditions and the following disclaimer in the
00038  *    documentation and/or other materials provided with the distribution.
00039  * 3. The name of the author may not be used to endorse or promote products
00040  *    derived from this software without specific prior written permission.
00041  *
00042  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00043  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00044  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
00045  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00046  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00047  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00048  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00049  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00050  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00051  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00052  */
00053 
00054 #if defined(LIBC_SCCS) && !defined(lint)
00055 static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $";
00056 #endif /* LIBC_SCCS and not lint */
00057 
00058 #include <sys/types.h>
00059 #include <string.h>
00060 
00061 /*
00062  * Copy src to string dst of size siz.  At most siz-1 characters
00063  * will be copied.  Always NUL terminates (unless siz == 0).
00064  * Returns strlen(src); if retval >= siz, truncation occurred.
00065  */
00066 PHPAPI size_t php_strlcpy(dst, src, siz)
00067        char *dst;
00068        const char *src;
00069        size_t siz;
00070 {
00071        register char *d = dst;
00072        register const char *s = src;
00073        register size_t n = siz;
00074 
00075        /* Copy as many bytes as will fit */
00076        if (n != 0 && --n != 0) {
00077               do {
00078                      if ((*d++ = *s++) == 0)
00079                             break;
00080               } while (--n != 0);
00081        }
00082 
00083        /* Not enough room in dst, add NUL and traverse rest of src */
00084        if (n == 0) {
00085               if (siz != 0)
00086                      *d = '\0';           /* NUL-terminate dst */
00087               while (*s++)
00088                      ;
00089        }
00090 
00091        return(s - src - 1); /* count does not include NUL */
00092 }
00093 
00094 #endif /* !HAVE_STRLCPY */
00095 
00096 /*
00097  * Local variables:
00098  * tab-width: 4
00099  * c-basic-offset: 4
00100  * End:
00101  * vim600: sw=4 ts=4 fdm=marker
00102  * vim<600: sw=4 ts=4
00103  */