/* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */12/*3* Copyright (c) 1998 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */19#include "file.h"20#ifndef lint21FILE_RCSID("@(#)$File: strlcpy.c,v 1.5 2022/09/24 20:30:13 christos Exp $")22#endif2324#include <sys/types.h>25#include <string.h>2627/*28* Copy src to string dst of size siz. At most siz-1 characters29* will be copied. Always NUL terminates (unless siz == 0).30* Returns strlen(src); if retval >= siz, truncation occurred.31*/32size_t33strlcpy(char *dst, const char *src, size_t siz)34{35char *d = dst;36const char *s = src;37size_t n = siz;3839/* Copy as many bytes as will fit */40if (n != 0 && --n != 0) {41do {42if ((*d++ = *s++) == 0)43break;44} while (--n != 0);45}4647/* Not enough room in dst, add NUL and traverse rest of src */48if (n == 0) {49if (siz != 0)50*d = '\0'; /* NUL-terminate dst */51while (*s++)52;53}5455return(s - src - 1); /* count does not include NUL */56}575859