Merge branch 'vendor/OPENSSL' into HEAD
[dragonfly.git] / libexec / dma / dfcompat.c
1 #ifdef NEED_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: 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 $
21  */
22
23 #include <sys/types.h>
24 #include <string.h>
25
26 /*
27  * Copy src to string dst of size siz.  At most siz-1 characters
28  * will be copied.  Always NUL terminates (unless siz == 0).
29  * Returns strlen(src); if retval >= siz, truncation occurred.
30  */
31 size_t
32 strlcpy(char *dst, const char *src, size_t siz)
33 {
34         char *d = dst;
35         const char *s = src;
36         size_t n = siz;
37
38         /* Copy as many bytes as will fit */
39         if (n != 0) {
40                 while (--n != 0) {
41                         if ((*d++ = *s++) == '\0')
42                                 break;
43                 }
44         }
45
46         /* Not enough room in dst, add NUL and traverse rest of src */
47         if (n == 0) {
48                 if (siz != 0)
49                         *d = '\0';              /* NUL-terminate dst */
50                 while (*s++)
51                         ;
52         }
53
54         return(s - src - 1);    /* count does not include NUL */
55 }
56
57 #endif /* NEED_STRLCPY */
58
59 #ifdef NEED_REALLOCF
60
61 /*-
62  * Copyright (c) 1998, M. Warner Losh <imp@freebsd.org>
63  * All rights reserved.
64  *
65  * Redistribution and use in source and binary forms, with or without
66  * modification, are permitted provided that the following conditions
67  * are met:
68  * 1. Redistributions of source code must retain the above copyright
69  *    notice, this list of conditions and the following disclaimer.
70  * 2. Redistributions in binary form must reproduce the above copyright
71  *    notice, this list of conditions and the following disclaimer in the
72  *    documentation and/or other materials provided with the distribution.
73  *
74  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
75  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
78  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84  * SUCH DAMAGE.
85  *
86  * $FreeBSD: src/lib/libc/stdlib/reallocf.c,v 1.3 1999/08/28 00:01:37 peter Exp $
87  * $DragonFly: src/lib/libc/stdlib/reallocf.c,v 1.2 2003/06/17 04:26:46 dillon Exp $
88  */
89 #include <stdlib.h>
90
91 void *
92 reallocf(void *ptr, size_t size)
93 {
94         void *nptr;
95
96         nptr = realloc(ptr, size);
97         if (!nptr && ptr)
98                 free(ptr);
99         return (nptr);
100 }
101
102 #endif /* NEED_REALLOCF */
103
104 #ifdef NEED_GETPROGNAME
105
106 #ifdef __GLIBC__
107
108 #define __USE_GNU
109 #include <errno.h>
110
111 const char *
112 getprogname(void)
113 {
114         return (program_invocation_short_name);
115 }
116
117 #else /* __GLIBC__ */
118 #error "no getprogname implementation available"
119 #endif
120
121 #endif /* NEED_GETPROGNAME */