.\" $OpenBSD: kern.9,v 1.20 2014/07/14 06:44:11 jmc Exp $ .\" .\" Copyright (c) 2002, 2003 CubeSoft Communications, Inc. .\" .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES .\" (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .Dd $Mdocdate: July 14 2014 $ .Dt KERN 9 .Os .Sh NAME .Nm kern .Nd kernel library routines .Sh SYNOPSIS .In lib/libkern/libkern.h .Sh DESCRIPTION The .Nm library implements a set of useful functions and macros inside the kernel. .Sh MATH .nr nS 1 .Ft int .Fn imax "int a" "int b" .Ft int .Fn imin "int a" "int b" .Ft long .Fn lmax "long a" "long b" .Ft long .Fn lmin "long a" "long b" .Ft u_int .Fn max "u_int a" "u_int b" .Ft u_int .Fn min "u_int a" "u_int b" .Ft u_long .Fn ulmax "u_long a" "u_long b" .Ft u_long .Fn ulmin "u_long a" "u_long b" .Ft int .Fn abs "int j" .nr nS 0 .Pp The .Fn min , .Fn imin , .Fn lmin and .Fn ulmin functions return the smallest integer between .Fa a and .Fa b , inclusive. The .Fn max , .Fn imax , .Fn lmax and .Fn ulmax functions return the largest integer between .Fa a and .Fa b , inclusive. .Pp The .Fn abs function computes the absolute value of integer .Fa j . .Sh ASSERTIONS .nr nS 1 .Ft "void" .Fn assert "CONDITION" .Ft "void" .Fn KASSERT "CONDITION" .Ft "void" .Fn KDASSERT "CONDITION" .Ft "void" .Fn KASSERTMSG "CONDITION" "fmt" "..." .Ft "void" .Fn KDASSERTMSG "CONDITION" "fmt" "..." .nr nS 0 .Pp These macros cause kernel .Xr panic 9 if the given condition evaluates to false. .Fn assert tests are always compiled in. .Fn KASSERT and .Fn KASSERTMSG tests are only included if the kernel has .Dv DIAGNOSTIC enabled. .Fn KDASSERT and .Fn KDASSERTMSG tests are only included if the kernel has .Dv DEBUG enabled. The .Fn KASSERTMSG and .Fn KDASSERTMSG macros append to the .Xr panic 9 format string the message specified by .Fa format and its subsequent arguments, similar to .Xr printf 9 functions. .Pp .nr nS 1 .Fn CTASSERT "CONDITION" .nr nS 0 .Pp This macro causes a compile time error if the given condition evaluates to false. Its main purpose is to verify assertions about type and struct sizes that would otherwise make the code fail at run time. .Fn CTASSERT can be used in global scope or at the start of blocks, where variable declarations are allowed. .Sh BYTE STRINGS .nr nS 1 .Ft int .Fn skpc "int mask" "size_t size" "u_char *cp" .Ft int .Fn scanc "u_int size" "const u_char *cp" "const u_char *table" "int mask" .Ft int .Fn bcmp "const void *b1" "const void *b2" "size_t len" .Ft int .Fn timingsafe_bcmp "const void *b1" "const void *b2" "size_t len" .Ft void * .Fn memchr "const void *b" "int c" "size_t len" .Ft int .Fn memcmp "const void *b1" "const void *b2" "size_t len" .Ft int .Fn ffs "int value" .Ft int .Fn fls "int value" .Ft int .Fn flsl "long value" .nr nS 0 .Pp The .Fn skpc function locates the first unsigned character of value different than .Fa mask inside the string .Fa cp . .Pp The .Fn scanc function expects a string of indexes into the table .Fa table . Each table element is bitwise ANDed against .Fa mask . .Pp .Fn skpc and .Fn scanc expect the string to be of size .Fa size , and return the index relative to the end of the string where the match occurred, or zero if .Fa mask is not present in the string. .Pp The .Fn fls and .Fn flsl functions find the last bit set in .Fa value and return the index of that bit. .Pp The remaining functions have the same semantics as their libc counterparts, .Xr bcmp 3 , .Xr timingsafe_bcmp 3 , .Xr memchr 3 , .Xr memcmp 3 and .Xr ffs 3 . .Sh CHARACTER STRINGS .nr nS 1 .Ft size_t .Fn strlen "const char *s" .Ft char * .Fn strncpy "char *dst" "const char *src" "size_t len" .Ft size_t .Fn strnlen "const char *s" "size_t maxlen" .Ft size_t .Fn strlcpy "char *dst" "const char *src" "size_t size" .Ft size_t .Fn strlcat "char *dst" "const char *src" "size_t size" .Ft int .Fn strcmp "const char *s1" "const char *s2" .Ft int .Fn strncmp "const char *s1" "const char *s2" "size_t len" .Ft int .Fn strncasecmp "const char *s1" "const char *s2" "size_tlen" .nr nS 0 .Pp Those functions have the same semantics as their libc counterparts, .Xr strlen 3 , .Xr strncpy 3 , .Xr strnlen 3 , .Xr strlcpy 3 , .Xr strlcat 3 , .Xr strcmp 3 , .Xr strncmp 3 and .Xr strncasecmp 3 . .Sh MISCELLANEOUS .nr nS 1 .Ft int .Fn getsn "char *cp" "int size" .nr nS 0 .Pp The .Fn getsn function reads user input from the console and returns on newline. The result is written into .Fa cp , which is assumed to be .Fa size bytes long. .Sh SEE ALSO .Xr assert 3 , .Xr bcmp 3 , .Xr ffs 3 , .Xr memchr 3 , .Xr memcmp 3 , .Xr string 3 .Sh STANDARDS The .Fn abs , .Fn memchr , .Fn memcmp , .Fn strlen , .Fn strncpy , .Fn strcmp , .Fn strncmp and .Fn strcasecmp functions conform to .St -ansiC . .Sh HISTORY The .Fn skpc and .Fn scanc functions are based on vax instructions of the same name. The .Fn KASSERTMSG and .Fn KDASSERTMSG macros are taken from .Nx .