diff options
author | Stuart Henderson <sthen@cvs.openbsd.org> | 2015-07-19 17:52:29 +0000 |
---|---|---|
committer | Stuart Henderson <sthen@cvs.openbsd.org> | 2015-07-19 17:52:29 +0000 |
commit | 5640253e6a858c1287ea930b6ccc7a05ecf3e1b3 (patch) | |
tree | 4c3ecf3af64002b3fd30649b021da44036637df6 /usr.sbin/nsd | |
parent | 29742bc144a31815cf8af6b82807bd146e1f607f (diff) |
remove unused compat files, ok florian@
b64_{ntop,pton} remain for now, they are in libc as __b64_{ntop,pton}
but to use this we either need to pull in resolv.h (which does "#define
b64_ntop __b64_ntop") or access the __ versions directly (as done in ssh).
Diffstat (limited to 'usr.sbin/nsd')
-rw-r--r-- | usr.sbin/nsd/compat/basename.c | 18 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/fake-rfc2553.c | 227 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/fake-rfc2553.h | 181 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/inet_aton.c | 178 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/inet_ntop.c | 208 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/inet_pton.c | 226 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/malloc.c | 22 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/memcmp.c | 25 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/memcmp.h | 16 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/memmove.c | 43 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/pselect.c | 44 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/snprintf.c | 1036 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/strlcat.c | 73 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/strlcpy.c | 57 | ||||
-rw-r--r-- | usr.sbin/nsd/compat/strptime.c | 349 |
15 files changed, 0 insertions, 2703 deletions
diff --git a/usr.sbin/nsd/compat/basename.c b/usr.sbin/nsd/compat/basename.c deleted file mode 100644 index cd8600c31ea..00000000000 --- a/usr.sbin/nsd/compat/basename.c +++ /dev/null @@ -1,18 +0,0 @@ -/* Return the basename of a pathname. - This file is in the public domain. */ - -char * -basename (name) - const char *name; -{ - const char *base; - - for (base = name; *name; name++) - { - if (*name == '/') - { - base = name + 1; - } - } - return (char *) base; -} diff --git a/usr.sbin/nsd/compat/fake-rfc2553.c b/usr.sbin/nsd/compat/fake-rfc2553.c deleted file mode 100644 index 91ddf8a809b..00000000000 --- a/usr.sbin/nsd/compat/fake-rfc2553.c +++ /dev/null @@ -1,227 +0,0 @@ -/* From openssh 4.3p2 filename openbsd-compat/fake-rfc2553.h */ -/* - * Copyright (C) 2000-2003 Damien Miller. All rights reserved. - * Copyright (C) 1999 WIDE Project. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Pseudo-implementation of RFC2553 name / address resolution functions - * - * But these functions are not implemented correctly. The minimum subset - * is implemented for ssh use only. For example, this routine assumes - * that ai_family is AF_INET. Don't use it for another purpose. - */ - -#include <unistd.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> -#include "compat/fake-rfc2553.h" - -#ifndef HAVE_GETNAMEINFO -int getnameinfo(const struct sockaddr *sa, size_t ATTR_UNUSED(salen), char *host, - size_t hostlen, char *serv, size_t servlen, int flags) -{ - struct sockaddr_in *sin = (struct sockaddr_in *)sa; - struct hostent *hp; - char tmpserv[16]; - - if (serv != NULL) { - snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port)); - if (strlcpy(serv, tmpserv, servlen) >= servlen) - return (EAI_MEMORY); - } - - if (host != NULL) { - if (flags & NI_NUMERICHOST) { - if (strlcpy(host, inet_ntoa(sin->sin_addr), - hostlen) >= hostlen) - return (EAI_MEMORY); - else - return (0); - } else { - hp = gethostbyaddr((char *)&sin->sin_addr, - sizeof(struct in_addr), AF_INET); - if (hp == NULL) - return (EAI_NODATA); - - if (strlcpy(host, hp->h_name, hostlen) >= hostlen) - return (EAI_MEMORY); - else - return (0); - } - } - return (0); -} -#endif /* !HAVE_GETNAMEINFO */ - -#ifndef HAVE_GAI_STRERROR -#ifdef HAVE_CONST_GAI_STRERROR_PROTO -const char * -#else -char * -#endif -gai_strerror(int err) -{ - switch (err) { - case EAI_NODATA: - return ("no address associated with name"); - case EAI_MEMORY: - return ("memory allocation failure."); - case EAI_NONAME: - return ("nodename nor servname provided, or not known"); - default: - return ("unknown/invalid error."); - } -} -#endif /* !HAVE_GAI_STRERROR */ - -#ifndef HAVE_FREEADDRINFO -void -freeaddrinfo(struct addrinfo *ai) -{ - struct addrinfo *next; - - for(; ai != NULL;) { - next = ai->ai_next; - free(ai); - ai = next; - } -} -#endif /* !HAVE_FREEADDRINFO */ - -#ifndef HAVE_GETADDRINFO -static struct -addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints) -{ - struct addrinfo *ai; - - ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in)); - if (ai == NULL) - return (NULL); - - memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in)); - - ai->ai_addr = (struct sockaddr *)(ai + 1); - /* XXX -- ssh doesn't use sa_len */ - ai->ai_addrlen = sizeof(struct sockaddr_in); - ai->ai_addr->sa_family = ai->ai_family = AF_INET; - - ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port; - ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr; - - /* XXX: the following is not generally correct, but does what we want */ - if (hints->ai_socktype) - ai->ai_socktype = hints->ai_socktype; - else - ai->ai_socktype = SOCK_STREAM; - - if (hints->ai_protocol) - ai->ai_protocol = hints->ai_protocol; - - return (ai); -} - -int -getaddrinfo(const char *hostname, const char *servname, - const struct addrinfo *hints, struct addrinfo **res) -{ - struct hostent *hp; - struct servent *sp; - struct in_addr in; - int i; - long int port; - u_long addr; - - port = 0; - if (servname != NULL) { - char *cp; - - port = strtol(servname, &cp, 10); - if (port > 0 && port <= 65535 && *cp == '\0') - port = htons(port); - else if ((sp = getservbyname(servname, NULL)) != NULL) - port = sp->s_port; - else - port = 0; - } - - if (hints && hints->ai_flags & AI_PASSIVE) { - addr = htonl(0x00000000); - if (hostname && inet_aton(hostname, &in) != 0) - addr = in.s_addr; - *res = malloc_ai(port, addr, hints); - if (*res == NULL) - return (EAI_MEMORY); - return (0); - } - - if (!hostname) { - *res = malloc_ai(port, htonl(0x7f000001), hints); - if (*res == NULL) - return (EAI_MEMORY); - return (0); - } - - if (inet_aton(hostname, &in)) { - *res = malloc_ai(port, in.s_addr, hints); - if (*res == NULL) - return (EAI_MEMORY); - return (0); - } - - /* Don't try DNS if AI_NUMERICHOST is set */ - if (hints && hints->ai_flags & AI_NUMERICHOST) - return (EAI_NONAME); - - hp = gethostbyname(hostname); - if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { - struct addrinfo *cur, *prev; - - cur = prev = *res = NULL; - for (i = 0; hp->h_addr_list[i]; i++) { - struct in_addr *in = (struct in_addr *)hp->h_addr_list[i]; - - cur = malloc_ai(port, in->s_addr, hints); - if (cur == NULL) { - if (*res != NULL) - freeaddrinfo(*res); - return (EAI_MEMORY); - } - if (prev) - prev->ai_next = cur; - else - *res = cur; - - prev = cur; - } - return (0); - } - - return (EAI_NODATA); -} -#endif /* !HAVE_GETADDRINFO */ diff --git a/usr.sbin/nsd/compat/fake-rfc2553.h b/usr.sbin/nsd/compat/fake-rfc2553.h deleted file mode 100644 index efae6dc1e18..00000000000 --- a/usr.sbin/nsd/compat/fake-rfc2553.h +++ /dev/null @@ -1,181 +0,0 @@ -/* From openssh 4.3p2 filename openbsd-compat/fake-rfc2553.h */ -/* - * Copyright (C) 2000-2003 Damien Miller. All rights reserved. - * Copyright (C) 1999 WIDE Project. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Pseudo-implementation of RFC2553 name / address resolution functions - * - * But these functions are not implemented correctly. The minimum subset - * is implemented for ssh use only. For example, this routine assumes - * that ai_family is AF_INET. Don't use it for another purpose. - */ - -#ifndef _FAKE_RFC2553_H -#define _FAKE_RFC2553_H - -#include <config.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netdb.h> -#include <limits.h> - -/* - * First, socket and INET6 related definitions - */ -#ifndef HAVE_STRUCT_SOCKADDR_STORAGE -# define _SS_MAXSIZE 128 /* Implementation specific max size */ -# define _SS_PADSIZE (_SS_MAXSIZE - sizeof (struct sockaddr)) -struct sockaddr_storage { - struct sockaddr ss_sa; - char __ss_pad2[_SS_PADSIZE]; -}; -# define ss_family ss_sa.sa_family -#endif /* !HAVE_STRUCT_SOCKADDR_STORAGE */ - -#ifndef IN6_IS_ADDR_LOOPBACK -# define IN6_IS_ADDR_LOOPBACK(a) \ - (((uint32_t *)(a))[0] == 0 && ((uint32_t *)(a))[1] == 0 && \ - ((uint32_t *)(a))[2] == 0 && ((uint32_t *)(a))[3] == htonl(1)) -#endif /* !IN6_IS_ADDR_LOOPBACK */ - -#ifndef HAVE_STRUCT_IN6_ADDR -struct in6_addr { - uint8_t s6_addr[16]; -}; -#endif /* !HAVE_STRUCT_IN6_ADDR */ - -#ifndef HAVE_STRUCT_SOCKADDR_IN6 -struct sockaddr_in6 { - unsigned short sin6_family; - uint16_t sin6_port; - uint32_t sin6_flowinfo; - struct in6_addr sin6_addr; -}; -#endif /* !HAVE_STRUCT_SOCKADDR_IN6 */ - -#ifndef AF_INET6 -/* Define it to something that should never appear */ -#define AF_INET6 AF_MAX -#endif - -#ifndef PF_INET -#define PF_INET AF_INET -#endif -#ifndef PF_INET6 -#define PF_INET6 AF_INET6 -#endif - -/* - * Next, RFC2553 name / address resolution API - */ - -#ifndef NI_NUMERICHOST -# define NI_NUMERICHOST (1) -#endif -#ifndef NI_NAMEREQD -# define NI_NAMEREQD (1<<1) -#endif -#ifndef NI_NUMERICSERV -# define NI_NUMERICSERV (1<<2) -#endif - -#ifndef AI_PASSIVE -# define AI_PASSIVE (1) -#endif -#ifndef AI_CANONNAME -# define AI_CANONNAME (1<<1) -#endif -#ifndef AI_NUMERICHOST -# define AI_NUMERICHOST (1<<2) -#endif - -#ifndef NI_MAXSERV -# define NI_MAXSERV 32 -#endif /* !NI_MAXSERV */ -#ifndef NI_MAXHOST -# define NI_MAXHOST 1025 -#endif /* !NI_MAXHOST */ - -#ifndef INT_MAX -#define INT_MAX 0xffffffff -#endif - -#ifndef EAI_NODATA -# define EAI_NODATA (INT_MAX - 1) -#endif -#ifndef EAI_MEMORY -# define EAI_MEMORY (INT_MAX - 2) -#endif -#ifndef EAI_NONAME -# define EAI_NONAME (INT_MAX - 3) -#endif -#ifndef EAI_SYSTEM -# define EAI_SYSTEM (INT_MAX - 4) -#endif - -#ifndef HAVE_STRUCT_ADDRINFO -struct addrinfo { - int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ - int ai_family; /* PF_xxx */ - int ai_socktype; /* SOCK_xxx */ - int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ - size_t ai_addrlen; /* length of ai_addr */ - char *ai_canonname; /* canonical name for hostname */ - struct sockaddr *ai_addr; /* binary address */ - struct addrinfo *ai_next; /* next structure in linked list */ -}; -#endif /* !HAVE_STRUCT_ADDRINFO */ - -#ifndef HAVE_GETADDRINFO -#ifdef getaddrinfo -# undef getaddrinfo -#endif -#define getaddrinfo(a,b,c,d) (ssh_getaddrinfo(a,b,c,d)) -int getaddrinfo(const char *, const char *, - const struct addrinfo *, struct addrinfo **); -#endif /* !HAVE_GETADDRINFO */ - -#if !defined(HAVE_GAI_STRERROR) && !defined(HAVE_CONST_GAI_STRERROR_PROTO) -#define gai_strerror(a) (ssh_gai_strerror(a)) -char *gai_strerror(int); -#endif /* !HAVE_GAI_STRERROR */ - -#ifndef HAVE_FREEADDRINFO -#define freeaddrinfo(a) (ssh_freeaddrinfo(a)) -void freeaddrinfo(struct addrinfo *); -#endif /* !HAVE_FREEADDRINFO */ - -#ifndef HAVE_GETNAMEINFO -#define getnameinfo(a,b,c,d,e,f,g) (ssh_getnameinfo(a,b,c,d,e,f,g)) -int getnameinfo(const struct sockaddr *, size_t, char *, size_t, - char *, size_t, int); -#endif /* !HAVE_GETNAMEINFO */ - -#endif /* !_FAKE_RFC2553_H */ - diff --git a/usr.sbin/nsd/compat/inet_aton.c b/usr.sbin/nsd/compat/inet_aton.c deleted file mode 100644 index 430f9b03d4f..00000000000 --- a/usr.sbin/nsd/compat/inet_aton.c +++ /dev/null @@ -1,178 +0,0 @@ -/* From openssh4.3p2 compat/inet_aton.c */ -/* - * Copyright (c) 1983, 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - - * Portions Copyright (c) 1993 by Digital Equipment Corporation. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies, and that - * the name of Digital Equipment Corporation not be used in advertising or - * publicity pertaining to distribution of the document or software without - * specific, written prior permission. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL - * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT - * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS - * SOFTWARE. - * - - * --Copyright-- - */ - -/* OPENBSD ORIGINAL: lib/libc/net/inet_addr.c */ - -#include <config.h> - -#if !defined(HAVE_INET_ATON) - -#include <sys/types.h> -#include <sys/param.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <ctype.h> - -#if 0 -/* - * Ascii internet address interpretation routine. - * The value returned is in network order. - */ -in_addr_t -inet_addr(const char *cp) -{ - struct in_addr val; - - if (inet_aton(cp, &val)) - return (val.s_addr); - return (INADDR_NONE); -} -#endif - -/* - * Check whether "cp" is a valid ascii representation - * of an Internet address and convert to a binary address. - * Returns 1 if the address is valid, 0 if not. - * This replaces inet_addr, the return value from which - * cannot distinguish between failure and a local broadcast address. - */ -int -inet_aton(const char *cp, struct in_addr *addr) -{ - uint32_t val; - int base, n; - char c; - unsigned int parts[4]; - unsigned int *pp = parts; - - c = *cp; - for (;;) { - /* - * Collect number up to ``.''. - * Values are specified as for C: - * 0x=hex, 0=octal, isdigit=decimal. - */ - if (!isdigit((unsigned char)c)) - return (0); - val = 0; base = 10; - if (c == '0') { - c = *++cp; - if (c == 'x' || c == 'X') - base = 16, c = *++cp; - else - base = 8; - } - for (;;) { - if (isascii((unsigned char)c) && isdigit((unsigned char)c)) { - val = (val * base) + (c - '0'); - c = *++cp; - } else if (base == 16 && isascii((unsigned char)c) && isxdigit((unsigned char)c)) { - val = (val << 4) | - (c + 10 - (islower((unsigned char)c) ? 'a' : 'A')); - c = *++cp; - } else - break; - } - if (c == '.') { - /* - * Internet format: - * a.b.c.d - * a.b.c (with c treated as 16 bits) - * a.b (with b treated as 24 bits) - */ - if (pp >= parts + 3) - return (0); - *pp++ = val; - c = *++cp; - } else - break; - } - /* - * Check for trailing characters. - */ - if (c != '\0' && (!isascii((unsigned char)c) || !isspace((unsigned char)c))) - return (0); - /* - * Concoct the address according to - * the number of parts specified. - */ - n = pp - parts + 1; - switch (n) { - - case 0: - return (0); /* initial nondigit */ - - case 1: /* a -- 32 bits */ - break; - - case 2: /* a.b -- 8.24 bits */ - if ((val > 0xffffff) || (parts[0] > 0xff)) - return (0); - val |= parts[0] << 24; - break; - - case 3: /* a.b.c -- 8.8.16 bits */ - if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff)) - return (0); - val |= (parts[0] << 24) | (parts[1] << 16); - break; - - case 4: /* a.b.c.d -- 8.8.8.8 bits */ - if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) - return (0); - val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); - break; - } - if (addr) - addr->s_addr = htonl(val); - return (1); -} - -#endif /* !defined(HAVE_INET_ATON) */ diff --git a/usr.sbin/nsd/compat/inet_ntop.c b/usr.sbin/nsd/compat/inet_ntop.c deleted file mode 100644 index 1361733ad08..00000000000 --- a/usr.sbin/nsd/compat/inet_ntop.c +++ /dev/null @@ -1,208 +0,0 @@ -/* From openssh 4.3p2 compat/inet_ntop.c */ -/* Copyright (c) 1996 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS - * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE - * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS - * SOFTWARE. - */ - -/* OPENBSD ORIGINAL: lib/libc/net/inet_ntop.c */ - -#include <config.h> - -#ifndef HAVE_INET_NTOP - -#include <sys/param.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <string.h> -#include <errno.h> -#include <stdio.h> - -#ifndef IN6ADDRSZ -#define IN6ADDRSZ 16 /* IPv6 T_AAAA */ -#endif - -#ifndef INT16SZ -#define INT16SZ 2 /* for systems without 16-bit ints */ -#endif - -/* - * WARNING: Don't even consider trying to compile this on a system where - * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. - */ - -static const char *inet_ntop4(const u_char *src, char *dst, size_t size); -static const char *inet_ntop6(const u_char *src, char *dst, size_t size); - -/* char * - * inet_ntop(af, src, dst, size) - * convert a network format address to presentation format. - * return: - * pointer to presentation format address (`dst'), or NULL (see errno). - * author: - * Paul Vixie, 1996. - */ -const char * -inet_ntop(int af, const void *src, char *dst, size_t size) -{ - switch (af) { - case AF_INET: - return (inet_ntop4(src, dst, size)); - case AF_INET6: - return (inet_ntop6(src, dst, size)); - default: - errno = EAFNOSUPPORT; - return (NULL); - } - /* NOTREACHED */ -} - -/* const char * - * inet_ntop4(src, dst, size) - * format an IPv4 address, more or less like inet_ntoa() - * return: - * `dst' (as a const) - * notes: - * (1) uses no statics - * (2) takes a u_char* not an in_addr as input - * author: - * Paul Vixie, 1996. - */ -static const char * -inet_ntop4(const u_char *src, char *dst, size_t size) -{ - static const char fmt[] = "%u.%u.%u.%u"; - char tmp[sizeof "255.255.255.255"]; - int l; - - l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]); - if (l <= 0 || l >= (int)size) { - errno = ENOSPC; - return (NULL); - } - strlcpy(dst, tmp, size); - return (dst); -} - -/* const char * - * inet_ntop6(src, dst, size) - * convert IPv6 binary address into presentation (printable) format - * author: - * Paul Vixie, 1996. - */ -static const char * -inet_ntop6(const u_char *src, char *dst, size_t size) -{ - /* - * Note that int32_t and int16_t need only be "at least" large enough - * to contain a value of the specified size. On some systems, like - * Crays, there is no such thing as an integer variable with 16 bits. - * Keep this in mind if you think this function should have been coded - * to use pointer overlays. All the world's not a VAX. - */ - char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; - char *tp, *ep; - struct { int base, len; } best, cur; - u_int words[IN6ADDRSZ / INT16SZ]; - int i; - int advance; - - /* - * Preprocess: - * Copy the input (bytewise) array into a wordwise array. - * Find the longest run of 0x00's in src[] for :: shorthanding. - */ - memset(words, '\0', sizeof words); - for (i = 0; i < IN6ADDRSZ; i++) - words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); - best.base = -1; - cur.base = -1; - for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { - if (words[i] == 0) { - if (cur.base == -1) - cur.base = i, cur.len = 1; - else - cur.len++; - } else { - if (cur.base != -1) { - if (best.base == -1 || cur.len > best.len) - best = cur; - cur.base = -1; - } - } - } - if (cur.base != -1) { - if (best.base == -1 || cur.len > best.len) - best = cur; - } - if (best.base != -1 && best.len < 2) - best.base = -1; - - /* - * Format the result. - */ - tp = tmp; - ep = tmp + sizeof(tmp); - for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) { - /* Are we inside the best run of 0x00's? */ - if (best.base != -1 && i >= best.base && - i < (best.base + best.len)) { - if (i == best.base) { - if (tp + 1 >= ep) - return (NULL); - *tp++ = ':'; - } - continue; - } - /* Are we following an initial run of 0x00s or any real hex? */ - if (i != 0) { - if (tp + 1 >= ep) - return (NULL); - *tp++ = ':'; - } - /* Is this address an encapsulated IPv4? */ - if (i == 6 && best.base == 0 && - (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { - if (!inet_ntop4(src+12, tp, (size_t)(ep - tp))) - return (NULL); - tp += strlen(tp); - break; - } - advance = snprintf(tp, ep - tp, "%x", words[i]); - if (advance <= 0 || advance >= ep - tp) - return (NULL); - tp += advance; - } - /* Was it a trailing run of 0x00's? */ - if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) { - if (tp + 1 >= ep) - return (NULL); - *tp++ = ':'; - } - if (tp + 1 >= ep) - return (NULL); - *tp++ = '\0'; - - /* - * Check for overflow, copy, and we're done. - */ - if ((size_t)(tp - tmp) > size) { - errno = ENOSPC; - return (NULL); - } - strlcpy(dst, tmp, size); - return (dst); -} - -#endif /* !HAVE_INET_NTOP */ diff --git a/usr.sbin/nsd/compat/inet_pton.c b/usr.sbin/nsd/compat/inet_pton.c deleted file mode 100644 index 91987917a49..00000000000 --- a/usr.sbin/nsd/compat/inet_pton.c +++ /dev/null @@ -1,226 +0,0 @@ -/* $KAME: inet_pton.c,v 1.5 2001/08/20 02:32:40 itojun Exp $ */ - -/* Copyright (c) 1996 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS - * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE - * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL - * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR - * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS - * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS - * SOFTWARE. - */ - -#include <config.h> - -#include <string.h> -#include <stdio.h> -#include <errno.h> - -/* - * WARNING: Don't even consider trying to compile this on a system where - * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. - */ - -static int inet_pton4 (const char *src, uint8_t *dst); -static int inet_pton6 (const char *src, uint8_t *dst); - -/* - * - * The definitions we might miss. - * - */ -#ifndef NS_INT16SZ -#define NS_INT16SZ 2 -#endif - -#ifndef NS_IN6ADDRSZ -#define NS_IN6ADDRSZ 16 -#endif - -#ifndef NS_INADDRSZ -#define NS_INADDRSZ 4 -#endif - -/* int - * inet_pton(af, src, dst) - * convert from presentation format (which usually means ASCII printable) - * to network format (which is usually some kind of binary format). - * return: - * 1 if the address was valid for the specified address family - * 0 if the address wasn't valid (`dst' is untouched in this case) - * -1 if some other error occurred (`dst' is untouched in this case, too) - * author: - * Paul Vixie, 1996. - */ -int -inet_pton(af, src, dst) - int af; - const char *src; - void *dst; -{ - switch (af) { - case AF_INET: - return (inet_pton4(src, dst)); - case AF_INET6: - return (inet_pton6(src, dst)); - default: - errno = EAFNOSUPPORT; - return (-1); - } - /* NOTREACHED */ -} - -/* int - * inet_pton4(src, dst) - * like inet_aton() but without all the hexadecimal and shorthand. - * return: - * 1 if `src' is a valid dotted quad, else 0. - * notice: - * does not touch `dst' unless it's returning 1. - * author: - * Paul Vixie, 1996. - */ -static int -inet_pton4(src, dst) - const char *src; - uint8_t *dst; -{ - static const char digits[] = "0123456789"; - int saw_digit, octets, ch; - uint8_t tmp[NS_INADDRSZ], *tp; - - saw_digit = 0; - octets = 0; - *(tp = tmp) = 0; - while ((ch = *src++) != '\0') { - const char *pch; - - if ((pch = strchr(digits, ch)) != NULL) { - uint32_t new = *tp * 10 + (pch - digits); - - if (new > 255) - return (0); - *tp = new; - if (! saw_digit) { - if (++octets > 4) - return (0); - saw_digit = 1; - } - } else if (ch == '.' && saw_digit) { - if (octets == 4) - return (0); - *++tp = 0; - saw_digit = 0; - } else - return (0); - } - if (octets < 4) - return (0); - - memcpy(dst, tmp, NS_INADDRSZ); - return (1); -} - -/* int - * inet_pton6(src, dst) - * convert presentation level address to network order binary form. - * return: - * 1 if `src' is a valid [RFC1884 2.2] address, else 0. - * notice: - * (1) does not touch `dst' unless it's returning 1. - * (2) :: in a full address is silently ignored. - * credit: - * inspired by Mark Andrews. - * author: - * Paul Vixie, 1996. - */ -static int -inet_pton6(src, dst) - const char *src; - uint8_t *dst; -{ - static const char xdigits_l[] = "0123456789abcdef", - xdigits_u[] = "0123456789ABCDEF"; - uint8_t tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; - const char *xdigits, *curtok; - int ch, saw_xdigit; - uint32_t val; - - memset((tp = tmp), '\0', NS_IN6ADDRSZ); - endp = tp + NS_IN6ADDRSZ; - colonp = NULL; - /* Leading :: requires some special handling. */ - if (*src == ':') - if (*++src != ':') - return (0); - curtok = src; - saw_xdigit = 0; - val = 0; - while ((ch = *src++) != '\0') { - const char *pch; - - if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) - pch = strchr((xdigits = xdigits_u), ch); - if (pch != NULL) { - val <<= 4; - val |= (pch - xdigits); - if (val > 0xffff) - return (0); - saw_xdigit = 1; - continue; - } - if (ch == ':') { - curtok = src; - if (!saw_xdigit) { - if (colonp) - return (0); - colonp = tp; - continue; - } - if (tp + NS_INT16SZ > endp) - return (0); - *tp++ = (uint8_t) (val >> 8) & 0xff; - *tp++ = (uint8_t) val & 0xff; - saw_xdigit = 0; - val = 0; - continue; - } - if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && - inet_pton4(curtok, tp) > 0) { - tp += NS_INADDRSZ; - saw_xdigit = 0; - break; /* '\0' was seen by inet_pton4(). */ - } - return (0); - } - if (saw_xdigit) { - if (tp + NS_INT16SZ > endp) - return (0); - *tp++ = (uint8_t) (val >> 8) & 0xff; - *tp++ = (uint8_t) val & 0xff; - } - if (colonp != NULL) { - /* - * Since some memmove()'s erroneously fail to handle - * overlapping regions, we'll do the shift by hand. - */ - const int n = tp - colonp; - int i; - - for (i = 1; i <= n; i++) { - endp[- i] = colonp[n - i]; - colonp[n - i] = 0; - } - tp = endp; - } - if (tp != endp) - return (0); - memcpy(dst, tmp, NS_IN6ADDRSZ); - return (1); -} diff --git a/usr.sbin/nsd/compat/malloc.c b/usr.sbin/nsd/compat/malloc.c deleted file mode 100644 index c32b3f9a359..00000000000 --- a/usr.sbin/nsd/compat/malloc.c +++ /dev/null @@ -1,22 +0,0 @@ -/* Just a replacement, if the original malloc is not - GNU-compliant. See autoconf documentation. */ - -#if HAVE_CONFIG_H -#include <config.h> -#endif -#undef malloc - -#include <sys/types.h> - -void *malloc (); - -/* Allocate an N-byte block of memory from the heap. - If N is zero, allocate a 1-byte block. */ - -void * -rpl_malloc (size_t n) -{ - if (n == 0) - n = 1; - return malloc (n); -} diff --git a/usr.sbin/nsd/compat/memcmp.c b/usr.sbin/nsd/compat/memcmp.c deleted file mode 100644 index 9446276f410..00000000000 --- a/usr.sbin/nsd/compat/memcmp.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * memcmp.c: memcmp compat implementation. - * - * Copyright (c) 2010, NLnet Labs. All rights reserved. - * - * See LICENSE for the license. -*/ - -#include <config.h> - -int memcmp(const void *x, const void *y, size_t n); - -int memcmp(const void *x, const void *y, size_t n) -{ - const uint8_t* x8 = (const uint8_t*)x; - const uint8_t* y8 = (const uint8_t*)y; - size_t i; - for(i=0; i<n; i++) { - if(x8[i] < y8[i]) - return -1; - else if(x8[i] > y8[i]) - return 1; - } - return 0; -} diff --git a/usr.sbin/nsd/compat/memcmp.h b/usr.sbin/nsd/compat/memcmp.h deleted file mode 100644 index c1d195ccf4a..00000000000 --- a/usr.sbin/nsd/compat/memcmp.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * memcmp.h: undef memcmp for compat. - * - * Copyright (c) 2012, NLnet Labs. All rights reserved. - * - * See LICENSE for the license. -*/ -#ifndef COMPAT_MEMCMP_H -#define COMPAT_MEMCMP_H - -#ifdef memcmp -/* undef here otherwise autoheader messes it up in config.h */ -# undef memcmp -#endif - -#endif /* COMPAT_MEMCMP_H */ diff --git a/usr.sbin/nsd/compat/memmove.c b/usr.sbin/nsd/compat/memmove.c deleted file mode 100644 index 0035bbf7533..00000000000 --- a/usr.sbin/nsd/compat/memmove.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * memmove.c: memmove compat implementation. - * - * Copyright (c) 2001-2006, NLnet Labs. All rights reserved. - * - * See LICENSE for the license. -*/ - -#include <config.h> -#include <stdlib.h> - -void *memmove(void *dest, const void *src, size_t n); - -void *memmove(void *dest, const void *src, size_t n) -{ - uint8_t* from = (uint8_t*) src; - uint8_t* to = (uint8_t*) dest; - - if (from == to || n == 0) - return dest; - if (to > from && to-from < (int)n) { - /* to overlaps with from */ - /* <from......> */ - /* <to........> */ - /* copy in reverse, to avoid overwriting from */ - int i; - for(i=n-1; i>=0; i--) - to[i] = from[i]; - return dest; - } - if (from > to && from-to < (int)n) { - /* to overlaps with from */ - /* <from......> */ - /* <to........> */ - /* copy forwards, to avoid overwriting from */ - size_t i; - for(i=0; i<n; i++) - to[i] = from[i]; - return dest; - } - memcpy(dest, src, n); - return dest; -} diff --git a/usr.sbin/nsd/compat/pselect.c b/usr.sbin/nsd/compat/pselect.c deleted file mode 100644 index 524cf2837a0..00000000000 --- a/usr.sbin/nsd/compat/pselect.c +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Like select(2) but set the signals to block while waiting in - * select. This version is not entirely race condition safe. Only - * operating system support can make it so. - */ - -#include <config.h> - -#include <sys/time.h> -#include <sys/types.h> -#ifdef HAVE_SYS_SELECT_H -#include <sys/select.h> -#endif -#include <unistd.h> -#include <signal.h> - -int -pselect (int n, - fd_set *readfds, - fd_set *writefds, - fd_set *exceptfds, - const struct timespec *timeout, - const sigset_t *sigmask) -{ - int result; - sigset_t saved_sigmask; - struct timeval saved_timeout; - - if (sigmask && sigprocmask(SIG_SETMASK, sigmask, &saved_sigmask) == -1) - return -1; - - if (timeout) { - saved_timeout.tv_sec = timeout->tv_sec; - saved_timeout.tv_usec = timeout->tv_nsec / 1000; - result = select(n, readfds, writefds, exceptfds, &saved_timeout); - } else { - result = select(n, readfds, writefds, exceptfds, NULL); - } - - if (sigmask && sigprocmask(SIG_SETMASK, &saved_sigmask, NULL) == -1) - return -1; - - return result; -} diff --git a/usr.sbin/nsd/compat/snprintf.c b/usr.sbin/nsd/compat/snprintf.c deleted file mode 100644 index 0663557037f..00000000000 --- a/usr.sbin/nsd/compat/snprintf.c +++ /dev/null @@ -1,1036 +0,0 @@ -/* snprintf - compatibility implementation of snprintf, vsnprintf - * - * Copyright (c) 2013, NLnet Labs. All rights reserved. - * - * This software is open source. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * Neither the name of the NLNET LABS nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "config.h" -#include <stdio.h> -#include <ctype.h> -#include <string.h> -#include <stdarg.h> -#include <stdlib.h> -#include <errno.h> -#ifdef HAVE_STDINT_H -#include <stdint.h> -#endif - -/* for test */ -/* #define SNPRINTF_TEST 1 */ -#ifdef SNPRINTF_TEST -#define snprintf my_snprintf -#define vsnprintf my_vsnprintf -#endif /* SNPRINTF_TEST */ - -int snprintf(char* str, size_t size, const char* format, ...); -int vsnprintf(char* str, size_t size, const char* format, va_list arg); - -/** - * Very portable snprintf implementation, limited in functionality, - * esp. for %[capital] %[nonportable] and so on. Reduced float functionality, - * mostly in formatting and range (e+-16), for %f and %g. - * - * %s, %d, %u, %i, %x, %c, %n and %% are fully supported. - * This includes width, precision, flags 0- +, and *(arg for wid,prec). - * %f, %g, %m, %p have reduced support, support for wid,prec,flags,*, but - * less floating point range, no %e formatting for %g. - */ -int snprintf(char* str, size_t size, const char* format, ...) -{ - int r; - va_list args; - va_start(args, format); - r = vsnprintf(str, size, format, args); - va_end(args); - return r; -} - -/** add padding to string */ -static void -print_pad(char** at, size_t* left, int* ret, char p, int num) -{ - while(num--) { - if(*left > 1) { - *(*at)++ = p; - (*left)--; - } - (*ret)++; - } -} - -/** get negative symbol, 0 if none */ -static char -get_negsign(int negative, int plus, int space) -{ - if(negative) - return '-'; - if(plus) - return '+'; - if(space) - return ' '; - return 0; -} - -#define PRINT_DEC_BUFSZ 32 /* 20 is enough for 64 bit decimals */ -/** print decimal into buffer, returns length */ -static int -print_dec(char* buf, int max, unsigned int value) -{ - int i = 0; - if(value == 0) { - if(max > 0) { - buf[0] = '0'; - i = 1; - } - } else while(value && i < max) { - buf[i++] = '0' + value % 10; - value /= 10; - } - return i; -} - -/** print long decimal into buffer, returns length */ -static int -print_dec_l(char* buf, int max, unsigned long value) -{ - int i = 0; - if(value == 0) { - if(max > 0) { - buf[0] = '0'; - i = 1; - } - } else while(value && i < max) { - buf[i++] = '0' + value % 10; - value /= 10; - } - return i; -} - -/** print long decimal into buffer, returns length */ -static int -print_dec_ll(char* buf, int max, unsigned long long value) -{ - int i = 0; - if(value == 0) { - if(max > 0) { - buf[0] = '0'; - i = 1; - } - } else while(value && i < max) { - buf[i++] = '0' + value % 10; - value /= 10; - } - return i; -} - -/** print hex into buffer, returns length */ -static int -print_hex(char* buf, int max, unsigned int value) -{ - const char* h = "0123456789abcdef"; - int i = 0; - if(value == 0) { - if(max > 0) { - buf[0] = '0'; - i = 1; - } - } else while(value && i < max) { - buf[i++] = h[value & 0x0f]; - value >>= 4; - } - return i; -} - -/** print long hex into buffer, returns length */ -static int -print_hex_l(char* buf, int max, unsigned long value) -{ - const char* h = "0123456789abcdef"; - int i = 0; - if(value == 0) { - if(max > 0) { - buf[0] = '0'; - i = 1; - } - } else while(value && i < max) { - buf[i++] = h[value & 0x0f]; - value >>= 4; - } - return i; -} - -/** print long long hex into buffer, returns length */ -static int -print_hex_ll(char* buf, int max, unsigned long long value) -{ - const char* h = "0123456789abcdef"; - int i = 0; - if(value == 0) { - if(max > 0) { - buf[0] = '0'; - i = 1; - } - } else while(value && i < max) { - buf[i++] = h[value & 0x0f]; - value >>= 4; - } - return i; -} - -/** copy string into result, reversed */ -static void -spool_str_rev(char** at, size_t* left, int* ret, const char* buf, int len) -{ - int i = len; - while(i) { - if(*left > 1) { - *(*at)++ = buf[--i]; - (*left)--; - } else --i; - (*ret)++; - } -} - -/** copy string into result */ -static void -spool_str(char** at, size_t* left, int* ret, const char* buf, int len) -{ - int i; - for(i=0; i<len; i++) { - if(*left > 1) { - *(*at)++ = buf[i]; - (*left)--; - } - (*ret)++; - } -} - -/** print number formatted */ -static void -print_num(char** at, size_t* left, int* ret, int minw, int precision, - int prgiven, int zeropad, int minus, int plus, int space, - int zero, int negative, char* buf, int len) -{ - int w = len; /* excludes minus sign */ - char s = get_negsign(negative, plus, space); - if(minus) { - /* left adjust the number into the field, space padding */ - /* calc numw = [sign][zeroes][number] */ - int numw = w; - if(precision == 0 && zero) numw = 0; - if(numw < precision) numw = precision; - if(s) numw++; - - /* sign */ - if(s) print_pad(at, left, ret, s, 1); - - /* number */ - if(precision == 0 && zero) { - /* "" for the number */ - } else { - if(w < precision) - print_pad(at, left, ret, '0', precision - w); - spool_str_rev(at, left, ret, buf, len); - } - /* spaces */ - if(numw < minw) - print_pad(at, left, ret, ' ', minw - numw); - } else { - /* pad on the left of the number */ - /* calculate numw has width of [sign][zeroes][number] */ - int numw = w; - if(precision == 0 && zero) numw = 0; - if(numw < precision) numw = precision; - if(!prgiven && zeropad && numw < minw) numw = minw; - else if(s) numw++; - - /* pad with spaces */ - if(numw < minw) - print_pad(at, left, ret, ' ', minw - numw); - /* print sign (and one less zeropad if so) */ - if(s) { - print_pad(at, left, ret, s, 1); - numw--; - } - /* pad with zeroes */ - if(w < numw) - print_pad(at, left, ret, '0', numw - w); - if(precision == 0 && zero) - return; - /* print the characters for the value */ - spool_str_rev(at, left, ret, buf, len); - } -} - -/** print %d and %i */ -static void -print_num_d(char** at, size_t* left, int* ret, int value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = (value < 0); - int zero = (value == 0); - int len = print_dec(buf, (int)sizeof(buf), - (unsigned int)(negative?-value:value)); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %ld and %li */ -static void -print_num_ld(char** at, size_t* left, int* ret, long value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = (value < 0); - int zero = (value == 0); - int len = print_dec_l(buf, (int)sizeof(buf), - (unsigned long)(negative?-value:value)); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %lld and %lli */ -static void -print_num_lld(char** at, size_t* left, int* ret, long long value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = (value < 0); - int zero = (value == 0); - int len = print_dec_ll(buf, (int)sizeof(buf), - (unsigned long long)(negative?-value:value)); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %u */ -static void -print_num_u(char** at, size_t* left, int* ret, unsigned int value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = 0; - int zero = (value == 0); - int len = print_dec(buf, (int)sizeof(buf), value); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %lu */ -static void -print_num_lu(char** at, size_t* left, int* ret, unsigned long value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = 0; - int zero = (value == 0); - int len = print_dec_l(buf, (int)sizeof(buf), value); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %llu */ -static void -print_num_llu(char** at, size_t* left, int* ret, unsigned long long value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = 0; - int zero = (value == 0); - int len = print_dec_ll(buf, (int)sizeof(buf), value); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %x */ -static void -print_num_x(char** at, size_t* left, int* ret, unsigned int value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = 0; - int zero = (value == 0); - int len = print_hex(buf, (int)sizeof(buf), value); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %lx */ -static void -print_num_lx(char** at, size_t* left, int* ret, unsigned long value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = 0; - int zero = (value == 0); - int len = print_hex_l(buf, (int)sizeof(buf), value); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %llx */ -static void -print_num_llx(char** at, size_t* left, int* ret, unsigned long long value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = 0; - int zero = (value == 0); - int len = print_hex_ll(buf, (int)sizeof(buf), value); - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/** print %llp */ -static void -print_num_llp(char** at, size_t* left, int* ret, void* value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_DEC_BUFSZ]; - int negative = 0; - int zero = (value == 0); -#if defined(UINTPTR_MAX) && defined(UINT32_MAX) && (UINTPTR_MAX == UINT32_MAX) - /* avoid warning about upcast on 32bit systems */ - unsigned long long llvalue = (unsigned long)value; -#else - unsigned long long llvalue = (unsigned long long)value; -#endif - int len = print_hex_ll(buf, (int)sizeof(buf), llvalue); - if(zero) { - buf[0]=')'; - buf[1]='l'; - buf[2]='i'; - buf[3]='n'; - buf[4]='('; - len = 5; - } else { - /* put '0x' in front of the (reversed) buffer result */ - if(len < PRINT_DEC_BUFSZ) - buf[len++] = 'x'; - if(len < PRINT_DEC_BUFSZ) - buf[len++] = '0'; - } - print_num(at, left, ret, minw, precision, prgiven, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -#define PRINT_FLOAT_BUFSZ 64 /* xx.yy with 20.20 about the max */ -/** spool remainder after the decimal point to buffer, in reverse */ -static int -print_remainder(char* buf, int max, double r, int prec) -{ - unsigned long long cap = 1; - unsigned long long value; - int len, i; - if(prec > 19) prec = 19; /* max we can do */ - if(max < prec) return 0; - for(i=0; i<prec; i++) { - cap *= 10; - } - r *= (double)cap; - value = (unsigned long long)r; - /* see if we need to round up */ - if(((unsigned long long)((r - (double)value)*10.0)) >= 5) { - value++; - /* that might carry to numbers before the comma, if so, - * just ignore that rounding. failure because 64bitprintout */ - if(value >= cap) - value = cap-1; - } - len = print_dec_ll(buf, max, value); - while(len < prec) { /* pad with zeroes, e.g. if 0.0012 */ - buf[len++] = '0'; - } - if(len < max) - buf[len++] = '.'; - return len; -} - -/** spool floating point to buffer */ -static int -print_float(char* buf, int max, double value, int prec) -{ - /* as xxx.xxx if prec==0, no '.', with prec decimals after . */ - /* no conversion for NAN and INF, because we do not want to require - linking with -lm. */ - /* Thus, the conversions use 64bit integers to convert the numbers, - * which makes 19 digits before and after the decimal point the max */ - unsigned long long whole = (unsigned long long)value; - double remain = value - (double)whole; - int len = 0; - if(prec != 0) - len = print_remainder(buf, max, remain, prec); - len += print_dec_ll(buf+len, max-len, whole); - return len; -} - -/** print %f */ -static void -print_num_f(char** at, size_t* left, int* ret, double value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_FLOAT_BUFSZ]; - int negative = (value < 0); - int zero = 0; - int len; - if(!prgiven) precision = 6; - len = print_float(buf, (int)sizeof(buf), negative?-value:value, - precision); - print_num(at, left, ret, minw, 1, 0, zeropad, minus, - plus, space, zero, negative, buf, len); -} - -/* rudimentary %g support */ -static int -print_float_g(char* buf, int max, double value, int prec) -{ - unsigned long long whole = (unsigned long long)value; - double remain = value - (double)whole; - int before = 0; - int len = 0; - - /* number of digits before the decimal point */ - while(whole > 0) { - before++; - whole /= 10; - } - whole = (unsigned long long)value; - - if(prec > before && remain != 0.0) { - /* see if the last decimals are zero, if so, skip them */ - len = print_remainder(buf, max, remain, prec-before); - while(len > 0 && buf[0]=='0') { - memmove(buf, buf+1, --len); - } - } - len += print_dec_ll(buf+len, max-len, whole); - return len; -} - - -/** print %g */ -static void -print_num_g(char** at, size_t* left, int* ret, double value, - int minw, int precision, int prgiven, int zeropad, int minus, - int plus, int space) -{ - char buf[PRINT_FLOAT_BUFSZ]; - int negative = (value < 0); - int zero = 0; - int len; - if(!prgiven) precision = 6; - if(precision == 0) precision = 1; - len = print_float_g(buf, (int)sizeof(buf), negative?-value:value, - precision); - print_num(at, left, ret, minw, 1, 0, zeropad, minus, - plus, space, zero, negative, buf, len); -} - - -/** strnlen (compat implementation) */ -static int -my_strnlen(const char* s, int max) -{ - int i; - for(i=0; i<max; i++) - if(s[i]==0) - return i; - return max; -} - -/** print %s */ -static void -print_str(char** at, size_t* left, int* ret, char* s, - int minw, int precision, int prgiven, int minus) -{ - int w; - /* with prec: no more than x characters from this string, stop at 0 */ - if(prgiven) - w = my_strnlen(s, precision); - else w = (int)strlen(s); /* up to the nul */ - if(w < minw && !minus) - print_pad(at, left, ret, ' ', minw - w); - spool_str(at, left, ret, s, w); - if(w < minw && minus) - print_pad(at, left, ret, ' ', minw - w); -} - -/** print %c */ -static void -print_char(char** at, size_t* left, int* ret, int c, - int minw, int minus) -{ - if(1 < minw && !minus) - print_pad(at, left, ret, ' ', minw - 1); - print_pad(at, left, ret, c, 1); - if(1 < minw && minus) - print_pad(at, left, ret, ' ', minw - 1); -} - - -/** - * Print to string. - * str: string buffer for result. result will be null terminated. - * size: size of the buffer. null is put inside buffer. - * format: printf format string. - * arg: '...' arguments to print. - * returns number of characters. a null is printed after this. - * return number of bytes that would have been written - * if the buffer had been large enough. - * - * supported format specifiers: - * %s, %u, %d, %x, %i, %f, %g, %c, %p, %n. - * length: l, ll (for d, u, x). - * precision: 6.6d (for d, u, x) - * %f, %g precisions, 0.3f - * %20s, '.*s' - * and %%. - */ -int vsnprintf(char* str, size_t size, const char* format, va_list arg) -{ - char* at = str; - size_t left = size; - int ret = 0; - const char* fmt = format; - int conv, minw, precision, prgiven, zeropad, minus, plus, space, length; - while(*fmt) { - /* copy string before % */ - while(*fmt && *fmt!='%') { - if(left > 1) { - *at++ = *fmt++; - left--; - } else fmt++; - ret++; - } - - /* see if we are at end */ - if(!*fmt) break; - - /* fetch next argument % designation from format string */ - fmt++; /* skip the '%' */ - - /********************************/ - /* get the argument designation */ - /********************************/ - /* we must do this vararg stuff inside this function for - * portability. Hence, get_designation, and print_designation - * are not their own functions. */ - - /* printout designation: - * conversion specifier: x, d, u, s, c, n, m, p - * flags: # not supported - * 0 zeropad (on the left) - * - left adjust (right by default) - * ' ' printspace for positive number (in - position). - * + alwayssign - * fieldwidth: [1-9][0-9]* minimum field width. - * if this is * then type int next argument specifies the minwidth. - * if this is negative, the - flag is set (with positive width). - * precision: period[digits]*, %.2x. - * if this is * then type int next argument specifies the precision. - * just '.' or negative value means precision=0. - * this is mindigits to print for d, i, u, x - * this is aftercomma digits for f - * this is max number significant digits for g - * maxnumber characters to be printed for s - * length: 0-none (int), 1-l (long), 2-ll (long long) - * notsupported: hh (char), h (short), L (long double), q, j, z, t - * Does not support %m$ and *m$ argument designation as array indices. - * Does not support %#x - * - */ - minw = 0; - precision = 1; - prgiven = 0; - zeropad = 0; - minus = 0; - plus = 0; - space = 0; - length = 0; - - /* get flags in any order */ - for(;;) { - if(*fmt == '0') - zeropad = 1; - else if(*fmt == '-') - minus = 1; - else if(*fmt == '+') - plus = 1; - else if(*fmt == ' ') - space = 1; - else break; - fmt++; - } - - /* field width */ - if(*fmt == '*') { - fmt++; /* skip char */ - minw = va_arg(arg, int); - if(minw < 0) { - minus = 1; - minw = -minw; - } - } else while(*fmt >= '0' && *fmt <= '9') { - minw = minw*10 + (*fmt++)-'0'; - } - - /* precision */ - if(*fmt == '.') { - fmt++; /* skip period */ - prgiven = 1; - precision = 0; - if(*fmt == '*') { - fmt++; /* skip char */ - precision = va_arg(arg, int); - if(precision < 0) - precision = 0; - } else while(*fmt >= '0' && *fmt <= '9') { - precision = precision*10 + (*fmt++)-'0'; - } - } - - /* length */ - if(*fmt == 'l') { - fmt++; /* skip char */ - length = 1; - if(*fmt == 'l') { - fmt++; /* skip char */ - length = 2; - } - } - - /* get the conversion */ - if(!*fmt) conv = 0; - else conv = *fmt++; - - /***********************************/ - /* print that argument designation */ - /***********************************/ - switch(conv) { - case 'i': - case 'd': - if(length == 0) - print_num_d(&at, &left, &ret, va_arg(arg, int), - minw, precision, prgiven, zeropad, minus, plus, space); - else if(length == 1) - print_num_ld(&at, &left, &ret, va_arg(arg, long), - minw, precision, prgiven, zeropad, minus, plus, space); - else if(length == 2) - print_num_lld(&at, &left, &ret, - va_arg(arg, long long), - minw, precision, prgiven, zeropad, minus, plus, space); - break; - case 'u': - if(length == 0) - print_num_u(&at, &left, &ret, - va_arg(arg, unsigned int), - minw, precision, prgiven, zeropad, minus, plus, space); - else if(length == 1) - print_num_lu(&at, &left, &ret, - va_arg(arg, unsigned long), - minw, precision, prgiven, zeropad, minus, plus, space); - else if(length == 2) - print_num_llu(&at, &left, &ret, - va_arg(arg, unsigned long long), - minw, precision, prgiven, zeropad, minus, plus, space); - break; - case 'x': - if(length == 0) - print_num_x(&at, &left, &ret, - va_arg(arg, unsigned int), - minw, precision, prgiven, zeropad, minus, plus, space); - else if(length == 1) - print_num_lx(&at, &left, &ret, - va_arg(arg, unsigned long), - minw, precision, prgiven, zeropad, minus, plus, space); - else if(length == 2) - print_num_llx(&at, &left, &ret, - va_arg(arg, unsigned long long), - minw, precision, prgiven, zeropad, minus, plus, space); - break; - case 's': - print_str(&at, &left, &ret, va_arg(arg, char*), - minw, precision, prgiven, minus); - break; - case 'c': - print_char(&at, &left, &ret, va_arg(arg, int), - minw, minus); - break; - case 'n': - *va_arg(arg, int*) = ret; - break; - case 'm': - print_str(&at, &left, &ret, strerror(errno), - minw, precision, prgiven, minus); - break; - case 'p': - print_num_llp(&at, &left, &ret, va_arg(arg, void*), - minw, precision, prgiven, zeropad, minus, plus, space); - break; - case '%': - print_pad(&at, &left, &ret, '%', 1); - break; - case 'f': - print_num_f(&at, &left, &ret, va_arg(arg, double), - minw, precision, prgiven, zeropad, minus, plus, space); - break; - case 'g': - print_num_g(&at, &left, &ret, va_arg(arg, double), - minw, precision, prgiven, zeropad, minus, plus, space); - break; - /* unknown */ - default: - case 0: break; - } - } - - /* zero terminate */ - if(left > 0) - *at = 0; - return ret; -} - -#ifdef SNPRINTF_TEST - -/** do tests */ -#undef snprintf -#define DOTEST(bufsz, result, retval, ...) do { \ - char buf[bufsz]; \ - printf("now test %s\n", #__VA_ARGS__); \ - int r=my_snprintf(buf, sizeof(buf), __VA_ARGS__); \ - if(r != retval || strcmp(buf, result) != 0) { \ - printf("error test(%s) was \"%s\":%d\n", \ - ""#bufsz", "#result", "#retval", "#__VA_ARGS__, \ - buf, r); \ - exit(1); \ - } \ - r=snprintf(buf, sizeof(buf), __VA_ARGS__); \ - if(r != retval || strcmp(buf, result) != 0) { \ - printf("error test(%s) differs with system, \"%s\":%d\n", \ - ""#bufsz", "#result", "#retval", "#__VA_ARGS__, \ - buf, r); \ - exit(1); \ - } \ - printf("test(\"%s\":%d) passed\n", buf, r); \ - } while(0); - -/** test program */ -int main(void) -{ - int x = 0; - - /* bufsize, expectedstring, expectedretval, snprintf arguments */ - DOTEST(1024, "hello", 5, "hello"); - DOTEST(1024, "h", 1, "h"); - /* warning from gcc for format string, but it does work - * DOTEST(1024, "", 0, ""); */ - - DOTEST(3, "he", 5, "hello"); - DOTEST(1, "", 7, "%d", 7823089); - - /* test positive numbers */ - DOTEST(1024, "0", 1, "%d", 0); - DOTEST(1024, "1", 1, "%d", 1); - DOTEST(1024, "9", 1, "%d", 9); - DOTEST(1024, "15", 2, "%d", 15); - DOTEST(1024, "ab15cd", 6, "ab%dcd", 15); - DOTEST(1024, "167", 3, "%d", 167); - DOTEST(1024, "7823089", 7, "%d", 7823089); - DOTEST(1024, " 12", 3, "%3d", 12); - DOTEST(1024, "012", 3, "%.3d", 12); - DOTEST(1024, "012", 3, "%3.3d", 12); - DOTEST(1024, "012", 3, "%03d", 12); - DOTEST(1024, " 012", 4, "%4.3d", 12); - DOTEST(1024, "", 0, "%.0d", 0); - - /* test negative numbers */ - DOTEST(1024, "-1", 2, "%d", -1); - DOTEST(1024, "-12", 3, "%3d", -12); - DOTEST(1024, " -2", 3, "%3d", -2); - DOTEST(1024, "-012", 4, "%.3d", -12); - DOTEST(1024, "-012", 4, "%3.3d", -12); - DOTEST(1024, "-012", 4, "%4.3d", -12); - DOTEST(1024, " -012", 5, "%5.3d", -12); - DOTEST(1024, "-12", 3, "%03d", -12); - DOTEST(1024, "-02", 3, "%03d", -2); - DOTEST(1024, "-15", 3, "%d", -15); - DOTEST(1024, "-7307", 5, "%d", -7307); - DOTEST(1024, "-12 ", 5, "%-5d", -12); - DOTEST(1024, "-00012", 6, "%-.5d", -12); - - /* test + and space flags */ - DOTEST(1024, "+12", 3, "%+d", 12); - DOTEST(1024, " 12", 3, "% d", 12); - - /* test %u */ - DOTEST(1024, "12", 2, "%u", 12); - DOTEST(1024, "0", 1, "%u", 0); - DOTEST(1024, "4294967295", 10, "%u", 0xffffffff); - - /* test %x */ - DOTEST(1024, "0", 1, "%x", 0); - DOTEST(1024, "c", 1, "%x", 12); - DOTEST(1024, "12ab34cd", 8, "%x", 0x12ab34cd); - - /* test %llu, %lld */ - DOTEST(1024, "18446744073709551615", 20, "%llu", - (long long)0xffffffffffffffff); - DOTEST(1024, "-9223372036854775808", 20, "%lld", - (long long)0x8000000000000000); - DOTEST(1024, "9223372036854775808", 19, "%llu", - (long long)0x8000000000000000); - - /* test %s */ - DOTEST(1024, "hello", 5, "%s", "hello"); - DOTEST(1024, " hello", 10, "%10s", "hello"); - DOTEST(1024, "hello ", 10, "%-10s", "hello"); - DOTEST(1024, "he", 2, "%.2s", "hello"); - DOTEST(1024, " he", 4, "%4.2s", "hello"); - DOTEST(1024, " h", 4, "%4.2s", "h"); - - /* test %c */ - DOTEST(1024, "a", 1, "%c", 'a'); - /* warning from gcc for format string, but it does work - DOTEST(1024, " a", 5, "%5c", 'a'); - DOTEST(1024, "a", 1, "%.0c", 'a'); */ - - /* test %n */ - DOTEST(1024, "hello", 5, "hello%n", &x); - if(x != 5) { printf("the %%n failed\n"); exit(1); } - - /* test %m */ - errno = 0; - DOTEST(1024, "Success", 7, "%m"); - - /* test %p */ - DOTEST(1024, "0x10", 4, "%p", (void*)0x10); - DOTEST(1024, "(nil)", 5, "%p", (void*)0x0); - - /* test %% */ - DOTEST(1024, "%", 1, "%%"); - - /* test %f */ - DOTEST(1024, "0.000000", 8, "%f", 0.0); - DOTEST(1024, "0.00", 4, "%.2f", 0.0); - /* differs, "-0.00" DOTEST(1024, "0.00", 4, "%.2f", -0.0); */ - DOTEST(1024, "234.00", 6, "%.2f", 234.005); - DOTEST(1024, "8973497.1246", 12, "%.4f", 8973497.12456); - DOTEST(1024, "-12.000000", 10, "%f", -12.0); - DOTEST(1024, "6", 1, "%.0f", 6.0); - - DOTEST(1024, "6", 1, "%g", 6.0); - DOTEST(1024, "6.1", 3, "%g", 6.1); - DOTEST(1024, "6.15", 4, "%g", 6.15); - - /* These format strings are from the code of NSD, Unbound, ldns */ - - DOTEST(1024, "abcdef", 6, "%s", "abcdef"); - DOTEST(1024, "005", 3, "%03u", 5); - DOTEST(1024, "12345", 5, "%03u", 12345); - DOTEST(1024, "5", 1, "%d", 5); - DOTEST(1024, "(nil)", 5, "%p", NULL); - DOTEST(1024, "12345", 5, "%ld", (long)12345); - DOTEST(1024, "12345", 5, "%lu", (long)12345); - DOTEST(1024, " 12345", 12, "%12u", (unsigned)12345); - DOTEST(1024, "12345", 5, "%u", (unsigned)12345); - DOTEST(1024, "12345", 5, "%llu", (unsigned long long)12345); - DOTEST(1024, "12345", 5, "%x", 0x12345); - DOTEST(1024, "12345", 5, "%llx", (long long)0x12345); - DOTEST(1024, "012345", 6, "%6.6d", 12345); - DOTEST(1024, "012345", 6, "%6.6u", 12345); - DOTEST(1024, "1234.54", 7, "%g", 1234.54); - DOTEST(1024, "123456789.54", 12, "%.12g", 123456789.54); - DOTEST(1024, "3456789123456.54", 16, "%.16g", 3456789123456.54); - /* %24g does not work with 24 digits, not enough accuracy, - * the first 16 digits are correct */ - DOTEST(1024, "12345", 5, "%3.3d", 12345); - DOTEST(1024, "000", 3, "%3.3d", 0); - DOTEST(1024, "001", 3, "%3.3d", 1); - DOTEST(1024, "012", 3, "%3.3d", 12); - DOTEST(1024, "-012", 4, "%3.3d", -12); - DOTEST(1024, "he", 2, "%.2s", "hello"); - DOTEST(1024, "helloworld", 10, "%s%s", "hello", "world"); - DOTEST(1024, "he", 2, "%.*s", 2, "hello"); - DOTEST(1024, " hello", 7, "%*s", 7, "hello"); - DOTEST(1024, "hello ", 7, "%*s", -7, "hello"); - DOTEST(1024, "0", 1, "%c", '0'); - DOTEST(1024, "A", 1, "%c", 'A'); - DOTEST(1024, "", 1, "%c", 0); - DOTEST(1024, "\010", 1, "%c", 8); - DOTEST(1024, "%", 1, "%%"); - DOTEST(1024, "0a", 2, "%02x", 0x0a); - DOTEST(1024, "bd", 2, "%02x", 0xbd); - DOTEST(1024, "12", 2, "%02ld", (long)12); - DOTEST(1024, "02", 2, "%02ld", (long)2); - DOTEST(1024, "02", 2, "%02u", (unsigned)2); - DOTEST(1024, "765432", 6, "%05u", (unsigned)765432); - DOTEST(1024, "10.234", 6, "%0.3f", 10.23421); - DOTEST(1024, "123456.234", 10, "%0.3f", 123456.23421); - DOTEST(1024, "123456789.234", 13, "%0.3f", 123456789.23421); - DOTEST(1024, "123456.23", 9, "%.2f", 123456.23421); - DOTEST(1024, "123456", 6, "%.0f", 123456.23421); - DOTEST(1024, "0123", 4, "%.4x", 0x0123); - DOTEST(1024, "00000123", 8, "%.8x", 0x0123); - DOTEST(1024, "ffeb0cde", 8, "%.8x", 0xffeb0cde); - DOTEST(1024, " 987654321", 10, "%10lu", (unsigned long)987654321); - DOTEST(1024, " 987654321", 12, "%12lu", (unsigned long)987654321); - DOTEST(1024, "987654321", 9, "%i", 987654321); - DOTEST(1024, "-87654321", 9, "%i", -87654321); - DOTEST(1024, "hello ", 16, "%-16s", "hello"); - DOTEST(1024, " ", 16, "%-16s", ""); - DOTEST(1024, "a ", 16, "%-16s", "a"); - DOTEST(1024, "foobarfoobar ", 16, "%-16s", "foobarfoobar"); - DOTEST(1024, "foobarfoobarfoobar", 18, "%-16s", "foobarfoobarfoobar"); - - /* combined expressions */ - DOTEST(1024, "foo 1.0 size 512 edns", 21, - "foo %s size %d %s%s", "1.0", 512, "", "edns"); - DOTEST(15, "foo 1.0 size 5", 21, - "foo %s size %d %s%s", "1.0", 512, "", "edns"); - DOTEST(1024, "packet 1203ceff id", 18, - "packet %2.2x%2.2x%2.2x%2.2x id", 0x12, 0x03, 0xce, 0xff); - DOTEST(1024, "/tmp/testbound_123abcd.tmp", 26, "/tmp/testbound_%u%s%s.tmp", 123, "ab", "cd"); - - return 0; -} -#endif /* SNPRINTF_TEST */ diff --git a/usr.sbin/nsd/compat/strlcat.c b/usr.sbin/nsd/compat/strlcat.c deleted file mode 100644 index cc5a5918d2e..00000000000 --- a/usr.sbin/nsd/compat/strlcat.c +++ /dev/null @@ -1,73 +0,0 @@ -/* compat/strlcat.c */ - -/*- - * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */ - -#include <config.h> -#ifndef HAVE_STRLCAT - -#include <sys/types.h> -#include <string.h> - -/* - * Appends src to string dst of size siz (unlike strncat, siz is the - * full size of dst, not space left). At most siz-1 characters - * will be copied. Always NUL terminates (unless siz <= strlen(dst)). - * Returns strlen(src) + MIN(siz, strlen(initial dst)). - * If retval >= siz, truncation occurred. - */ -size_t -strlcat(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - size_t dlen; - - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = siz - dlen; - - if (n == 0) - return(dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - *d = '\0'; - - return(dlen + (s - src)); /* count does not include NUL */ -} - -#endif /* !HAVE_STRLCAT */ diff --git a/usr.sbin/nsd/compat/strlcpy.c b/usr.sbin/nsd/compat/strlcpy.c deleted file mode 100644 index acd306a151c..00000000000 --- a/usr.sbin/nsd/compat/strlcpy.c +++ /dev/null @@ -1,57 +0,0 @@ -/* from openssh 4.3p2 compat/strlcpy.c */ -/* - * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */ - -#include <config.h> -#ifndef HAVE_STRLCPY - -#include <sys/types.h> -#include <string.h> - -/* - * Copy src to string dst of size siz. At most siz-1 characters - * will be copied. Always NUL terminates (unless siz == 0). - * Returns strlen(src); if retval >= siz, truncation occurred. - */ -size_t -strlcpy(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - - /* Copy as many bytes as will fit */ - if (n != 0 && --n != 0) { - do { - if ((*d++ = *s++) == 0) - break; - } while (--n != 0); - } - - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } - - return(s - src - 1); /* count does not include NUL */ -} - -#endif /* !HAVE_STRLCPY */ diff --git a/usr.sbin/nsd/compat/strptime.c b/usr.sbin/nsd/compat/strptime.c deleted file mode 100644 index 1ba36208d84..00000000000 --- a/usr.sbin/nsd/compat/strptime.c +++ /dev/null @@ -1,349 +0,0 @@ -/** strptime workaround (for oa macos leopard) - * This strptime follows the man strptime (2001-11-12) - * conforming to SUSv2, POSIX.1-2001 - * - * This very simple version of strptime has no: - * - E alternatives - * - O alternatives - * - Glibc additions - * - Does not process week numbers - * - Does not properly processes year day - * - * LICENSE - * Copyright (c) 2008, NLnet Labs, Matthijs Mekking. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of NLnetLabs nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - **/ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#ifndef HAVE_CONFIG_H -#include <time.h> -#endif - -#ifndef STRPTIME_WORKS - -#define TM_YEAR_BASE 1900 - -#include <ctype.h> -#include <string.h> - -static const char *abb_weekdays[] = { - "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL -}; -static const char *full_weekdays[] = { - "Sunday", "Monday", "Tuesday", "Wednesday", - "Thursday", "Friday", "Saturday", NULL -}; -static const char *abb_months[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL -}; -static const char *full_months[] = { - "January", "February", "March", "April", "May", "June", - "July", "August", "September", "October", "November", "December", NULL -}; -static const char *ampm[] = { - "am", "pm", NULL -}; - -static int -match_string(const char **buf, const char **strs) -{ - int i = 0; - - for (i = 0; strs[i] != NULL; i++) { - int len = strlen(strs[i]); - if (strncasecmp (*buf, strs[i], len) == 0) { - *buf += len; - return i; - } - } - return -1; -} - -static int -str2int(const char **buf, int max) -{ - int ret=0, count=0; - - while (*buf[0] != '\0' && isdigit((unsigned char)*buf[0]) && count<max) { - ret = ret*10 + (*buf[0] - '0'); - (*buf)++; - count++; - } - - if (!count) - return -1; - return ret; -} - -/** Converts the character string s to values which are stored in tm - * using the format specified by format - **/ -char * -nsd_strptime(const char *s, const char *format, struct tm *tm) -{ - int c, alt_format, ret; - int split_year = 0; - - while ((c = *format) != '\0') { - alt_format = 0; - - /* whitespace, literal or format */ - if (isspace((unsigned char)c)) { /* whitespace */ - /** whitespace matches zero or more whitespace characters in the - * input string. - **/ - while (isspace((unsigned char)*s)) - s++; - } - else if (c == '%') { /* format */ - format++; - c = *format; - switch (c) { - case '%': /* %% is converted to % */ - if (*s != c) { - return NULL; - } - s++; - break; - case 'a': /* weekday name, abbreviated or full */ - case 'A': - ret = match_string(&s, full_weekdays); - if (ret < 0) - ret = match_string(&s, abb_weekdays); - if (ret < 0) { - return NULL; - } - tm->tm_wday = ret; - break; - case 'b': /* month name, abbreviated or full */ - case 'B': - case 'h': - ret = match_string(&s, full_months); - if (ret < 0) - ret = match_string(&s, abb_months); - if (ret < 0) { - return NULL; - } - tm->tm_mon = ret; - break; - case 'c': /* date and time representation */ - if (!(s = nsd_strptime(s, "%x %X", tm))) { - return NULL; - } - break; - case 'C': /* century number */ - ret = str2int(&s, 2); - if (ret < 0 || ret > 99) { /* must be in [00,99] */ - return NULL; - } - - if (split_year) { - tm->tm_year = ret*100 + (tm->tm_year%100); - } - else { - tm->tm_year = ret*100 - TM_YEAR_BASE; - split_year = 1; - } - break; - case 'd': /* day of month */ - case 'e': - ret = str2int(&s, 2); - if (ret < 1 || ret > 31) { /* must be in [01,31] */ - return NULL; - } - tm->tm_mday = ret; - break; - case 'D': /* equivalent to %m/%d/%y */ - if (!(s = nsd_strptime(s, "%m/%d/%y", tm))) { - return NULL; - } - break; - case 'H': /* hour */ - ret = str2int(&s, 2); - if (ret < 0 || ret > 23) { /* must be in [00,23] */ - return NULL; - } - tm->tm_hour = ret; - break; - case 'I': /* 12hr clock hour */ - ret = str2int(&s, 2); - if (ret < 1 || ret > 12) { /* must be in [01,12] */ - return NULL; - } - if (ret == 12) /* actually [0,11] */ - ret = 0; - tm->tm_hour = ret; - break; - case 'j': /* day of year */ - ret = str2int(&s, 2); - if (ret < 1 || ret > 366) { /* must be in [001,366] */ - return NULL; - } - tm->tm_yday = ret; - break; - case 'm': /* month */ - ret = str2int(&s, 2); - if (ret < 1 || ret > 12) { /* must be in [01,12] */ - return NULL; - } - /* months go from 0-11 */ - tm->tm_mon = (ret-1); - break; - case 'M': /* minute */ - ret = str2int(&s, 2); - if (ret < 0 || ret > 59) { /* must be in [00,59] */ - return NULL; - } - tm->tm_min = ret; - break; - case 'n': /* arbitrary whitespace */ - case 't': - while (isspace((unsigned char)*s)) - s++; - break; - case 'p': /* am pm */ - ret = match_string(&s, ampm); - if (ret < 0) { - return NULL; - } - if (tm->tm_hour < 0 || tm->tm_hour > 11) { /* %I */ - return NULL; - } - - if (ret == 1) /* pm */ - tm->tm_hour += 12; - break; - case 'r': /* equivalent of %I:%M:%S %p */ - if (!(s = nsd_strptime(s, "%I:%M:%S %p", tm))) { - return NULL; - } - break; - case 'R': /* equivalent of %H:%M */ - if (!(s = nsd_strptime(s, "%H:%M", tm))) { - return NULL; - } - break; - case 'S': /* seconds */ - ret = str2int(&s, 2); - /* 60 may occur for leap seconds */ - /* earlier 61 was also allowed */ - if (ret < 0 || ret > 60) { /* must be in [00,60] */ - return NULL; - } - tm->tm_sec = ret; - break; - case 'T': /* equivalent of %H:%M:%S */ - if (!(s = nsd_strptime(s, "%H:%M:%S", tm))) { - return NULL; - } - break; - case 'U': /* week number, with the first Sun of Jan being w1 */ - ret = str2int(&s, 2); - if (ret < 0 || ret > 53) { /* must be in [00,53] */ - return NULL; - } - /** it is hard (and not necessary for nsd) to determine time - * data from week number. - **/ - break; - case 'w': /* day of week */ - ret = str2int(&s, 1); - if (ret < 0 || ret > 6) { /* must be in [0,6] */ - return NULL; - } - tm->tm_wday = ret; - break; - case 'W': /* week number, with the first Mon of Jan being w1 */ - ret = str2int(&s, 2); - if (ret < 0 || ret > 53) { /* must be in [00,53] */ - return NULL; - } - /** it is hard (and not necessary for nsd) to determine time - * data from week number. - **/ - break; - case 'x': /* date format */ - if (!(s = nsd_strptime(s, "%m/%d/%y", tm))) { - return NULL; - } - break; - case 'X': /* time format */ - if (!(s = nsd_strptime(s, "%H:%M:%S", tm))) { - return NULL; - } - break; - case 'y': /* last two digits of a year */ - ret = str2int(&s, 2); - if (ret < 0 || ret > 99) { /* must be in [00,99] */ - return NULL; - } - if (split_year) { - tm->tm_year = ((tm->tm_year/100) * 100) + ret; - } - else { - split_year = 1; - - /** currently: - * if in [0,68] we are in 21th century, - * if in [69,99] we are in 20th century. - **/ - if (ret < 69) /* 2000 */ - ret += 100; - tm->tm_year = ret; - } - break; - case 'Y': /* year */ - ret = str2int(&s, 4); - if (ret < 0 || ret > 9999) { - return NULL; - } - tm->tm_year = ret - TM_YEAR_BASE; - break; - case '\0': - default: /* unsupported, cannot match format */ - return NULL; - break; - } - } - else { /* literal */ - /* if input cannot match format, return NULL */ - if (*s != c) - return NULL; - s++; - } - - format++; - } - - /* return pointer to remainder of s */ - return (char*) s; -} - -#endif /* STRPTIME_WORKS */ |