1 /* $OpenBSD: libkern.h,v 1.17 2002/03/14 01:27:07 millert Exp $ */ 2 /* $NetBSD: libkern.h,v 1.7 1996/03/14 18:52:08 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)libkern.h 8.1 (Berkeley) 6/10/93 37 */ 38 39 #ifndef __LIBKERN_H__ 40 #define __LIBKERN_H__ 41 42 #include <sys/types.h> 43 44 #ifndef LIBKERN_INLINE 45 #define LIBKERN_INLINE static __inline 46 #define LIBKERN_BODY 47 #endif 48 49 50 LIBKERN_INLINE int imax(int, int); 51 LIBKERN_INLINE int imin(int, int); 52 LIBKERN_INLINE u_int max(u_int, u_int); 53 LIBKERN_INLINE u_int min(u_int, u_int); 54 LIBKERN_INLINE long lmax(long, long); 55 LIBKERN_INLINE long lmin(long, long); 56 LIBKERN_INLINE u_long ulmax(u_long, u_long); 57 LIBKERN_INLINE u_long ulmin(u_long, u_long); 58 LIBKERN_INLINE int abs(int); 59 60 #ifdef LIBKERN_BODY 61 LIBKERN_INLINE int 62 imax(a, b) 63 int a, b; 64 { 65 return (a > b ? a : b); 66 } 67 LIBKERN_INLINE int 68 imin(a, b) 69 int a, b; 70 { 71 return (a < b ? a : b); 72 } 73 LIBKERN_INLINE long 74 lmax(a, b) 75 long a, b; 76 { 77 return (a > b ? a : b); 78 } 79 LIBKERN_INLINE long 80 lmin(a, b) 81 long a, b; 82 { 83 return (a < b ? a : b); 84 } 85 LIBKERN_INLINE u_int 86 max(a, b) 87 u_int a, b; 88 { 89 return (a > b ? a : b); 90 } 91 LIBKERN_INLINE u_int 92 min(a, b) 93 u_int a, b; 94 { 95 return (a < b ? a : b); 96 } 97 LIBKERN_INLINE u_long 98 ulmax(a, b) 99 u_long a, b; 100 { 101 return (a > b ? a : b); 102 } 103 LIBKERN_INLINE u_long 104 ulmin(a, b) 105 u_long a, b; 106 { 107 return (a < b ? a : b); 108 } 109 110 LIBKERN_INLINE int 111 abs(j) 112 int j; 113 { 114 return(j < 0 ? -j : j); 115 } 116 #endif 117 118 #ifdef NDEBUG /* tradition! */ 119 #define assert(e) ((void)0) 120 #else 121 #ifdef __STDC__ 122 #define assert(e) ((e) ? (void)0 : \ 123 __assert("", __FILE__, __LINE__, #e)) 124 #else 125 #define assert(e) ((e) ? (void)0 : \ 126 __assert("", __FILE__, __LINE__, "e")) 127 #endif 128 #endif 129 130 #ifndef DIAGNOSTIC 131 #define KASSERT(e) ((void)0) 132 #else 133 #ifdef __STDC__ 134 #define KASSERT(e) ((e) ? (void)0 : \ 135 __assert("diagnostic ", __FILE__, __LINE__, #e)) 136 #else 137 #define KASSERT(e) ((e) ? (void)0 : \ 138 __assert("diagnostic ", __FILE__, __LINE__, "e")) 139 #endif 140 #endif 141 142 #ifndef DEBUG 143 #define KDASSERT(e) ((void)0) 144 #else 145 #ifdef __STDC__ 146 #define KDASSERT(e) ((e) ? (void)0 : \ 147 __assert("debugging ", __FILE__, __LINE__, #e)) 148 #else 149 #define KDASSERT(e) ((e) ? (void)0 : \ 150 __assert("debugging ", __FILE__, __LINE__, "e")) 151 #endif 152 #endif 153 154 /* Prototypes for non-quad routines. */ 155 void __assert(const char *, const char *, int, const char *) 156 __attribute__ ((__noreturn__)); 157 int bcmp(const void *, const void *, size_t); 158 int ffs(int); 159 int locc(int, char *, u_int); 160 void *memchr(const void *, int, size_t); 161 int memcmp(const void *, const void *, size_t); 162 u_long random(void); 163 void srandom(u_long); 164 int scanc(u_int, const u_char *, const u_char *, int); 165 int skpc(int, size_t, u_char *); 166 size_t strlen(const char *); 167 char *strcat(char *, const char *); 168 char *strcpy(char *, const char *); 169 char *strncpy(char *, const char *, size_t); 170 size_t strlcpy(char *, const char *, size_t); 171 size_t strlcat(char *, const char *, size_t); 172 int strcmp(const char *, const char *); 173 int strncmp(const char *, const char *, size_t); 174 int strncasecmp(const char *, const char *, size_t); 175 int getsn(char *, int); 176 177 extern u_int8_t const __bcd2bin[], __bin2bcd[]; 178 #define bcd2bin(b) (__bcd2bin[(b)&0xff]) 179 #define bin2bcd(b) (__bin2bcd[(b)&0xff]) 180 181 #endif /* __LIBKERN_H__ */ 182