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