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