util.h revision 1.5
1/*	$OpenBSD: util.h,v 1.5 2002/05/27 20:07:06 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, ...);
44
45/*
46 *	The following functions are declared inline so they can
47 *	be used before bootstrap linking has been finished.
48 */
49static inline void
50_dl_wrstderr(const char *s)
51{
52	while (*s) {
53		_dl_write(2, s, 1);
54		s++;
55	}
56}
57
58static inline void *
59_dl_memset(void *p, const char v, size_t c)
60{
61	char *ip = p;
62
63	while (c--)
64		*ip++ = v;
65	return(p);
66}
67
68static inline int
69_dl_strlen(const char *p)
70{
71	const char *s = p;
72
73	while (*s != '\0')
74		s++;
75	return(s - p);
76}
77
78static inline size_t
79_dl_strlcpy(char *dst, const char *src, int siz)
80{
81	char *d = dst;
82	const char *s = src;
83	size_t n = siz;
84
85	/* Copy as many bytes as will fit */
86	if (n != 0 && --n != 0) {
87		do {
88			if ((*d++ = *s++) == 0)
89				break;
90		} while (--n != 0);
91	}
92
93	/* Not enough room in dst, add NUL and traverse rest of src */
94	if (n == 0) {
95		if (siz != 0)
96			*d = '\0';		/* NUL-terminate dst */
97		while (*s++)
98			;
99	}
100
101	return(s - src - 1);	/* count does not include NUL */
102}
103
104static inline int
105_dl_strncmp(const char *d, const char *s, int c)
106{
107	while (c-- && *d && *d == *s) {
108		d++;
109		s++;
110	}
111	if (c < 0)
112		return(0);
113	return(*d - *s);
114}
115
116static inline int
117_dl_strcmp(const char *d, const char *s)
118{
119	while (*d && *d == *s) {
120		d++;
121		s++;
122	}
123	return(*d - *s);
124}
125
126static inline const char *
127_dl_strchr(const char *p, const int c)
128{
129	while (*p) {
130		if (*p == c)
131			return(p);
132		p++;
133	}
134	return(0);
135}
136
137#endif /*__DL_UTIL_H__*/
138