5.1 commit.
[dragonfly.git] / libexec / dma / dfcompat.c
blob8df906e96e33ce6f4d37ef85cef10c78310b9e73
1 #ifndef HAVE_STRLCPY
3 /*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
18 * $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $
19 * $FreeBSD: src/lib/libc/string/strlcpy.c,v 1.10 2008/10/19 10:11:35 delphij Exp $
20 * $DragonFly: src/lib/libc/string/strlcpy.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $
23 #include "dfcompat.h"
25 #include <sys/types.h>
26 #include <string.h>
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.
33 size_t
34 strlcpy(char *dst, const char *src, size_t siz)
36 char *d = dst;
37 const char *s = src;
38 size_t n = siz;
40 /* Copy as many bytes as will fit */
41 if (n != 0) {
42 while (--n != 0) {
43 if ((*d++ = *s++) == '\0')
44 break;
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++)
56 return(s - src - 1); /* count does not include NUL */
59 #endif /* !HAVE_STRLCPY */
61 #ifndef HAVE_REALLOCF
63 /*-
64 * Copyright (c) 1998, M. Warner Losh <imp@freebsd.org>
65 * All rights reserved.
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.
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.
88 * $FreeBSD: src/lib/libc/stdlib/reallocf.c,v 1.3 1999/08/28 00:01:37 peter Exp $
89 * $DragonFly: src/lib/libc/stdlib/reallocf.c,v 1.2 2003/06/17 04:26:46 dillon Exp $
91 #include <stdlib.h>
93 void *
94 reallocf(void *ptr, size_t size)
96 void *nptr;
98 nptr = realloc(ptr, size);
99 if (!nptr && ptr)
100 free(ptr);
101 return (nptr);
104 #endif /* !HAVE_REALLOCF */
106 #ifndef HAVE_GETPROGNAME
108 #ifdef __GLIBC__
110 #include <errno.h>
112 const char *
113 getprogname(void)
115 return (program_invocation_short_name);
118 #else /* __GLIBC__ */
119 #error "no getprogname implementation available"
120 #endif
122 #endif /* !HAVE_GETPROGNAME */