util.h revision 1.20
1/*	$OpenBSD: util.h,v 1.20 2007/02/09 14:51:13 drahn 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 <stdarg.h>
35
36void *_dl_malloc(const size_t size);
37void _dl_free(void *);
38char *_dl_strdup(const char *);
39void _dl_printf(const char *fmt, ...);
40void _dl_vprintf(const char *fmt, va_list ap);
41void _dl_fdprintf(int, const char *fmt, ...);
42void _dl_show_objects(void);
43unsigned int _dl_random(void);
44ssize_t _dl_write(int fd, const char* buf, size_t len);
45
46long _dl_strtol(const char *nptr, char **endptr, int base);
47
48#define	_dl_round_page(x)	(((x) + (__LDPGSZ - 1)) & ~(__LDPGSZ - 1))
49
50/*
51 *	The following functions are declared inline so they can
52 *	be used before bootstrap linking has been finished.
53 */
54static inline void
55_dl_wrstderr(const char *s)
56{
57	while (*s) {
58		_dl_write(2, s, 1);
59		s++;
60	}
61}
62
63static inline void *
64_dl_memset(void *dst, const int c, size_t n)
65{
66	if (n != 0) {
67		char *d = dst;
68
69		do
70			*d++ = c;
71		while (--n != 0);
72	}
73	return (dst);
74}
75
76static inline void
77_dl_bcopy(const void *src, void *dest, int size)
78{
79	unsigned const char *psrc = src;
80	unsigned char *pdest = dest;
81	int i;
82
83	for (i = 0; i < size; i++)
84		pdest[i] = psrc[i];
85}
86
87static inline int
88_dl_strlen(const char *str)
89{
90	const char *s;
91
92	for (s = str; *s; ++s)
93		;
94	return (s - str);
95}
96
97static inline size_t
98_dl_strlcpy(char *dst, const char *src, size_t siz)
99{
100	char *d = dst;
101	const char *s = src;
102	size_t n = siz;
103
104	/* Copy as many bytes as will fit */
105	if (n != 0 && --n != 0) {
106		do {
107			if ((*d++ = *s++) == 0)
108				break;
109		} while (--n != 0);
110	}
111
112	/* Not enough room in dst, add NUL and traverse rest of src */
113	if (n == 0) {
114		if (siz != 0)
115			*d = '\0';		/* NUL-terminate dst */
116		while (*s++)
117			;
118	}
119
120	return(s - src - 1);	/* count does not include NUL */
121}
122
123static inline int
124_dl_strncmp(const char *s1, const char *s2, size_t n)
125{
126	if (n == 0)
127		return (0);
128	do {
129		if (*s1 != *s2++)
130			return (*(unsigned char *)s1 - *(unsigned char *)--s2);
131		if (*s1++ == 0)
132			break;
133	} while (--n != 0);
134	return (0);
135}
136
137static inline int
138_dl_strcmp(const char *s1, const char *s2)
139{
140	while (*s1 == *s2++)
141		if (*s1++ == 0)
142			return (0);
143	return (*(unsigned char *)s1 - *(unsigned char *)--s2);
144}
145
146static inline const char *
147_dl_strchr(const char *p, const int ch)
148{
149	for (;; ++p) {
150		if (*p == ch)
151			return((char *)p);
152		if (!*p)
153			return((char *)NULL);
154	}
155	/* NOTREACHED */
156}
157
158static inline char *
159_dl_strrchr(const char *str, const int ch)
160{
161	const char *p;
162	char *retval = NULL;
163
164	for (p = str; *p != '\0'; ++p)
165		if (*p == ch)
166			retval = (char *)p;
167
168	return retval;
169}
170
171static inline char *
172_dl_strstr(const char *s, const char *find)
173{
174	char c, sc;
175	size_t len;
176	if ((c = *find++) != 0) {
177		len = _dl_strlen(find);
178		do {
179			do {
180				if ((sc = *s++) == 0)
181					return (NULL);
182			} while (sc != c);
183		} while (_dl_strncmp(s, find, len) != 0);
184		s--;
185	}
186	return ((char *)s);
187}
188
189#endif /*__DL_UTIL_H__*/
190