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