13a7d8294SWarner Losh.\" Copyright (c) Michael Smith 23a7d8294SWarner Losh.\" All rights reserved. 33a7d8294SWarner Losh.\" 43a7d8294SWarner Losh.\" Redistribution and use in source and binary forms, with or without 53a7d8294SWarner Losh.\" modification, are permitted provided that the following conditions 63a7d8294SWarner Losh.\" are met: 73a7d8294SWarner Losh.\" 1. Redistributions of source code must retain the above copyright 83a7d8294SWarner Losh.\" notice, this list of conditions and the following disclaimer. 93a7d8294SWarner Losh.\" 2. Redistributions in binary form must reproduce the above copyright 103a7d8294SWarner Losh.\" notice, this list of conditions and the following disclaimer in the 113a7d8294SWarner Losh.\" documentation and/or other materials provided with the distribution. 123a7d8294SWarner Losh.\" 133a7d8294SWarner Losh.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 143a7d8294SWarner Losh.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 153a7d8294SWarner Losh.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 163a7d8294SWarner Losh.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 173a7d8294SWarner Losh.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 183a7d8294SWarner Losh.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 193a7d8294SWarner Losh.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 203a7d8294SWarner Losh.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 213a7d8294SWarner Losh.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 223a7d8294SWarner Losh.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 233a7d8294SWarner Losh.\" SUCH DAMAGE. 243a7d8294SWarner Losh.\" 253a7d8294SWarner Losh.\" $FreeBSD$ 263a7d8294SWarner Losh.\" 27*45ad9557SWarner Losh.Dd September 9, 2022 28f6b2a429SKyle Evans.Dt LIBSA 3 293a7d8294SWarner Losh.Os 303a7d8294SWarner Losh.Sh NAME 31f6b2a429SKyle Evans.Nm libsa 323a7d8294SWarner Losh.Nd support library for standalone executables 333a7d8294SWarner Losh.Sh SYNOPSIS 343a7d8294SWarner Losh.In stand.h 353a7d8294SWarner Losh.Sh DESCRIPTION 363a7d8294SWarner LoshThe 373a7d8294SWarner Losh.Nm 383a7d8294SWarner Loshlibrary provides a set of supporting functions for standalone 393a7d8294SWarner Loshapplications, mimicking where possible the standard 403a7d8294SWarner Losh.Bx 413a7d8294SWarner Loshprogramming 423a7d8294SWarner Loshenvironment. 433a7d8294SWarner LoshThe following sections group these functions by kind. 443a7d8294SWarner LoshUnless specifically described here, see the corresponding section 3 453a7d8294SWarner Loshmanpages for the given functions. 463a7d8294SWarner Losh.Sh STRING FUNCTIONS 473a7d8294SWarner LoshString functions are available as documented in 483a7d8294SWarner Losh.Xr string 3 493a7d8294SWarner Loshand 503a7d8294SWarner Losh.Xr bstring 3 . 513a7d8294SWarner Losh.Sh MEMORY ALLOCATION 523a7d8294SWarner Losh.Bl -hang -width 10n 533a7d8294SWarner Losh.It Xo 543a7d8294SWarner Losh.Ft "void *" 553a7d8294SWarner Losh.Fn malloc "size_t size" 563a7d8294SWarner Losh.Xc 573a7d8294SWarner Losh.Pp 583a7d8294SWarner LoshAllocate 593a7d8294SWarner Losh.Fa size 603a7d8294SWarner Loshbytes of memory from the heap using a best-fit algorithm. 613a7d8294SWarner Losh.It Xo 623a7d8294SWarner Losh.Ft void 633a7d8294SWarner Losh.Fn free "void *ptr" 643a7d8294SWarner Losh.Xc 653a7d8294SWarner Losh.Pp 663a7d8294SWarner LoshFree the allocated object at 673a7d8294SWarner Losh.Fa ptr . 683a7d8294SWarner Losh.It Xo 693a7d8294SWarner Losh.Ft void 703a7d8294SWarner Losh.Fn setheap "void *start" "void *limit" 713a7d8294SWarner Losh.Xc 723a7d8294SWarner Losh.Pp 733a7d8294SWarner LoshInitialise the heap. 743a7d8294SWarner LoshThis function must be called before calling 753a7d8294SWarner Losh.Fn alloc 763a7d8294SWarner Loshfor the first time. 773a7d8294SWarner LoshThe region between 783a7d8294SWarner Losh.Fa start 793a7d8294SWarner Loshand 803a7d8294SWarner Losh.Fa limit 813a7d8294SWarner Loshwill be used for the heap; attempting to allocate beyond this will result 823a7d8294SWarner Loshin a panic. 833a7d8294SWarner Losh.It Xo 843a7d8294SWarner Losh.Ft "char *" 853a7d8294SWarner Losh.Fn sbrk "int junk" 863a7d8294SWarner Losh.Xc 873a7d8294SWarner Losh.Pp 883a7d8294SWarner LoshProvides the behaviour of 893a7d8294SWarner Losh.Fn sbrk 0 , 903a7d8294SWarner Loshi.e., returns the highest point that the heap has reached. 913a7d8294SWarner LoshThis value can 923a7d8294SWarner Loshbe used during testing to determine the actual heap usage. 933a7d8294SWarner LoshThe 943a7d8294SWarner Losh.Fa junk 953a7d8294SWarner Loshargument is ignored. 963a7d8294SWarner Losh.El 973a7d8294SWarner Losh.Sh ENVIRONMENT 983a7d8294SWarner LoshA set of functions are provided for manipulating a flat variable space similar 993a7d8294SWarner Loshto the traditional shell-supported environment. 1003a7d8294SWarner LoshMajor enhancements are support 1013a7d8294SWarner Loshfor set/unset hook functions. 1023a7d8294SWarner Losh.Bl -hang -width 10n 1033a7d8294SWarner Losh.It Xo 1043a7d8294SWarner Losh.Ft "char *" 1053a7d8294SWarner Losh.Fn getenv "const char *name" 1063a7d8294SWarner Losh.Xc 1073a7d8294SWarner Losh.It Xo 1083a7d8294SWarner Losh.Ft int 1093a7d8294SWarner Losh.Fn setenv "const char *name" "const char *value" "int overwrite" 1103a7d8294SWarner Losh.Xc 1113a7d8294SWarner Losh.It Xo 1123a7d8294SWarner Losh.Ft int 1133a7d8294SWarner Losh.Fn putenv "char *string" 1143a7d8294SWarner Losh.Xc 1153a7d8294SWarner Losh.It Xo 1163a7d8294SWarner Losh.Ft int 1173a7d8294SWarner Losh.Fn unsetenv "const char *name" 1183a7d8294SWarner Losh.Xc 1193a7d8294SWarner Losh.Pp 1203a7d8294SWarner LoshThese functions behave similarly to their standard library counterparts. 1213a7d8294SWarner Losh.It Xo 1223a7d8294SWarner Losh.Ft "struct env_var *" 1233a7d8294SWarner Losh.Fn env_getenv "const char *name" 1243a7d8294SWarner Losh.Xc 1253a7d8294SWarner Losh.Pp 1263a7d8294SWarner LoshLooks up a variable in the environment and returns its entire 1273a7d8294SWarner Loshdata structure. 1283a7d8294SWarner Losh.It Xo 1293a7d8294SWarner Losh.Ft int 1303a7d8294SWarner Losh.Fn env_setenv "const char *name" "int flags" "const void *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook" 1313a7d8294SWarner Losh.Xc 1323a7d8294SWarner Losh.Pp 1333a7d8294SWarner LoshCreates a new or sets an existing environment variable called 1343a7d8294SWarner Losh.Fa name . 1353a7d8294SWarner LoshIf creating a new variable, the 1363a7d8294SWarner Losh.Fa sethook 1373a7d8294SWarner Loshand 1383a7d8294SWarner Losh.Fa unsethook 1393a7d8294SWarner Losharguments may be specified. 1403a7d8294SWarner Losh.Pp 1413a7d8294SWarner LoshThe set hook is invoked whenever an attempt 1423a7d8294SWarner Loshis made to set the variable, unless the EV_NOHOOK flag is set. 1433a7d8294SWarner LoshTypically 1443a7d8294SWarner Losha set hook will validate the 1453a7d8294SWarner Losh.Fa value 1463a7d8294SWarner Loshargument, and then call 1473a7d8294SWarner Losh.Fn env_setenv 1483a7d8294SWarner Loshagain with EV_NOHOOK set to actually save the value. 1493a7d8294SWarner LoshThe predefined function 1503a7d8294SWarner Losh.Fn env_noset 1513a7d8294SWarner Loshmay be specified to refuse all attempts to set a variable. 1523a7d8294SWarner Losh.Pp 1533a7d8294SWarner LoshThe unset hook is invoked when an attempt is made to unset a variable. 1543a7d8294SWarner LoshIf it 1553a7d8294SWarner Loshreturns zero, the variable will be unset. 1563a7d8294SWarner LoshThe predefined function 1573a7d8294SWarner Losh.Fa env_nounset 1583a7d8294SWarner Loshmay be used to prevent a variable being unset. 1593a7d8294SWarner Losh.El 1603a7d8294SWarner Losh.Sh STANDARD LIBRARY SUPPORT 1613a7d8294SWarner Losh.Bl -hang -width 10n 1623a7d8294SWarner Losh.It Xo 1633a7d8294SWarner Losh.Ft int 1643a7d8294SWarner Losh.Fn abs "int i" 1653a7d8294SWarner Losh.Xc 1663a7d8294SWarner Losh.It Xo 1673a7d8294SWarner Losh.Ft int 1683a7d8294SWarner Losh.Fn getopt "int argc" "char * const *argv" "const char *optstring" 1693a7d8294SWarner Losh.Xc 1703a7d8294SWarner Losh.It Xo 1713a7d8294SWarner Losh.Ft long 1723a7d8294SWarner Losh.Fn strtol "const char *nptr" "char **endptr" "int base" 1733a7d8294SWarner Losh.Xc 1743a7d8294SWarner Losh.It Xo 1753a7d8294SWarner Losh.Ft long long 1763a7d8294SWarner Losh.Fn strtoll "const char *nptr" "char **endptr" "int base" 1773a7d8294SWarner Losh.Xc 1783a7d8294SWarner Losh.It Xo 1793a7d8294SWarner Losh.Ft long 1803a7d8294SWarner Losh.Fn strtoul "const char *nptr" "char **endptr" "int base" 1813a7d8294SWarner Losh.Xc 1823a7d8294SWarner Losh.It Xo 1833a7d8294SWarner Losh.Ft long long 1843a7d8294SWarner Losh.Fn strtoull "const char *nptr" "char **endptr" "int base" 1853a7d8294SWarner Losh.Xc 1863a7d8294SWarner Losh.It Xo 1873a7d8294SWarner Losh.Ft void 1883a7d8294SWarner Losh.Fn srandom "unsigned int seed" 1893a7d8294SWarner Losh.Xc 1903a7d8294SWarner Losh.It Xo 1913a7d8294SWarner Losh.Ft "long" 1923a7d8294SWarner Losh.Fn random void 1933a7d8294SWarner Losh.Xc 1943a7d8294SWarner Losh.It Xo 1953a7d8294SWarner Losh.Ft "char *" 1963a7d8294SWarner Losh.Fn strerror "int error" 1973a7d8294SWarner Losh.Xc 1983a7d8294SWarner Losh.Pp 1993a7d8294SWarner LoshReturns error messages for the subset of errno values supported by 2003a7d8294SWarner Losh.Nm . 2013a7d8294SWarner Losh.It Fn assert expression 2023a7d8294SWarner Losh.Pp 2033a7d8294SWarner LoshRequires 2043a7d8294SWarner Losh.In assert.h . 2053a7d8294SWarner Losh.It Xo 2063a7d8294SWarner Losh.Ft int 2073a7d8294SWarner Losh.Fn setjmp "jmp_buf env" 2083a7d8294SWarner Losh.Xc 2093a7d8294SWarner Losh.It Xo 2103a7d8294SWarner Losh.Ft void 2113a7d8294SWarner Losh.Fn longjmp "jmp_buf env" "int val" 2123a7d8294SWarner Losh.Xc 2133a7d8294SWarner Losh.Pp 2143a7d8294SWarner LoshDefined as 2153a7d8294SWarner Losh.Fn _setjmp 2163a7d8294SWarner Loshand 2173a7d8294SWarner Losh.Fn _longjmp 2183a7d8294SWarner Loshrespectively as there is no signal state to manipulate. 2193a7d8294SWarner LoshRequires 2203a7d8294SWarner Losh.In setjmp.h . 2213a7d8294SWarner Losh.El 2223a7d8294SWarner Losh.Sh CHARACTER I/O 2233a7d8294SWarner Losh.Bl -hang -width 10n 2243a7d8294SWarner Losh.It Xo 2253a7d8294SWarner Losh.Ft void 2263a7d8294SWarner Losh.Fn gets "char *buf" 2273a7d8294SWarner Losh.Xc 2283a7d8294SWarner Losh.Pp 2293a7d8294SWarner LoshRead characters from the console into 2303a7d8294SWarner Losh.Fa buf . 2313a7d8294SWarner LoshAll of the standard cautions apply to this function. 2323a7d8294SWarner Losh.It Xo 2333a7d8294SWarner Losh.Ft void 2343a7d8294SWarner Losh.Fn ngets "char *buf" "int size" 2353a7d8294SWarner Losh.Xc 2363a7d8294SWarner Losh.Pp 2373a7d8294SWarner LoshRead at most 2383a7d8294SWarner Losh.Fa size 2393a7d8294SWarner Losh- 1 characters from the console into 2403a7d8294SWarner Losh.Fa buf . 2413a7d8294SWarner LoshIf 2423a7d8294SWarner Losh.Fa size 2433a7d8294SWarner Loshis less than 1, the function's behaviour is as for 2443a7d8294SWarner Losh.Fn gets . 2453a7d8294SWarner Losh.It Xo 2463a7d8294SWarner Losh.Ft int 2473a7d8294SWarner Losh.Fn fgetstr "char *buf" "int size" "int fd" 2483a7d8294SWarner Losh.Xc 2493a7d8294SWarner Losh.Pp 2503a7d8294SWarner LoshRead a line of at most 2513a7d8294SWarner Losh.Fa size 2523a7d8294SWarner Loshcharacters into 2533a7d8294SWarner Losh.Fa buf . 2543a7d8294SWarner LoshLine terminating characters are stripped, and the buffer is always 2553a7d8294SWarner Losh.Dv NUL 2563a7d8294SWarner Loshterminated. 2573a7d8294SWarner LoshReturns the number of characters in 2583a7d8294SWarner Losh.Fa buf 2593a7d8294SWarner Loshif successful, or -1 if a read error occurs. 2603a7d8294SWarner Losh.It Xo 2613a7d8294SWarner Losh.Ft int 2623a7d8294SWarner Losh.Fn printf "const char *fmt" "..." 2633a7d8294SWarner Losh.Xc 2643a7d8294SWarner Losh.It Xo 2653a7d8294SWarner Losh.Ft void 2663a7d8294SWarner Losh.Fn vprintf "const char *fmt" "va_list ap" 2673a7d8294SWarner Losh.Xc 2683a7d8294SWarner Losh.It Xo 2693a7d8294SWarner Losh.Ft int 2703a7d8294SWarner Losh.Fn sprintf "char *buf" "const char *fmt" "..." 2713a7d8294SWarner Losh.Xc 2723a7d8294SWarner Losh.It Xo 2733a7d8294SWarner Losh.Ft void 2743a7d8294SWarner Losh.Fn vsprintf "char *buf" "const char *fmt" "va_list ap" 2753a7d8294SWarner Losh.Xc 2763a7d8294SWarner Losh.Pp 2773a7d8294SWarner LoshThe *printf functions implement a subset of the standard 2783a7d8294SWarner Losh.Fn printf 2793a7d8294SWarner Loshfamily functionality and some extensions. 2803a7d8294SWarner LoshThe following standard conversions 2813a7d8294SWarner Loshare supported: c,d,n,o,p,s,u,x. 2823a7d8294SWarner LoshThe following modifiers are supported: 2833a7d8294SWarner Losh+,-,#,*,0,field width,precision,l. 2843a7d8294SWarner Losh.Pp 2853a7d8294SWarner LoshThe 2863a7d8294SWarner Losh.Li b 2873a7d8294SWarner Loshconversion is provided to decode error registers. 2883a7d8294SWarner LoshIts usage is: 2893a7d8294SWarner Losh.Bd -ragged -offset indent 2903a7d8294SWarner Loshprintf( 2913a7d8294SWarner Losh.Qq reg=%b\en , 2923a7d8294SWarner Loshregval, 2933a7d8294SWarner Losh.Qq <base><arg>* 2943a7d8294SWarner Losh); 2953a7d8294SWarner Losh.Ed 2963a7d8294SWarner Losh.Pp 2973a7d8294SWarner Loshwhere <base> is the output expressed as a control character, e.g.\& \e10 gives 2983a7d8294SWarner Loshoctal, \e20 gives hex. 2993a7d8294SWarner LoshEach <arg> is a sequence of characters, the first of 3003a7d8294SWarner Loshwhich gives the bit number to be inspected (origin 1) and the next characters 3013a7d8294SWarner Losh(up to a character less than 32) give the text to be displayed if the bit is set. 3023a7d8294SWarner LoshThus 3033a7d8294SWarner Losh.Bd -ragged -offset indent 3043a7d8294SWarner Loshprintf( 3053a7d8294SWarner Losh.Qq reg=%b\en , 3063a7d8294SWarner Losh3, 3073a7d8294SWarner Losh.Qq \e10\e2BITTWO\e1BITONE 3083a7d8294SWarner Losh); 3093a7d8294SWarner Losh.Ed 3103a7d8294SWarner Losh.Pp 3113a7d8294SWarner Loshwould give the output 3123a7d8294SWarner Losh.Bd -ragged -offset indent 3133a7d8294SWarner Loshreg=3<BITTWO,BITONE> 3143a7d8294SWarner Losh.Ed 3153a7d8294SWarner Losh.Pp 3163a7d8294SWarner LoshThe 3173a7d8294SWarner Losh.Li D 3183a7d8294SWarner Loshconversion provides a hexdump facility, e.g. 3193a7d8294SWarner Losh.Bd -ragged -offset indent 3203a7d8294SWarner Loshprintf( 3213a7d8294SWarner Losh.Qq %6D , 3223a7d8294SWarner Loshptr, 3233a7d8294SWarner Losh.Qq \&: 3243a7d8294SWarner Losh); gives 3253a7d8294SWarner Losh.Qq XX:XX:XX:XX:XX:XX 3263a7d8294SWarner Losh.Ed 3273a7d8294SWarner Losh.Bd -ragged -offset indent 3283a7d8294SWarner Loshprintf( 3293a7d8294SWarner Losh.Qq %*D , 3303a7d8294SWarner Loshlen, 3313a7d8294SWarner Loshptr, 3323a7d8294SWarner Losh.Qq "\ " 3333a7d8294SWarner Losh); gives 3343a7d8294SWarner Losh.Qq XX XX XX ... 3353a7d8294SWarner Losh.Ed 3363a7d8294SWarner Losh.El 3373a7d8294SWarner Losh.Sh CHARACTER TESTS AND CONVERSIONS 3383a7d8294SWarner Losh.Bl -hang -width 10n 3393a7d8294SWarner Losh.It Xo 3403a7d8294SWarner Losh.Ft int 3413a7d8294SWarner Losh.Fn isupper "int c" 3423a7d8294SWarner Losh.Xc 3433a7d8294SWarner Losh.It Xo 3443a7d8294SWarner Losh.Ft int 3453a7d8294SWarner Losh.Fn islower "int c" 3463a7d8294SWarner Losh.Xc 3473a7d8294SWarner Losh.It Xo 3483a7d8294SWarner Losh.Ft int 3493a7d8294SWarner Losh.Fn isspace "int c" 3503a7d8294SWarner Losh.Xc 3513a7d8294SWarner Losh.It Xo 3523a7d8294SWarner Losh.Ft int 3533a7d8294SWarner Losh.Fn isdigit "int c" 3543a7d8294SWarner Losh.Xc 3553a7d8294SWarner Losh.It Xo 3563a7d8294SWarner Losh.Ft int 3573a7d8294SWarner Losh.Fn isxdigit "int c" 3583a7d8294SWarner Losh.Xc 3593a7d8294SWarner Losh.It Xo 3603a7d8294SWarner Losh.Ft int 3613a7d8294SWarner Losh.Fn isascii "int c" 3623a7d8294SWarner Losh.Xc 3633a7d8294SWarner Losh.It Xo 3643a7d8294SWarner Losh.Ft int 3653a7d8294SWarner Losh.Fn isalpha "int c" 3663a7d8294SWarner Losh.Xc 3673a7d8294SWarner Losh.It Xo 3683a7d8294SWarner Losh.Ft int 3693a7d8294SWarner Losh.Fn isalnum "int c" 3703a7d8294SWarner Losh.Xc 3713a7d8294SWarner Losh.It Xo 3723a7d8294SWarner Losh.Ft int 3733a7d8294SWarner Losh.Fn iscntrl "int c" 3743a7d8294SWarner Losh.Xc 3753a7d8294SWarner Losh.It Xo 3763a7d8294SWarner Losh.Ft int 3773a7d8294SWarner Losh.Fn isgraph "int c" 3783a7d8294SWarner Losh.Xc 3793a7d8294SWarner Losh.It Xo 3803a7d8294SWarner Losh.Ft int 3813a7d8294SWarner Losh.Fn ispunct "int c" 3823a7d8294SWarner Losh.Xc 3833a7d8294SWarner Losh.It Xo 3843a7d8294SWarner Losh.Ft int 3853a7d8294SWarner Losh.Fn toupper "int c" 3863a7d8294SWarner Losh.Xc 3873a7d8294SWarner Losh.It Xo 3883a7d8294SWarner Losh.Ft int 3893a7d8294SWarner Losh.Fn tolower "int c" 3903a7d8294SWarner Losh.Xc 3913a7d8294SWarner Losh.El 3923a7d8294SWarner Losh.Sh FILE I/O 3933a7d8294SWarner Losh.Bl -hang -width 10n 3943a7d8294SWarner Losh.It Xo 3953a7d8294SWarner Losh.Ft int 3963a7d8294SWarner Losh.Fn open "const char *path" "int flags" 3973a7d8294SWarner Losh.Xc 3983a7d8294SWarner Losh.Pp 3993a7d8294SWarner LoshSimilar to the behaviour as specified in 4003a7d8294SWarner Losh.Xr open 2 , 4013a7d8294SWarner Loshexcept that file creation is not supported, so the mode parameter is not 4023a7d8294SWarner Loshrequired. 4033a7d8294SWarner LoshThe 4043a7d8294SWarner Losh.Fa flags 405f6b2a429SKyle Evansargument may be one of O_RDONLY, O_WRONLY and O_RDWR. 406f6b2a429SKyle EvansOnly UFS currently supports writing. 4073a7d8294SWarner Losh.It Xo 4083a7d8294SWarner Losh.Ft int 4093a7d8294SWarner Losh.Fn close "int fd" 4103a7d8294SWarner Losh.Xc 4113a7d8294SWarner Losh.It Xo 4123a7d8294SWarner Losh.Ft void 4133a7d8294SWarner Losh.Fn closeall void 4143a7d8294SWarner Losh.Xc 4153a7d8294SWarner Losh.Pp 4163a7d8294SWarner LoshClose all open files. 4173a7d8294SWarner Losh.It Xo 4183a7d8294SWarner Losh.Ft ssize_t 4193a7d8294SWarner Losh.Fn read "int fd" "void *buf" "size_t len" 4203a7d8294SWarner Losh.Xc 4213a7d8294SWarner Losh.It Xo 4223a7d8294SWarner Losh.Ft ssize_t 4233a7d8294SWarner Losh.Fn write "int fd" "void *buf" "size_t len" 4243a7d8294SWarner Losh.Xc 4253a7d8294SWarner Losh.Pp 4263a7d8294SWarner Losh(No file systems currently support writing.) 4273a7d8294SWarner Losh.It Xo 4283a7d8294SWarner Losh.Ft off_t 4293a7d8294SWarner Losh.Fn lseek "int fd" "off_t offset" "int whence" 4303a7d8294SWarner Losh.Xc 4313a7d8294SWarner Losh.Pp 4323a7d8294SWarner LoshFiles being automatically uncompressed during reading cannot seek backwards 4333a7d8294SWarner Loshfrom the current point. 4343a7d8294SWarner Losh.It Xo 4353a7d8294SWarner Losh.Ft int 4363a7d8294SWarner Losh.Fn stat "const char *path" "struct stat *sb" 4373a7d8294SWarner Losh.Xc 4383a7d8294SWarner Losh.It Xo 4393a7d8294SWarner Losh.Ft int 4403a7d8294SWarner Losh.Fn fstat "int fd" "struct stat *sb" 4413a7d8294SWarner Losh.Xc 4423a7d8294SWarner Losh.Pp 4433a7d8294SWarner LoshThe 4443a7d8294SWarner Losh.Fn stat 4453a7d8294SWarner Loshand 4463a7d8294SWarner Losh.Fn fstat 4473a7d8294SWarner Loshfunctions only fill out the following fields in the 4483a7d8294SWarner Losh.Fa sb 4493a7d8294SWarner Loshstructure: st_mode,st_nlink,st_uid,st_gid,st_size. 4503a7d8294SWarner LoshThe 4513a7d8294SWarner Losh.Nm tftp 4523a7d8294SWarner Loshfile system cannot provide meaningful values for this call, and the 4533a7d8294SWarner Losh.Nm cd9660 4543a7d8294SWarner Loshfile system always reports files having uid/gid of zero. 4553a7d8294SWarner Losh.El 4563a7d8294SWarner Losh.Sh PAGER 4573a7d8294SWarner LoshThe 4583a7d8294SWarner Losh.Nm 4593a7d8294SWarner Loshlibrary supplies a simple internal pager to ease reading the output of large 4603a7d8294SWarner Loshcommands. 4613a7d8294SWarner Losh.Bl -hang -width 10n 4623a7d8294SWarner Losh.It Xo 4633a7d8294SWarner Losh.Ft void 4643a7d8294SWarner Losh.Fn pager_open 4653a7d8294SWarner Losh.Xc 4663a7d8294SWarner Losh.Pp 4673a7d8294SWarner LoshInitialises the pager and tells it that the next line output will be the top of the 4683a7d8294SWarner Loshdisplay. 4693a7d8294SWarner LoshThe environment variable LINES is consulted to determine the number of 4703a7d8294SWarner Loshlines to be displayed before pausing. 4713a7d8294SWarner Losh.It Xo 4723a7d8294SWarner Losh.Ft void 4733a7d8294SWarner Losh.Fn pager_close void 4743a7d8294SWarner Losh.Xc 4753a7d8294SWarner Losh.Pp 4763a7d8294SWarner LoshCloses the pager. 4773a7d8294SWarner Losh.It Xo 4783a7d8294SWarner Losh.Ft int 4793a7d8294SWarner Losh.Fn pager_output "const char *lines" 4803a7d8294SWarner Losh.Xc 4813a7d8294SWarner Losh.Pp 4823a7d8294SWarner LoshSends the lines in the 4833a7d8294SWarner Losh.Dv NUL Ns 4843a7d8294SWarner Losh-terminated buffer at 4853a7d8294SWarner Losh.Fa lines 4863a7d8294SWarner Loshto the pager. 4873a7d8294SWarner LoshNewline characters are counted in order to determine the number 4883a7d8294SWarner Loshof lines being output (wrapped lines are not accounted for). 4893a7d8294SWarner LoshThe 4903a7d8294SWarner Losh.Fn pager_output 4913a7d8294SWarner Loshfunction will return zero when all of the lines have been output, or nonzero 4923a7d8294SWarner Loshif the display was paused and the user elected to quit. 4933a7d8294SWarner Losh.It Xo 4943a7d8294SWarner Losh.Ft int 4953a7d8294SWarner Losh.Fn pager_file "const char *fname" 4963a7d8294SWarner Losh.Xc 4973a7d8294SWarner Losh.Pp 4983a7d8294SWarner LoshAttempts to open and display the file 4993a7d8294SWarner Losh.Fa fname . 5003a7d8294SWarner LoshReturns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading. 5013a7d8294SWarner Losh.El 5023a7d8294SWarner Losh.Sh MISC 5033a7d8294SWarner Losh.Bl -hang -width 10n 5043a7d8294SWarner Losh.It Xo 505*45ad9557SWarner Losh.Ft char * 506*45ad9557SWarner Losh.Fn devformat "struct devdesc *" 507*45ad9557SWarner Losh.Xc 508*45ad9557SWarner Losh.Pp 509*45ad9557SWarner LoshFormat the specified device as a string. 510*45ad9557SWarner Losh.It Xo 5113a7d8294SWarner Losh.Ft void 5123a7d8294SWarner Losh.Fn twiddle void 5133a7d8294SWarner Losh.Xc 5143a7d8294SWarner Losh.Pp 5153a7d8294SWarner LoshSuccessive calls emit the characters in the sequence |,/,-,\\ followed by a 5163a7d8294SWarner Loshbackspace in order to provide reassurance to the user. 5173a7d8294SWarner Losh.El 5183a7d8294SWarner Losh.Sh REQUIRED LOW-LEVEL SUPPORT 5193a7d8294SWarner LoshThe following resources are consumed by 5203a7d8294SWarner Losh.Nm 5213a7d8294SWarner Losh- stack, heap, console and devices. 5223a7d8294SWarner Losh.Pp 5233a7d8294SWarner LoshThe stack must be established before 5243a7d8294SWarner Losh.Nm 5253a7d8294SWarner Loshfunctions can be invoked. 5263a7d8294SWarner LoshStack requirements vary depending on the functions 5273a7d8294SWarner Loshand file systems used by the consumer and the support layer functions detailed 5283a7d8294SWarner Loshbelow. 5293a7d8294SWarner Losh.Pp 5303a7d8294SWarner LoshThe heap must be established before calling 5313a7d8294SWarner Losh.Fn alloc 5323a7d8294SWarner Loshor 5333a7d8294SWarner Losh.Fn open 5343a7d8294SWarner Loshby calling 5353a7d8294SWarner Losh.Fn setheap . 5363a7d8294SWarner LoshHeap usage will vary depending on the number of simultaneously open files, 5373a7d8294SWarner Loshas well as client behaviour. 5383a7d8294SWarner LoshAutomatic decompression will allocate more 5393a7d8294SWarner Loshthan 64K of data per open file. 5403a7d8294SWarner Losh.Pp 5413a7d8294SWarner LoshConsole access is performed via the 5423a7d8294SWarner Losh.Fn getchar , 5433a7d8294SWarner Losh.Fn putchar 5443a7d8294SWarner Loshand 5453a7d8294SWarner Losh.Fn ischar 5463a7d8294SWarner Loshfunctions detailed below. 5473a7d8294SWarner Losh.Pp 5483a7d8294SWarner LoshDevice access is initiated via 5493a7d8294SWarner Losh.Fn devopen 5503a7d8294SWarner Loshand is performed through the 5513a7d8294SWarner Losh.Fn dv_strategy , 5523a7d8294SWarner Losh.Fn dv_ioctl 5533a7d8294SWarner Loshand 5543a7d8294SWarner Losh.Fn dv_close 5553a7d8294SWarner Loshfunctions in the device switch structure that 5563a7d8294SWarner Losh.Fn devopen 5573a7d8294SWarner Loshreturns. 5583a7d8294SWarner Losh.Pp 5593a7d8294SWarner LoshThe consumer must provide the following support functions: 5603a7d8294SWarner Losh.Bl -hang -width 10n 5613a7d8294SWarner Losh.It Xo 5623a7d8294SWarner Losh.Ft int 5633a7d8294SWarner Losh.Fn getchar void 5643a7d8294SWarner Losh.Xc 5653a7d8294SWarner Losh.Pp 5663a7d8294SWarner LoshReturn a character from the console, used by 5673a7d8294SWarner Losh.Fn gets , 5683a7d8294SWarner Losh.Fn ngets 5693a7d8294SWarner Loshand pager functions. 5703a7d8294SWarner Losh.It Xo 5713a7d8294SWarner Losh.Ft int 5723a7d8294SWarner Losh.Fn ischar void 5733a7d8294SWarner Losh.Xc 5743a7d8294SWarner Losh.Pp 5753a7d8294SWarner LoshReturns nonzero if a character is waiting from the console. 5763a7d8294SWarner Losh.It Xo 5773a7d8294SWarner Losh.Ft void 5783a7d8294SWarner Losh.Fn putchar int 5793a7d8294SWarner Losh.Xc 5803a7d8294SWarner Losh.Pp 5813a7d8294SWarner LoshWrite a character to the console, used by 5823a7d8294SWarner Losh.Fn gets , 5833a7d8294SWarner Losh.Fn ngets , 5843a7d8294SWarner Losh.Fn *printf , 5853a7d8294SWarner Losh.Fn panic 5863a7d8294SWarner Loshand 5873a7d8294SWarner Losh.Fn twiddle 5883a7d8294SWarner Loshand thus by many other functions for debugging and informational output. 5893a7d8294SWarner Losh.It Xo 5903a7d8294SWarner Losh.Ft int 5913a7d8294SWarner Losh.Fn devopen "struct open_file *of" "const char *name" "const char **file" 5923a7d8294SWarner Losh.Xc 5933a7d8294SWarner Losh.Pp 5943a7d8294SWarner LoshOpen the appropriate device for the file named in 5953a7d8294SWarner Losh.Fa name , 5963a7d8294SWarner Loshreturning in 5973a7d8294SWarner Losh.Fa file 5983a7d8294SWarner Losha pointer to the remaining body of 5993a7d8294SWarner Losh.Fa name 6003a7d8294SWarner Loshwhich does not refer to the device. 6013a7d8294SWarner LoshThe 6023a7d8294SWarner Losh.Va f_dev 6033a7d8294SWarner Loshfield in 6043a7d8294SWarner Losh.Fa of 6053a7d8294SWarner Loshwill be set to point to the 6063a7d8294SWarner Losh.Vt devsw 6073a7d8294SWarner Loshstructure for the opened device if successful. 6083a7d8294SWarner LoshDevice identifiers must 6093a7d8294SWarner Loshalways precede the path component, but may otherwise be arbitrarily formatted. 6103a7d8294SWarner LoshUsed by 6113a7d8294SWarner Losh.Fn open 6123a7d8294SWarner Loshand thus for all device-related I/O. 6133a7d8294SWarner Losh.It Xo 6143a7d8294SWarner Losh.Ft int 6153a7d8294SWarner Losh.Fn devclose "struct open_file *of" 6163a7d8294SWarner Losh.Xc 6173a7d8294SWarner Losh.Pp 6183a7d8294SWarner LoshClose the device allocated for 6193a7d8294SWarner Losh.Fa of . 6203a7d8294SWarner LoshThe device driver itself will already have been called for the close; this call 6213a7d8294SWarner Loshshould clean up any allocation made by devopen only. 6223a7d8294SWarner Losh.It Xo 6233a7d8294SWarner Losh.Ft void 624*45ad9557SWarner Losh.Fn __abort 6253a7d8294SWarner Losh.Xc 6263a7d8294SWarner Losh.Pp 6273a7d8294SWarner LoshCalls 6283a7d8294SWarner Losh.Fn panic 6293a7d8294SWarner Loshwith a fixed string. 6303a7d8294SWarner Losh.It Xo 6313a7d8294SWarner Losh.Ft void 6323a7d8294SWarner Losh.Fn panic "const char *msg" "..." 6333a7d8294SWarner Losh.Xc 6343a7d8294SWarner Losh.Pp 6353a7d8294SWarner LoshSignal a fatal and unrecoverable error condition. 6363a7d8294SWarner LoshThe 6373a7d8294SWarner Losh.Fa msg ... 6383a7d8294SWarner Losharguments are as for 6393a7d8294SWarner Losh.Fn printf . 6403a7d8294SWarner Losh.El 6413a7d8294SWarner Losh.Sh INTERNAL FILE SYSTEMS 6423a7d8294SWarner LoshInternal file systems are enabled by the consumer exporting the array 6433a7d8294SWarner Losh.Vt struct fs_ops *file_system[] , 6443a7d8294SWarner Loshwhich should be initialised with pointers 6453a7d8294SWarner Loshto 6463a7d8294SWarner Losh.Vt struct fs_ops 6473a7d8294SWarner Loshstructures. 6483a7d8294SWarner LoshThe following file system handlers are supplied by 6493a7d8294SWarner Losh.Nm , 6503a7d8294SWarner Loshthe consumer may supply other file systems of their own: 6513a7d8294SWarner Losh.Bl -hang -width ".Va cd9660_fsops" 6523a7d8294SWarner Losh.It Va ufs_fsops 6533a7d8294SWarner LoshThe 6543a7d8294SWarner Losh.Bx 6553a7d8294SWarner LoshUFS. 6563a7d8294SWarner Losh.It Va ext2fs_fsops 6573a7d8294SWarner LoshLinux ext2fs file system. 6583a7d8294SWarner Losh.It Va tftp_fsops 6593a7d8294SWarner LoshFile access via TFTP. 6603a7d8294SWarner Losh.It Va nfs_fsops 6613a7d8294SWarner LoshFile access via NFS. 6623a7d8294SWarner Losh.It Va cd9660_fsops 6633a7d8294SWarner LoshISO 9660 (CD-ROM) file system. 6643a7d8294SWarner Losh.It Va gzipfs_fsops 6653a7d8294SWarner LoshStacked file system supporting gzipped files. 6663a7d8294SWarner LoshWhen trying the gzipfs file system, 6673a7d8294SWarner Losh.Nm 6683a7d8294SWarner Loshappends 6693a7d8294SWarner Losh.Li .gz 6703a7d8294SWarner Loshto the end of the filename, and then tries to locate the file using the other 6713a7d8294SWarner Loshfile systems. 6723a7d8294SWarner LoshPlacement of this file system in the 6733a7d8294SWarner Losh.Va file_system[] 6743a7d8294SWarner Losharray determines whether gzipped files will be opened in preference to non-gzipped 6753a7d8294SWarner Loshfiles. 6763a7d8294SWarner LoshIt is only possible to seek a gzipped file forwards, and 6773a7d8294SWarner Losh.Fn stat 6783a7d8294SWarner Loshand 6793a7d8294SWarner Losh.Fn fstat 6803a7d8294SWarner Loshon gzipped files will report an invalid length. 6813a7d8294SWarner Losh.It Va bzipfs_fsops 6823a7d8294SWarner LoshThe same as 6833a7d8294SWarner Losh.Va gzipfs_fsops , 6843a7d8294SWarner Loshbut for 6853a7d8294SWarner Losh.Xr bzip2 1 Ns -compressed 6863a7d8294SWarner Loshfiles. 6873a7d8294SWarner Losh.El 6883a7d8294SWarner Losh.Pp 6893a7d8294SWarner LoshThe array of 6903a7d8294SWarner Losh.Vt struct fs_ops 6913a7d8294SWarner Loshpointers should be terminated with a NULL. 6923a7d8294SWarner Losh.Sh DEVICES 6933a7d8294SWarner LoshDevices are exported by the supporting code via the array 6943a7d8294SWarner Losh.Vt struct devsw *devsw[] 6953a7d8294SWarner Loshwhich is a NULL terminated array of pointers to device switch structures. 696*45ad9557SWarner Losh.Sh DRIVER INTERFACE 697*45ad9557SWarner LoshThe driver needs to provide a common set of entry points that are 698*45ad9557SWarner Loshused by 699*45ad9557SWarner Losh.Nm libsa 700*45ad9557SWarner Loshto interface with the device. 701*45ad9557SWarner Losh.Bd -literal 702*45ad9557SWarner Loshstruct devsw { 703*45ad9557SWarner Losh const char dv_name[DEV_NAMLEN]; 704*45ad9557SWarner Losh int dv_type; 705*45ad9557SWarner Losh int (*dv_init)(void); 706*45ad9557SWarner Losh int (*dv_strategy)(void *devdata, int rw, daddr_t blk, 707*45ad9557SWarner Losh size_t size, char *buf, size_t *rsize); 708*45ad9557SWarner Losh int (*dv_open)(struct open_file *f, ...); 709*45ad9557SWarner Losh int (*dv_close)(struct open_file *f); 710*45ad9557SWarner Losh int (*dv_ioctl)(struct open_file *f, u_long cmd, void *data); 711*45ad9557SWarner Losh int (*dv_print)(int verbose); 712*45ad9557SWarner Losh void (*dv_cleanup)(void); 713*45ad9557SWarner Losh void (*dv_fmtdev)(struct devdesc *); 714*45ad9557SWarner Losh}; 715*45ad9557SWarner Losh.Ed 716*45ad9557SWarner Losh.Bl -tag -width ".Fn dv_strategy" 717*45ad9557SWarner Losh.It Fn dv_name 718*45ad9557SWarner LoshThe device's name. 719*45ad9557SWarner Losh.It Fn dv_type 720*45ad9557SWarner LoshType of device. 721*45ad9557SWarner LoshThe supported types are: 722*45ad9557SWarner Losh.Bl -tag -width "DEVT_NONE" 723*45ad9557SWarner Losh.It DEVT_NONE 724*45ad9557SWarner Losh.It DEVT_DISK 725*45ad9557SWarner Losh.It DEVT_NET 726*45ad9557SWarner Losh.It DEVT_CD 727*45ad9557SWarner Losh.It DEVT_ZFS 728*45ad9557SWarner Losh.It DEVT_FD 729*45ad9557SWarner Losh.El 730*45ad9557SWarner LoshEach type may have its own associated (struct type_devdesc), 731*45ad9557SWarner Loshwhich has the generic (struct devdesc) as its first member. 732*45ad9557SWarner Losh.It Fn dv_init 733*45ad9557SWarner LoshDriver initialization routine. 734*45ad9557SWarner LoshThis routine should probe for available units. 735*45ad9557SWarner LoshDrivers are responsible for maintaining lists of units for later enumeration. 736*45ad9557SWarner LoshNo other driver routines may be called before 737*45ad9557SWarner Losh.Fn dv_init 738*45ad9557SWarner Loshreturns. 739*45ad9557SWarner Losh.It Fn dv_open 740*45ad9557SWarner LoshThe driver open routine. 741*45ad9557SWarner Losh.It Fn dv_close 742*45ad9557SWarner LoshThe driver close routine. 743*45ad9557SWarner Losh.It Fn dv_ioctl 744*45ad9557SWarner LoshThe driver ioctl routine. 745*45ad9557SWarner Losh.It Fn dv_print 746*45ad9557SWarner LoshPrints information about the available devices. 747*45ad9557SWarner LoshInformation should be presented with 748*45ad9557SWarner Losh.Fn pager_output . 749*45ad9557SWarner Losh.It Fn dv_cleanup 750*45ad9557SWarner LoshCleans up any memory used by the device before the next stage is run. 751*45ad9557SWarner Losh.It Fn dv_fmtdev 752*45ad9557SWarner LoshConverts the specified devdesc to the canonical string representation 753*45ad9557SWarner Loshfor that device. 754*45ad9557SWarner Losh.El 7553a7d8294SWarner Losh.Sh HISTORY 7563a7d8294SWarner LoshThe 7573a7d8294SWarner Losh.Nm 7583a7d8294SWarner Loshlibrary contains contributions from many sources, including: 7593a7d8294SWarner Losh.Bl -bullet -compact 7603a7d8294SWarner Losh.It 7613a7d8294SWarner Losh.Nm libsa 7623a7d8294SWarner Loshfrom 7633a7d8294SWarner Losh.Nx 7643a7d8294SWarner Losh.It 7653a7d8294SWarner Losh.Nm libc 7663a7d8294SWarner Loshand 7673a7d8294SWarner Losh.Nm libkern 7683a7d8294SWarner Loshfrom 7693a7d8294SWarner Losh.Fx 3.0 . 7703a7d8294SWarner Losh.It 7713a7d8294SWarner Losh.Nm zalloc 7723a7d8294SWarner Loshfrom 7733a7d8294SWarner Losh.An Matthew Dillon Aq Mt dillon@backplane.com 7743a7d8294SWarner Losh.El 7753a7d8294SWarner Losh.Pp 7763a7d8294SWarner LoshThe reorganisation and port to 7773a7d8294SWarner Losh.Fx 3.0 , 7783a7d8294SWarner Loshthe environment functions and this manpage were written by 7793a7d8294SWarner Losh.An Mike Smith Aq Mt msmith@FreeBSD.org . 7803a7d8294SWarner Losh.Sh BUGS 7813a7d8294SWarner LoshThe lack of detailed memory usage data is unhelpful. 782