readelf: report ARM program and section header types
[freebsd-src.git] / contrib / bsnmp / lib / support.c
blob391c152d075918988651462f2f97d76c18015a49
1 /*
2 * Copyright (C) 2004
3 * Hartmut Brandt.
4 * All rights reserved.
5 *
6 * Author: Harti Brandt <harti@freebsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * $Begemot: bsnmp/lib/support.c,v 1.1 2004/08/06 08:47:58 brandt Exp $
31 * Functions that are missing on certain systems.
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <errno.h>
37 #include <string.h>
38 #include "support.h"
40 #ifndef HAVE_ERR_H
42 void
43 warnx(const char *fmt, ...)
45 va_list ap;
47 va_start(ap, fmt);
48 fprintf(stderr, "warning: ");
49 vfprintf(stderr, fmt, ap);
50 fprintf(stderr, "\n");
51 va_end(ap);
54 void
55 warn(const char *fmt, ...)
57 va_list ap;
58 int e = errno;
60 va_start(ap, fmt);
61 fprintf(stderr, "warning: ");
62 vfprintf(stderr, fmt, ap);
63 fprintf(stderr, ": %s\n", strerror(e));
64 va_end(ap);
67 void
68 errx(int code, const char *fmt, ...)
70 va_list ap;
72 va_start(ap, fmt);
73 fprintf(stderr, "error: ");
74 vfprintf(stderr, fmt, ap);
75 fprintf(stderr, "\n");
76 va_end(ap);
77 exit(code);
80 void
81 err(int code, const char *fmt, ...)
83 va_list ap;
84 int e = errno;
86 va_start(ap, fmt);
87 fprintf(stderr, "error: ");
88 vfprintf(stderr, fmt, ap);
89 fprintf(stderr, ": %s\n", strerror(e));
90 va_end(ap);
91 exit(code);
94 #endif
96 #ifndef HAVE_STRLCPY
98 size_t
99 strlcpy(char *dst, const char *src, size_t len)
101 size_t ret = strlen(dst);
103 while (len > 1) {
104 *dst++ = *src++;
105 len--;
107 if (len > 0)
108 *dst = '\0';
109 return (ret);
112 #endif
114 #ifndef HAVE_GETADDRINFO
116 #include <sys/types.h>
117 #include <sys/socket.h>
118 #include <netinet/in.h>
119 #include <netdb.h>
121 extern int h_nerr;
122 extern int h_errno;
123 extern const char *h_errlist[];
126 * VERY poor man's implementation
129 getaddrinfo(const char *host, const char *port, const struct addrinfo *hints,
130 struct addrinfo **res)
132 struct hostent *hent;
133 struct sockaddr_in *s;
134 struct servent *sent;
136 if ((hent = gethostbyname(host)) == NULL)
137 return (h_errno);
138 if (hent->h_addrtype != hints->ai_family)
139 return (HOST_NOT_FOUND);
140 if (hent->h_addrtype != AF_INET)
141 return (HOST_NOT_FOUND);
143 if ((*res = malloc(sizeof(**res))) == NULL)
144 return (HOST_NOT_FOUND);
146 (*res)->ai_flags = hints->ai_flags;
147 (*res)->ai_family = hints->ai_family;
148 (*res)->ai_socktype = hints->ai_socktype;
149 (*res)->ai_protocol = hints->ai_protocol;
150 (*res)->ai_next = NULL;
152 if (((*res)->ai_addr = malloc(sizeof(struct sockaddr_in))) == NULL) {
153 freeaddrinfo(*res);
154 return (HOST_NOT_FOUND);
156 (*res)->ai_addrlen = sizeof(struct sockaddr_in);
157 s = (struct sockaddr_in *)(*res)->ai_addr;
158 s->sin_family = hints->ai_family;
159 s->sin_len = sizeof(*s);
160 memcpy(&s->sin_addr, hent->h_addr, 4);
162 if ((sent = getservbyname(port, NULL)) == NULL) {
163 freeaddrinfo(*res);
164 return (HOST_NOT_FOUND);
166 s->sin_port = sent->s_port;
168 return (0);
171 const char *
172 gai_strerror(int e)
175 if (e < 0 || e >= h_nerr)
176 return ("unknown error");
177 return (h_errlist[e]);
180 void
181 freeaddrinfo(struct addrinfo *p)
183 struct addrinfo *next;
185 while (p != NULL) {
186 next = p->ai_next;
187 if (p->ai_addr != NULL)
188 free(p->ai_addr);
189 free(p);
190 p = next;
194 #endif