1 /* $NetBSD: libkern.h,v 1.33 2001/04/05 04:38:06 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)libkern.h 8.2 (Berkeley) 8/5/94 36 */ 37 38 #ifndef _LIB_LIBKERN_LIBKERN_H_ 39 #define _LIB_LIBKERN_LIBKERN_H_ 40 41 #include <sys/types.h> 42 43 #ifndef LIBKERN_INLINE 44 #define LIBKERN_INLINE static __inline 45 #define LIBKERN_BODY 46 #endif 47 48 LIBKERN_INLINE int imax __P((int, int)) __attribute__ ((unused)); 49 LIBKERN_INLINE int imin __P((int, int)) __attribute__ ((unused)); 50 LIBKERN_INLINE u_int max __P((u_int, u_int)) __attribute__ ((unused)); 51 LIBKERN_INLINE u_int min __P((u_int, u_int)) __attribute__ ((unused)); 52 LIBKERN_INLINE long lmax __P((long, long)) __attribute__ ((unused)); 53 LIBKERN_INLINE long lmin __P((long, long)) __attribute__ ((unused)); 54 LIBKERN_INLINE u_long ulmax __P((u_long, u_long)) __attribute__ ((unused)); 55 LIBKERN_INLINE u_long ulmin __P((u_long, u_long)) __attribute__ ((unused)); 56 LIBKERN_INLINE int abs __P((int)) __attribute__ ((unused)); 57 58 LIBKERN_INLINE int isspace __P((int)) __attribute__((__unused__)); 59 LIBKERN_INLINE int isascii __P((int)) __attribute__((__unused__)); 60 LIBKERN_INLINE int isupper __P((int)) __attribute__((__unused__)); 61 LIBKERN_INLINE int islower __P((int)) __attribute__((__unused__)); 62 LIBKERN_INLINE int isalpha __P((int)) __attribute__((__unused__)); 63 LIBKERN_INLINE int isdigit __P((int)) __attribute__((__unused__)); 64 LIBKERN_INLINE int isxdigit __P((int)) __attribute__((__unused__)); 65 LIBKERN_INLINE int toupper __P((int)) __attribute__((__unused__)); 66 LIBKERN_INLINE int tolower __P((int)) __attribute__((__unused__)); 67 68 #ifdef LIBKERN_BODY 69 LIBKERN_INLINE int 70 imax(a, b) 71 int a, b; 72 { 73 return (a > b ? a : b); 74 } 75 LIBKERN_INLINE int 76 imin(a, b) 77 int a, b; 78 { 79 return (a < b ? a : b); 80 } 81 LIBKERN_INLINE long 82 lmax(a, b) 83 long a, b; 84 { 85 return (a > b ? a : b); 86 } 87 LIBKERN_INLINE long 88 lmin(a, b) 89 long a, b; 90 { 91 return (a < b ? a : b); 92 } 93 LIBKERN_INLINE u_int 94 max(a, b) 95 u_int a, b; 96 { 97 return (a > b ? a : b); 98 } 99 LIBKERN_INLINE u_int 100 min(a, b) 101 u_int a, b; 102 { 103 return (a < b ? a : b); 104 } 105 LIBKERN_INLINE u_long 106 ulmax(a, b) 107 u_long a, b; 108 { 109 return (a > b ? a : b); 110 } 111 LIBKERN_INLINE u_long 112 ulmin(a, b) 113 u_long a, b; 114 { 115 return (a < b ? a : b); 116 } 117 118 LIBKERN_INLINE int 119 abs(j) 120 int j; 121 { 122 return(j < 0 ? -j : j); 123 } 124 125 LIBKERN_INLINE int 126 isspace(ch) 127 int ch; 128 { 129 130 return (ch == ' ' || (ch >= '\t' && ch <= '\r')); 131 } 132 133 LIBKERN_INLINE int 134 isascii(ch) 135 int ch; 136 { 137 138 return ((ch & ~0x7f) == 0); 139 } 140 141 LIBKERN_INLINE int 142 isupper(ch) 143 int ch; 144 { 145 146 return (ch >= 'A' && ch <= 'Z'); 147 } 148 149 LIBKERN_INLINE int 150 islower(ch) 151 int ch; 152 { 153 154 return (ch >= 'a' && ch <= 'z'); 155 } 156 157 LIBKERN_INLINE int 158 isalpha(ch) 159 int ch; 160 { 161 162 return (isupper(ch) || islower(ch)); 163 } 164 165 LIBKERN_INLINE int 166 isdigit(ch) 167 int ch; 168 { 169 170 return (ch >= '0' && ch <= '9'); 171 } 172 173 LIBKERN_INLINE int 174 isxdigit(ch) 175 int ch; 176 { 177 178 return (isdigit(ch) || 179 (ch >= 'A' && ch <= 'F') || 180 (ch >= 'a' && ch <= 'f')); 181 } 182 183 LIBKERN_INLINE int 184 toupper(ch) 185 int ch; 186 { 187 188 if (islower(ch)) 189 return (ch - 0x20); 190 return (ch); 191 } 192 193 LIBKERN_INLINE int 194 tolower(ch) 195 int ch; 196 { 197 198 if (isupper(ch)) 199 return (ch + 0x20); 200 return (ch); 201 } 202 #endif 203 204 #ifdef NDEBUG /* tradition! */ 205 #define assert(e) ((void)0) 206 #else 207 #ifdef __STDC__ 208 #define assert(e) (__predict_true((e)) ? (void)0 : \ 209 __assert("", __FILE__, __LINE__, #e)) 210 #else 211 #define assert(e) (__predict_true((e)) ? (void)0 : \ 212 __assert("", __FILE__, __LINE__, "e")) 213 #endif 214 #endif 215 216 #ifndef DIAGNOSTIC 217 #define KASSERT(e) ((void)0) 218 #else 219 #ifdef __STDC__ 220 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 221 __assert("diagnostic ", __FILE__, __LINE__, #e)) 222 #else 223 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 224 __assert("diagnostic ", __FILE__, __LINE__, "e")) 225 #endif 226 #endif 227 228 #ifndef DEBUG 229 #define KDASSERT(e) ((void)0) 230 #else 231 #ifdef __STDC__ 232 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 233 __assert("debugging ", __FILE__, __LINE__, #e)) 234 #else 235 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 236 __assert("debugging ", __FILE__, __LINE__, "e")) 237 #endif 238 #endif 239 240 #ifndef offsetof 241 #define offsetof(type, member) ((size_t)(&((type *)0)->member)) 242 #endif 243 244 /* Prototypes for non-quad routines. */ 245 void __assert __P((const char *, const char *, int, const char *)) 246 __attribute__((__noreturn__)); 247 int bcmp __P((const void *, const void *, size_t)); 248 void bzero __P((void *, size_t)); 249 int ffs __P((int)); 250 u_int32_t 251 inet_addr __P((const char *)); 252 char *intoa __P((u_int32_t)); 253 #define inet_ntoa(a) intoa((a).s_addr) 254 void *memchr __P((const void *, int, size_t)); 255 int memcmp __P((const void *, const void *, size_t)); 256 void *memcpy __P((void *, const void *, size_t)); 257 void *memmove __P((void *, const void *, size_t)); 258 void *memset __P((void *, int, size_t)); 259 int pmatch __P((const char *, const char *, const char **)); 260 u_long random __P((void)); 261 int scanc __P((u_int, const u_char *, const u_char *, int)); 262 int skpc __P((int, size_t, u_char *)); 263 char *strcat __P((char *, const char *)); 264 char *strchr __P((const char *, int)); 265 int strcmp __P((const char *, const char *)); 266 int strcasecmp __P((const char *, const char *)); 267 char *strcpy __P((char *, const char *)); 268 size_t strlen __P((const char *)); 269 int strncasecmp __P((const char *, const char *, size_t)); 270 int strncmp __P((const char *, const char *, size_t)); 271 char *strncpy __P((char *, const char *, size_t)); 272 char *strrchr __P((const char *, int)); 273 u_long strtoul __P((const char *, char **, int)); 274 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */ 275