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