istrlcpy.c - stagit-gopher - A git gopher frontend. (mirror) Err bitreich.org 70 hgit clone git://bitreich.org/stagit-gopher/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/stagit-gopher/ URL:git://bitreich.org/stagit-gopher/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/stagit-gopher/ bitreich.org 70 1Log /scm/stagit-gopher/log.gph bitreich.org 70 1Files /scm/stagit-gopher/files.gph bitreich.org 70 1Refs /scm/stagit-gopher/refs.gph bitreich.org 70 1Tags /scm/stagit-gopher/tag bitreich.org 70 1README /scm/stagit-gopher/file/README.gph bitreich.org 70 1LICENSE /scm/stagit-gopher/file/LICENSE.gph bitreich.org 70 i--- Err bitreich.org 70 istrlcpy.c (1620B) Err bitreich.org 70 i--- Err bitreich.org 70 i 1 /* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */ Err bitreich.org 70 i 2 Err bitreich.org 70 i 3 /* Err bitreich.org 70 i 4 * Copyright (c) 1998, 2015 Todd C. Miller Err bitreich.org 70 i 5 * Err bitreich.org 70 i 6 * Permission to use, copy, modify, and distribute this software for any Err bitreich.org 70 i 7 * purpose with or without fee is hereby granted, provided that the above Err bitreich.org 70 i 8 * copyright notice and this permission notice appear in all copies. Err bitreich.org 70 i 9 * Err bitreich.org 70 i 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES Err bitreich.org 70 i 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF Err bitreich.org 70 i 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR Err bitreich.org 70 i 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES Err bitreich.org 70 i 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN Err bitreich.org 70 i 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF Err bitreich.org 70 i 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. Err bitreich.org 70 i 17 */ Err bitreich.org 70 i 18 Err bitreich.org 70 i 19 #include Err bitreich.org 70 i 20 #include Err bitreich.org 70 i 21 Err bitreich.org 70 i 22 #include "compat.h" Err bitreich.org 70 i 23 Err bitreich.org 70 i 24 /* Err bitreich.org 70 i 25 * Copy string src to buffer dst of size dsize. At most dsize-1 Err bitreich.org 70 i 26 * chars will be copied. Always NUL terminates (unless dsize == 0). Err bitreich.org 70 i 27 * Returns strlen(src); if retval >= dsize, truncation occurred. Err bitreich.org 70 i 28 */ Err bitreich.org 70 i 29 size_t Err bitreich.org 70 i 30 strlcpy(char *dst, const char *src, size_t dsize) Err bitreich.org 70 i 31 { Err bitreich.org 70 i 32 const char *osrc = src; Err bitreich.org 70 i 33 size_t nleft = dsize; Err bitreich.org 70 i 34 Err bitreich.org 70 i 35 /* Copy as many bytes as will fit. */ Err bitreich.org 70 i 36 if (nleft != 0) { Err bitreich.org 70 i 37 while (--nleft != 0) { Err bitreich.org 70 i 38 if ((*dst++ = *src++) == '\0') Err bitreich.org 70 i 39 break; Err bitreich.org 70 i 40 } Err bitreich.org 70 i 41 } Err bitreich.org 70 i 42 Err bitreich.org 70 i 43 /* Not enough room in dst, add NUL and traverse rest of src. */ Err bitreich.org 70 i 44 if (nleft == 0) { Err bitreich.org 70 i 45 if (dsize != 0) Err bitreich.org 70 i 46 *dst = '\0'; /* NUL-terminate dst */ Err bitreich.org 70 i 47 while (*src++) Err bitreich.org 70 i 48 ; Err bitreich.org 70 i 49 } Err bitreich.org 70 i 50 Err bitreich.org 70 i 51 return(src - osrc - 1); /* count does not include NUL */ Err bitreich.org 70 i 52 } Err bitreich.org 70 .