nfs-ganesha 1.4

strlcpy.c

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