105b261ecSmrg/* 205b261ecSmrg * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 305b261ecSmrg * 405b261ecSmrg * Permission to use, copy, modify, and distribute this software for any 505b261ecSmrg * purpose with or without fee is hereby granted, provided that the above 605b261ecSmrg * copyright notice and this permission notice appear in all copies. 705b261ecSmrg * 805b261ecSmrg * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL 905b261ecSmrg * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 1005b261ecSmrg * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE 1105b261ecSmrg * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1205b261ecSmrg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 1305b261ecSmrg * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 1405b261ecSmrg * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1505b261ecSmrg */ 1605b261ecSmrg 178223e2f2Smrg#ifdef HAVE_DIX_CONFIG_H 188223e2f2Smrg#include <dix-config.h> 1905b261ecSmrg#endif 2005b261ecSmrg 2105b261ecSmrg#include <sys/types.h> 2205b261ecSmrg#include <string.h> 234642e01fSmrg#include "os.h" 2405b261ecSmrg 2535c4bbdfSmrg#ifndef HAVE_STRLCPY 2605b261ecSmrg/* 2705b261ecSmrg * Copy src to string dst of size siz. At most siz-1 characters 2805b261ecSmrg * will be copied. Always NUL terminates (unless siz == 0). 2905b261ecSmrg * Returns strlen(src); if retval >= siz, truncation occurred. 3005b261ecSmrg */ 3105b261ecSmrgsize_t 3205b261ecSmrgstrlcpy(char *dst, const char *src, size_t siz) 3305b261ecSmrg{ 3435c4bbdfSmrg register char *d = dst; 3535c4bbdfSmrg register const char *s = src; 3635c4bbdfSmrg register size_t n = siz; 3705b261ecSmrg 3835c4bbdfSmrg /* Copy as many bytes as will fit */ 3935c4bbdfSmrg if (n != 0 && --n != 0) { 4035c4bbdfSmrg do { 4135c4bbdfSmrg if ((*d++ = *s++) == 0) 4235c4bbdfSmrg break; 4335c4bbdfSmrg } while (--n != 0); 4435c4bbdfSmrg } 4505b261ecSmrg 4635c4bbdfSmrg /* Not enough room in dst, add NUL and traverse rest of src */ 4735c4bbdfSmrg if (n == 0) { 4835c4bbdfSmrg if (siz != 0) 4935c4bbdfSmrg *d = '\0'; /* NUL-terminate dst */ 5035c4bbdfSmrg while (*s++); 5135c4bbdfSmrg } 5205b261ecSmrg 5335c4bbdfSmrg return s - src - 1; /* count does not include NUL */ 5405b261ecSmrg} 5535c4bbdfSmrg#endif 56