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