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