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