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