1706f2543Smrg/* 2706f2543Smrg * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 3706f2543Smrg * 4706f2543Smrg * Permission to use, copy, modify, and distribute this software for any 5706f2543Smrg * purpose with or without fee is hereby granted, provided that the above 6706f2543Smrg * copyright notice and this permission notice appear in all copies. 7706f2543Smrg * 8706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL 9706f2543Smrg * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 10706f2543Smrg * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE 11706f2543Smrg * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12706f2543Smrg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 13706f2543Smrg * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 14706f2543Smrg * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15706f2543Smrg */ 16706f2543Smrg 17706f2543Smrg#ifdef HAVE_DIX_CONFIG_H 18706f2543Smrg#include <dix-config.h> 19706f2543Smrg#endif 20706f2543Smrg 21706f2543Smrg#include <sys/types.h> 22706f2543Smrg#include <string.h> 23706f2543Smrg#include "os.h" 24706f2543Smrg 25706f2543Smrg/* 26706f2543Smrg * Copy src to string dst of size siz. At most siz-1 characters 27706f2543Smrg * will be copied. Always NUL terminates (unless siz == 0). 28706f2543Smrg * Returns strlen(src); if retval >= siz, truncation occurred. 29706f2543Smrg */ 30706f2543Smrgsize_t 31706f2543Smrgstrlcpy(char *dst, const char *src, size_t siz) 32706f2543Smrg{ 33706f2543Smrg register char *d = dst; 34706f2543Smrg register const char *s = src; 35706f2543Smrg register size_t n = siz; 36706f2543Smrg 37706f2543Smrg /* Copy as many bytes as will fit */ 38706f2543Smrg if (n != 0 && --n != 0) { 39706f2543Smrg do { 40706f2543Smrg if ((*d++ = *s++) == 0) 41706f2543Smrg break; 42706f2543Smrg } while (--n != 0); 43706f2543Smrg } 44706f2543Smrg 45706f2543Smrg /* Not enough room in dst, add NUL and traverse rest of src */ 46706f2543Smrg if (n == 0) { 47706f2543Smrg if (siz != 0) 48706f2543Smrg *d = '\0'; /* NUL-terminate dst */ 49706f2543Smrg while (*s++) 50706f2543Smrg ; 51706f2543Smrg } 52706f2543Smrg 53706f2543Smrg return s - src - 1; /* count does not include NUL */ 54706f2543Smrg} 55