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