OpenBSD-4.6/share/man/man9/kern.9

Compare this file to the similar file:
Show the results in this format:

.\"	$OpenBSD: kern.9,v 1.12 2007/05/31 19:20:00 jmc Exp $
.\"
.\" Copyright (c) 2002, 2003 CubeSoft Communications, Inc.
.\" <http://www.csoft.org>
.\"
.\" 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: May 31 2007 $
.Dt KERN 9
.Os
.Sh NAME
.Nm kern
.Nd kernel library routines
.Sh SYNOPSIS
.Fd #include <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"
.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
tests are only included if the kernel has
.Dv DIAGNOSTIC
enabled.
.Fn KDASSERT
tests are only included if the kernel has
.Dv DEBUG
enabled.
.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 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"
.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 remaining functions have the same semantics as their libc counterparts,
.Xr 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 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 strlcpy 3 ,
.Xr strlcat 3 ,
.Xr strcmp 3 ,
.Xr strncmp 3
and
.Xr strncasecmp 3 .
.Sh RANDOM NUMBER GENERATION
.nr nS 1
.Ft u_long
.Fn random "void"
.Ft void
.Fn srandom "u_long seed"
.nr nS 0
.Pp
The
.Fn random
function returns a random number.
The
.Fn srandom
function initializes the random seed.
.Fn random
will by default produce a sequence of numbers that can be duplicated
by calling
.Fn srandom
with `1' as the seed.
The
.Fn random
function is discouraged in favor of
.Xr arc4random 9 .
.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 ,
.Xr arc4random 9
.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.