util.h revision 1.29
1/*	$OpenBSD: util.h,v 1.29 2017/01/23 11:04:19 guenther Exp $	*/
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved.
6 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
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.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * 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.
28 *
29 */
30
31#ifndef __DL_UTIL_H__
32#define __DL_UTIL_H__
33
34#include <sys/utsname.h>
35#include <stdarg.h>
36#include <stddef.h>		/* for NULL */
37
38__BEGIN_HIDDEN_DECLS
39void *_dl_malloc(size_t size);
40void *_dl_calloc(size_t nmemb, const size_t size);
41void *_dl_realloc(void *, size_t size);
42void *_dl_reallocarray(void *, size_t nmemb, size_t size);
43void _dl_free(void *);
44char *_dl_strdup(const char *);
45size_t _dl_strlen(const char *);
46size_t _dl_strlcat(char *dst, const char *src, size_t siz);
47void _dl_printf(const char *fmt, ...);
48void _dl_vprintf(const char *fmt, va_list ap);
49void _dl_fdprintf(int, const char *fmt, ...);
50void _dl_show_objects(void);
51void _dl_arc4randombuf(void *, size_t);
52u_int32_t _dl_arc4random(void);
53ssize_t _dl_write(int fd, const char* buf, size_t len);
54char * _dl_dirname(const char *path);
55char *_dl_realpath(const char *path, char *resolved);
56int _dl_uname(struct utsname *name);
57
58long _dl_strtol(const char *nptr, char **endptr, int base);
59__END_HIDDEN_DECLS
60
61#define	_dl_round_page(x)	(((x) + (__LDPGSZ - 1)) & ~(__LDPGSZ - 1))
62
63/*
64 *	The following functions are declared inline so they can
65 *	be used before bootstrap linking has been finished.
66 */
67static inline void
68_dl_wrstderr(const char *s)
69{
70	const char *p = s;
71	size_t n = 0;
72
73	while (*p++)
74		n++;
75	_dl_write(2, s, n);
76}
77
78static inline void *
79_dl_memset(void *dst, const int c, size_t n)
80{
81	if (n != 0) {
82		char *d = dst;
83
84		do
85			*d++ = c;
86		while (--n != 0);
87	}
88	return (dst);
89}
90
91static inline void
92_dl_bcopy(const void *src, void *dest, int size)
93{
94	unsigned const char *psrc = src;
95	unsigned char *pdest = dest;
96	int i;
97
98	for (i = 0; i < size; i++)
99		pdest[i] = psrc[i];
100}
101
102static inline size_t
103_dl_strlcpy(char *dst, const char *src, size_t siz)
104{
105	char *d = dst;
106	const char *s = src;
107	size_t n = siz;
108
109	/* Copy as many bytes as will fit */
110	if (n != 0 && --n != 0) {
111		do {
112			if ((*d++ = *s++) == 0)
113				break;
114		} while (--n != 0);
115	}
116
117	/* Not enough room in dst, add NUL and traverse rest of src */
118	if (n == 0) {
119		if (siz != 0)
120			*d = '\0';		/* NUL-terminate dst */
121		while (*s++)
122			;
123	}
124
125	return(s - src - 1);	/* count does not include NUL */
126}
127
128static inline int
129_dl_strncmp(const char *s1, const char *s2, size_t n)
130{
131	if (n == 0)
132		return (0);
133	do {
134		if (*s1 != *s2++)
135			return (*(unsigned char *)s1 - *(unsigned char *)--s2);
136		if (*s1++ == 0)
137			break;
138	} while (--n != 0);
139	return (0);
140}
141
142static inline int
143_dl_strcmp(const char *s1, const char *s2)
144{
145	while (*s1 == *s2++)
146		if (*s1++ == 0)
147			return (0);
148	return (*(unsigned char *)s1 - *(unsigned char *)--s2);
149}
150
151static inline const char *
152_dl_strchr(const char *p, const int ch)
153{
154	for (;; ++p) {
155		if (*p == ch)
156			return((char *)p);
157		if (!*p)
158			return((char *)NULL);
159	}
160	/* NOTREACHED */
161}
162
163static inline char *
164_dl_strrchr(const char *str, const int ch)
165{
166	const char *p;
167	char *retval = NULL;
168
169	for (p = str; *p != '\0'; ++p)
170		if (*p == ch)
171			retval = (char *)p;
172
173	return retval;
174}
175
176static inline char *
177_dl_strstr(const char *s, const char *find)
178{
179	char c, sc;
180	size_t len;
181	if ((c = *find++) != 0) {
182		len = _dl_strlen(find);
183		do {
184			do {
185				if ((sc = *s++) == 0)
186					return (NULL);
187			} while (sc != c);
188		} while (_dl_strncmp(s, find, len) != 0);
189		s--;
190	}
191	return ((char *)s);
192}
193
194static inline int
195_dl_isalnum(int c)
196{
197	return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
198}
199
200#endif /*__DL_UTIL_H__*/
201