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