util.h revision 1.26
1/* $OpenBSD: util.h,v 1.26 2014/06/21 08:00:23 otto 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 <sys/utsname.h> 35#include <stdarg.h> 36 37void *_dl_malloc(size_t size); 38void *_dl_calloc(size_t nmemb, const size_t size); 39void *_dl_realloc(void *, size_t size); 40void *_dl_reallocarray(void *, size_t nmemb, size_t size); 41void _dl_free(void *); 42char *_dl_strdup(const char *); 43size_t _dl_strlen(const char *); 44size_t _dl_strlcat(char *dst, const char *src, size_t siz); 45void _dl_printf(const char *fmt, ...); 46void _dl_vprintf(const char *fmt, va_list ap); 47void _dl_fdprintf(int, const char *fmt, ...); 48void _dl_show_objects(void); 49void _dl_randombuf(void *, size_t); 50u_int32_t _dl_random(void); 51ssize_t _dl_write(int fd, const char* buf, size_t len); 52char * _dl_dirname(const char *path); 53char *_dl_realpath(const char *path, char *resolved); 54int _dl_uname(struct utsname *name); 55 56long _dl_strtol(const char *nptr, char **endptr, int base); 57 58#define _dl_round_page(x) (((x) + (__LDPGSZ - 1)) & ~(__LDPGSZ - 1)) 59 60/* 61 * The following functions are declared inline so they can 62 * be used before bootstrap linking has been finished. 63 */ 64static inline void 65_dl_wrstderr(const char *s) 66{ 67 const char *p = s; 68 size_t n = 0; 69 70 while (*p++) 71 n++; 72 _dl_write(2, s, n); 73} 74 75static inline void * 76_dl_memset(void *dst, const int c, size_t n) 77{ 78 if (n != 0) { 79 char *d = dst; 80 81 do 82 *d++ = c; 83 while (--n != 0); 84 } 85 return (dst); 86} 87 88static inline void 89_dl_bcopy(const void *src, void *dest, int size) 90{ 91 unsigned const char *psrc = src; 92 unsigned char *pdest = dest; 93 int i; 94 95 for (i = 0; i < size; i++) 96 pdest[i] = psrc[i]; 97} 98 99static inline size_t 100_dl_strlcpy(char *dst, const char *src, size_t siz) 101{ 102 char *d = dst; 103 const char *s = src; 104 size_t n = siz; 105 106 /* Copy as many bytes as will fit */ 107 if (n != 0 && --n != 0) { 108 do { 109 if ((*d++ = *s++) == 0) 110 break; 111 } while (--n != 0); 112 } 113 114 /* Not enough room in dst, add NUL and traverse rest of src */ 115 if (n == 0) { 116 if (siz != 0) 117 *d = '\0'; /* NUL-terminate dst */ 118 while (*s++) 119 ; 120 } 121 122 return(s - src - 1); /* count does not include NUL */ 123} 124 125static inline int 126_dl_strncmp(const char *s1, const char *s2, size_t n) 127{ 128 if (n == 0) 129 return (0); 130 do { 131 if (*s1 != *s2++) 132 return (*(unsigned char *)s1 - *(unsigned char *)--s2); 133 if (*s1++ == 0) 134 break; 135 } while (--n != 0); 136 return (0); 137} 138 139static inline int 140_dl_strcmp(const char *s1, const char *s2) 141{ 142 while (*s1 == *s2++) 143 if (*s1++ == 0) 144 return (0); 145 return (*(unsigned char *)s1 - *(unsigned char *)--s2); 146} 147 148static inline const char * 149_dl_strchr(const char *p, const int ch) 150{ 151 for (;; ++p) { 152 if (*p == ch) 153 return((char *)p); 154 if (!*p) 155 return((char *)NULL); 156 } 157 /* NOTREACHED */ 158} 159 160static inline char * 161_dl_strrchr(const char *str, const int ch) 162{ 163 const char *p; 164 char *retval = NULL; 165 166 for (p = str; *p != '\0'; ++p) 167 if (*p == ch) 168 retval = (char *)p; 169 170 return retval; 171} 172 173static inline char * 174_dl_strstr(const char *s, const char *find) 175{ 176 char c, sc; 177 size_t len; 178 if ((c = *find++) != 0) { 179 len = _dl_strlen(find); 180 do { 181 do { 182 if ((sc = *s++) == 0) 183 return (NULL); 184 } while (sc != c); 185 } while (_dl_strncmp(s, find, len) != 0); 186 s--; 187 } 188 return ((char *)s); 189} 190 191static inline int 192_dl_isalnum(int c) 193{ 194 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); 195} 196 197#endif /*__DL_UTIL_H__*/ 198