util.h revision 1.12
1/*	$OpenBSD: util.h,v 1.12 2002/08/31 15:11:59 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 * 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__
39void *_dl_malloc(const size_t size);
40void _dl_free(void *);
41char *_dl_strdup(const char *);
42void _dl_printf(const char *fmt, ...);
43void _dl_fdprintf(int, 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 *dst, const int c, size_t n)
61{
62	if (n != 0) {
63		register char *d = dst;
64
65		do
66			*d++ = c;
67		while (--n != 0);
68	}
69	return (dst);
70}
71
72static inline int
73_dl_strlen(const char *str)
74{
75	const char *s;
76
77	for (s = str; *s; ++s)
78		;
79	return (s - str);
80}
81
82static inline size_t
83_dl_strlcpy(char *dst, const char *src, size_t siz)
84{
85	char *d = dst;
86	const char *s = src;
87	size_t n = siz;
88
89	/* Copy as many bytes as will fit */
90	if (n != 0 && --n != 0) {
91		do {
92			if ((*d++ = *s++) == 0)
93				break;
94		} while (--n != 0);
95	}
96
97	/* Not enough room in dst, add NUL and traverse rest of src */
98	if (n == 0) {
99		if (siz != 0)
100			*d = '\0';		/* NUL-terminate dst */
101		while (*s++)
102			;
103	}
104
105	return(s - src - 1);	/* count does not include NUL */
106}
107
108static inline int
109_dl_strncmp(const char *s1, const char *s2, size_t n)
110{
111	if (n == 0)
112		return (0);
113	do {
114		if (*s1 != *s2++)
115			return (*(unsigned char *)s1 - *(unsigned char *)--s2);
116		if (*s1++ == 0)
117			break;
118	} while (--n != 0);
119	return (0);
120}
121
122static inline int
123_dl_strcmp(const char *s1, const char *s2)
124{
125	while (*s1 == *s2++)
126		if (*s1++ == 0)
127			return (0);
128	return (*(unsigned char *)s1 - *(unsigned char *)--s2);
129}
130
131static inline const char *
132_dl_strchr(const char *p, const int ch)
133{
134	for (;; ++p) {
135		if (*p == ch)
136			return((char *)p);
137		if (!*p)
138			return((char *)NULL);
139	}
140	/* NOTREACHED */
141}
142
143#endif /*__DL_UTIL_H__*/
144