1d38539dbapt#ifndef HAVE_STRLCPY
2d38539dbapt
3d38539dbapt/*
4d38539dbapt * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5d38539dbapt *
6d38539dbapt * Permission to use, copy, modify, and distribute this software for any
7d38539dbapt * purpose with or without fee is hereby granted, provided that the above
8d38539dbapt * copyright notice and this permission notice appear in all copies.
9d38539dbapt *
10d38539dbapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11d38539dbapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12d38539dbapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13d38539dbapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14d38539dbapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15d38539dbapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16d38539dbapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17d38539dbapt *
18d38539dbapt * $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $
19d38539dbapt * $FreeBSD$
20d38539dbapt * $DragonFly: src/lib/libc/string/strlcpy.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $
21d38539dbapt */
22d38539dbapt
23d38539dbapt#include "dfcompat.h"
24d38539dbapt
25d38539dbapt#include <sys/types.h>
26d38539dbapt#include <string.h>
27d38539dbapt
28d38539dbapt/*
29d38539dbapt * Copy src to string dst of size siz.  At most siz-1 characters
30d38539dbapt * will be copied.  Always NUL terminates (unless siz == 0).
31d38539dbapt * Returns strlen(src); if retval >= siz, truncation occurred.
32d38539dbapt */
33d38539dbaptsize_t
34d38539dbaptstrlcpy(char *dst, const char *src, size_t siz)
35d38539dbapt{
36d38539dbapt	char *d = dst;
37d38539dbapt	const char *s = src;
38d38539dbapt	size_t n = siz;
39d38539dbapt
40d38539dbapt	/* Copy as many bytes as will fit */
41d38539dbapt	if (n != 0) {
42d38539dbapt		while (--n != 0) {
43d38539dbapt			if ((*d++ = *s++) == '\0')
44d38539dbapt				break;
45d38539dbapt		}
46d38539dbapt	}
47d38539dbapt
48d38539dbapt	/* Not enough room in dst, add NUL and traverse rest of src */
49d38539dbapt	if (n == 0) {
50d38539dbapt		if (siz != 0)
51d38539dbapt			*d = '\0';		/* NUL-terminate dst */
52d38539dbapt		while (*s++)
53d38539dbapt			;
54d38539dbapt	}
55d38539dbapt
56d38539dbapt	return(s - src - 1);	/* count does not include NUL */
57d38539dbapt}
58d38539dbapt
59d38539dbapt#endif /* !HAVE_STRLCPY */
60d38539dbapt
61d38539dbapt#ifndef HAVE_REALLOCF
62d38539dbapt
63d38539dbapt/*-
64d38539dbapt * Copyright (c) 1998, M. Warner Losh <imp@freebsd.org>
65d38539dbapt * All rights reserved.
66d38539dbapt *
67d38539dbapt * Redistribution and use in source and binary forms, with or without
68d38539dbapt * modification, are permitted provided that the following conditions
69d38539dbapt * are met:
70d38539dbapt * 1. Redistributions of source code must retain the above copyright
71d38539dbapt *    notice, this list of conditions and the following disclaimer.
72d38539dbapt * 2. Redistributions in binary form must reproduce the above copyright
73d38539dbapt *    notice, this list of conditions and the following disclaimer in the
74d38539dbapt *    documentation and/or other materials provided with the distribution.
75d38539dbapt *
76d38539dbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
77d38539dbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78d38539dbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
79d38539dbapt * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
80d38539dbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81d38539dbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
82d38539dbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
83d38539dbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
84d38539dbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
85d38539dbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86d38539dbapt * SUCH DAMAGE.
87d38539dbapt *
88d38539dbapt * $FreeBSD$
89d38539dbapt * $DragonFly: src/lib/libc/stdlib/reallocf.c,v 1.2 2003/06/17 04:26:46 dillon Exp $
90d38539dbapt */
91d38539dbapt#include <stdlib.h>
92d38539dbapt
93d38539dbaptvoid *
94d38539dbaptreallocf(void *ptr, size_t size)
95d38539dbapt{
96d38539dbapt	void *nptr;
97d38539dbapt
98d38539dbapt	nptr = realloc(ptr, size);
99d38539dbapt	if (!nptr && ptr)
100d38539dbapt		free(ptr);
101d38539dbapt	return (nptr);
102d38539dbapt}
103d38539dbapt
104d38539dbapt#endif /* !HAVE_REALLOCF */
105d38539dbapt
106d38539dbapt#ifndef HAVE_GETPROGNAME
107d38539dbapt
108d38539dbapt#ifdef __GLIBC__
109d38539dbapt
110d38539dbapt#include <errno.h>
111d38539dbapt
112d38539dbaptconst char *
113d38539dbaptgetprogname(void)
114d38539dbapt{
115d38539dbapt	return (program_invocation_short_name);
116d38539dbapt}
117d38539dbapt
118d38539dbapt#else /* __GLIBC__ */
119d38539dbapt#error "no getprogname implementation available"
120d38539dbapt#endif
121d38539dbapt
122d38539dbapt#endif /* !HAVE_GETPROGNAME */
123