strlcpy.c

00001 /*  $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
00002 
00003 /*
00004  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 
00019 #include "config.h"
00020 #ifndef HAVE_STRLCPY
00021 
00022 #if defined(LIBC_SCCS) && !defined(lint)
00023 static char *rcsid = "$OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $";
00024 #endif /* LIBC_SCCS and not lint */
00025 
00026 #include <sys/types.h>
00027 #include <string.h>
00028 #include "misc.h"
00029 
00030 /*
00031  * Copy src to string dst of size siz.  At most siz-1 characters
00032  * will be copied.  Always NUL terminates (unless siz == 0).
00033  * Returns strlen(src); if retval >= siz, truncation occurred.
00034  */
00035 INTERNAL size_t
00036 strlcpy(char *dst, const char *src, size_t siz)
00037 {
00038     register char *d = dst;
00039     register const char *s = src;
00040     register size_t n = siz;
00041 
00042     /* Copy as many bytes as will fit */
00043     if (n != 0 && --n != 0) {
00044         do {
00045             if ((*d++ = *s++) == 0)
00046                 break;
00047         } while (--n != 0);
00048     }
00049 
00050     /* Not enough room in dst, add NUL and traverse rest of src */
00051     if (n == 0) {
00052         if (siz != 0)
00053             *d = '\0';      /* NUL-terminate dst */
00054         while (*s++)
00055             ;
00056     }
00057 
00058     return(s - src - 1);    /* count does not include NUL */
00059 }
00060 
00061 #endif
00062 

Generated on Sat Sep 15 17:18:32 2007 for pcsc-lite by  doxygen 1.4.7