util.h revision 1.9
1/*	$OpenBSD: util.h,v 1.9 2002/07/24 04:00:44 deraadt 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 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed under OpenBSD by
19 *	Per Fogelstrom, Opsycon AB, Sweden.
20 * 4. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37#ifndef __DL_UTIL_H__
38#define __DL_UTIL_H__
39int _dl_write(int, const char *, int);
40void *_dl_malloc(const int size);
41void _dl_free(void *);
42char *_dl_strdup(const char *);
43void _dl_printf(const char *fmt, ...);
44void _dl_show_objects(void);
45
46/*
47 *	The following functions are declared inline so they can
48 *	be used before bootstrap linking has been finished.
49 */
50static inline void
51_dl_wrstderr(const char *s)
52{
53	while (*s) {
54		_dl_write(2, s, 1);
55		s++;
56	}
57}
58
59static inline void *
60_dl_memset(void *p, const char v, size_t c)
61{
62	char *ip = p;
63
64	while (c--)
65		*ip++ = v;
66	return(p);
67}
68
69static inline int
70_dl_strlen(const char *p)
71{
72	const char *s = p;
73
74	while (*s != '\0')
75		s++;
76	return(s - p);
77}
78
79static inline size_t
80_dl_strlcpy(char *dst, const char *src, int siz)
81{
82	char *d = dst;
83	const char *s = src;
84	size_t n = siz;
85
86	/* Copy as many bytes as will fit */
87	if (n != 0 && --n != 0) {
88		do {
89			if ((*d++ = *s++) == 0)
90				break;
91		} while (--n != 0);
92	}
93
94	/* Not enough room in dst, add NUL and traverse rest of src */
95	if (n == 0) {
96		if (siz != 0)
97			*d = '\0';		/* NUL-terminate dst */
98		while (*s++)
99			;
100	}
101
102	return(s - src - 1);	/* count does not include NUL */
103}
104
105static inline int
106_dl_strncmp(const char *d, const char *s, int c)
107{
108	while (c-- && *d && *d == *s) {
109		d++;
110		s++;
111	}
112	if (c < 0)
113		return(0);
114	return(*d - *s);
115}
116
117static inline int
118_dl_strcmp(const char *d, const char *s)
119{
120	while (*d && *d == *s) {
121		d++;
122		s++;
123	}
124	return(*d - *s);
125}
126
127static inline const char *
128_dl_strchr(const char *p, const int c)
129{
130	while (*p) {
131		if (*p == c)
132			return(p);
133		p++;
134	}
135	return(0);
136}
137
138#endif /*__DL_UTIL_H__*/
139