1863edaecSDag-Erling Smørgrav.\"- 2863edaecSDag-Erling Smørgrav.\" Copyright (c) 2000 Poul Henning Kamp and Dag-Erling Co�dan Sm�rgrav 3863edaecSDag-Erling Smørgrav.\" All rights reserved. 4863edaecSDag-Erling Smørgrav.\" 5863edaecSDag-Erling Smørgrav.\" Redistribution and use in source and binary forms, with or without 6863edaecSDag-Erling Smørgrav.\" modification, are permitted provided that the following conditions 7863edaecSDag-Erling Smørgrav.\" are met: 8863edaecSDag-Erling Smørgrav.\" 1. Redistributions of source code must retain the above copyright 9863edaecSDag-Erling Smørgrav.\" notice, this list of conditions and the following disclaimer. 10863edaecSDag-Erling Smørgrav.\" 2. Redistributions in binary form must reproduce the above copyright 11863edaecSDag-Erling Smørgrav.\" notice, this list of conditions and the following disclaimer in the 12863edaecSDag-Erling Smørgrav.\" documentation and/or other materials provided with the distribution. 13863edaecSDag-Erling Smørgrav.\" 14863edaecSDag-Erling Smørgrav.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15863edaecSDag-Erling Smørgrav.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16863edaecSDag-Erling Smørgrav.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17863edaecSDag-Erling Smørgrav.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18863edaecSDag-Erling Smørgrav.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19863edaecSDag-Erling Smørgrav.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20863edaecSDag-Erling Smørgrav.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21863edaecSDag-Erling Smørgrav.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22863edaecSDag-Erling Smørgrav.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23863edaecSDag-Erling Smørgrav.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24863edaecSDag-Erling Smørgrav.\" SUCH DAMAGE. 25863edaecSDag-Erling Smørgrav.\" 26863edaecSDag-Erling Smørgrav.\" $FreeBSD$ 27863edaecSDag-Erling Smørgrav.\" 28863edaecSDag-Erling Smørgrav.Dd December 10, 2000 29863edaecSDag-Erling Smørgrav.Dt sbuf 9 30863edaecSDag-Erling Smørgrav.Os FreeBSD 31863edaecSDag-Erling Smørgrav.Sh NAME 32863edaecSDag-Erling Smørgrav.Nm sbuf_new , 33863edaecSDag-Erling Smørgrav.Nm sbuf_setpos , 34863edaecSDag-Erling Smørgrav.Nm sbuf_cat , 35863edaecSDag-Erling Smørgrav.Nm sbuf_cpy , 36863edaecSDag-Erling Smørgrav.Nm sbuf_printf , 37863edaecSDag-Erling Smørgrav.Nm sbuf_putc , 38863edaecSDag-Erling Smørgrav.Nm sbuf_finish , 39863edaecSDag-Erling Smørgrav.Nm sbuf_data , 40863edaecSDag-Erling Smørgrav.Nm sbuf_len , 41863edaecSDag-Erling Smørgrav.Nm sbuf_delete 42863edaecSDag-Erling Smørgrav.Nd safe string formatting 43863edaecSDag-Erling Smørgrav.Sh SYNOPSIS 44863edaecSDag-Erling Smørgrav.Fd #include <sys/sbuf.h> 45863edaecSDag-Erling Smørgrav.Ft int 46863edaecSDag-Erling Smørgrav.Fn sbuf_new "struct sbuf *s" "char *buf" "size_t length" "int flags" 47863edaecSDag-Erling Smørgrav.Ft int 48863edaecSDag-Erling Smørgrav.Fn sbuf_setpos "struct sbuf *s" "size_t pos" 49863edaecSDag-Erling Smørgrav.Ft int 50863edaecSDag-Erling Smørgrav.Fn sbuf_cat "struct sbuf *s" "char *str" 51863edaecSDag-Erling Smørgrav.Ft int 52863edaecSDag-Erling Smørgrav.Fn sbuf_cpy "struct sbuf *s" "char *str" 53863edaecSDag-Erling Smørgrav.Ft int 54863edaecSDag-Erling Smørgrav.Fn sbuf_printf "struct sbuf *s" "char *fmt" "..." 55863edaecSDag-Erling Smørgrav.Ft int 56863edaecSDag-Erling Smørgrav.Fn sbuf_putc "struct sbuf *s" "int c" 57863edaecSDag-Erling Smørgrav.Ft int 58863edaecSDag-Erling Smørgrav.Fn sbuf_finish "struct sbuf *s" 59863edaecSDag-Erling Smørgrav.Ft char * 60863edaecSDag-Erling Smørgrav.Fn sbuf_data "struct sbuf *s" 61863edaecSDag-Erling Smørgrav.Ft size_t 62863edaecSDag-Erling Smørgrav.Fn sbuf_len "struct sbuf *s" 63863edaecSDag-Erling Smørgrav.Ft void 64863edaecSDag-Erling Smørgrav.Fn sbuf_delete "struct sbuf *s" 65863edaecSDag-Erling Smørgrav.Sh DESCRIPTION 66863edaecSDag-Erling SmørgravThe 67863edaecSDag-Erling Smørgrav.Nm sbuf 68863edaecSDag-Erling Smørgravfamily of functions allow one to safely allocate, construct and 69863edaecSDag-Erling Smørgravrelease bounded null-terminated strings in kernel space. 70863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures 71863edaecSDag-Erling Smørgravcalled 72863edaecSDag-Erling Smørgrav.Fa sbufs , 73863edaecSDag-Erling Smørgravdefined in 74863edaecSDag-Erling Smørgrav.Aq Pa sys/sbuf.h . 75863edaecSDag-Erling Smørgrav.Pp 76863edaecSDag-Erling SmørgravThe 77863edaecSDag-Erling Smørgrav.Fn sbuf_new 78863edaecSDag-Erling Smørgravfunction initializes the 79863edaecSDag-Erling Smørgrav.Fa sbuf 80863edaecSDag-Erling Smørgravpointed to by its first argument. 81863edaecSDag-Erling SmørgravThe 82863edaecSDag-Erling Smørgrav.Fa buf 83863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string; 84863edaecSDag-Erling Smørgravif it is 85863edaecSDag-Erling Smørgrav.Dv NULL , 86863edaecSDag-Erling Smørgrav.Fn sbuf_new 87863edaecSDag-Erling Smørgravwill allocate one using 88863edaecSDag-Erling Smørgrav.Xr malloc 9 . 89863edaecSDag-Erling SmørgravThe 90863edaecSDag-Erling Smørgrav.Fa length 91863edaecSDag-Erling Smørgravis the intended size of the storage buffer. 92863edaecSDag-Erling SmørgravThe fourth argument, 93863edaecSDag-Erling Smørgrav.Fa flags , 94863edaecSDag-Erling Smørgravis currently unused and should always be set to zero. 95863edaecSDag-Erling Smørgrav.Pp 96863edaecSDag-Erling SmørgravNote that if 97863edaecSDag-Erling Smørgrav.Fa buf 98863edaecSDag-Erling Smørgravis not 99863edaecSDag-Erling Smørgrav.Dv NULL , 100863edaecSDag-Erling Smørgravit must point to an array of at least 101863edaecSDag-Erling Smørgrav.Fa length 102863edaecSDag-Erling Smørgravcharacters. 103863edaecSDag-Erling Smørgrav.Pp 104863edaecSDag-Erling SmørgravThe 105863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 106863edaecSDag-Erling Smørgravfunction sets the 107863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's 108863edaecSDag-Erling Smørgravcurrent position to 109863edaecSDag-Erling Smørgrav.Fa pos , 110863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the 111863edaecSDag-Erling Smørgravstorage buffer. 112863edaecSDag-Erling Smørgrav.Pp 113863edaecSDag-Erling SmørgravThe 114863edaecSDag-Erling Smørgrav.Fn sbuf_cat 115863edaecSDag-Erling Smørgravfunction appends the string 116863edaecSDag-Erling Smørgrav.Fa str 117863edaecSDag-Erling Smørgravto the 118863edaecSDag-Erling Smørgrav.Fa sbuf 119863edaecSDag-Erling Smørgravat the current position. 120863edaecSDag-Erling Smørgrav.Pp 121863edaecSDag-Erling SmørgravThe 122863edaecSDag-Erling Smørgrav.Fn sbuf_cpy 123863edaecSDag-Erling Smørgravfunction replaces the contents of the 124863edaecSDag-Erling Smørgrav.Fa sbuf 125863edaecSDag-Erling Smørgravwith those of the string 126863edaecSDag-Erling Smørgrav.Fa str . 127863edaecSDag-Erling SmørgravThis is equivalent to calling 128863edaecSDag-Erling Smørgrav.Fn sbuf_cat 129863edaecSDag-Erling Smørgravwith a fresh 130863edaecSDag-Erling Smørgrav.Fa sbuf 131863edaecSDag-Erling Smørgravor one which position has been reset to zero with 132863edaecSDag-Erling Smørgrav.Fn sbuf_setpos . 133863edaecSDag-Erling Smørgrav.Pp 134863edaecSDag-Erling SmørgravThe 135863edaecSDag-Erling Smørgrav.Fn sbuf_printf 136863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed 137863edaecSDag-Erling Smørgravto by 138863edaecSDag-Erling Smørgrav.Fa fmt 139863edaecSDag-Erling Smørgravand appends the resulting string to the 140863edaecSDag-Erling Smørgrav.Fa sbuf 141863edaecSDag-Erling Smørgravat the current position. 142863edaecSDag-Erling Smørgrav.Pp 143863edaecSDag-Erling SmørgravThe 144863edaecSDag-Erling Smørgrav.Fn sbuf_putc 145863edaecSDag-Erling Smørgravfunction appends the character 146863edaecSDag-Erling Smørgrav.Fa c 147863edaecSDag-Erling Smørgravto the 148863edaecSDag-Erling Smørgrav.Fa sbuf 149863edaecSDag-Erling Smørgravat the current position. 150863edaecSDag-Erling Smørgrav.Pp 151863edaecSDag-Erling SmørgravThe 152863edaecSDag-Erling Smørgrav.Fn sbuf_finish 153863edaecSDag-Erling Smørgravfunction null-terminates the 154863edaecSDag-Erling Smørgrav.Fa sbuf 155863edaecSDag-Erling Smørgravand marks it as finished, which means that it may no longer be 156863edaecSDag-Erling Smørgravmodified using 157863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 158863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 159863edaecSDag-Erling Smørgrav.Fn sbuf_cpu , 160863edaecSDag-Erling Smørgrav.Fn sbuf_printf 161863edaecSDag-Erling Smørgravor 162863edaecSDag-Erling Smørgrav.Fn sbuf_putc . 163863edaecSDag-Erling Smørgrav.Pp 164863edaecSDag-Erling SmørgravThe 165863edaecSDag-Erling Smørgrav.Fn sbuf_data 166863edaecSDag-Erling Smørgravand 167863edaecSDag-Erling Smørgrav.Fn sbuf_len 168863edaecSDag-Erling Smørgravfunctions return the actual string and its length, respectively, and 169863edaecSDag-Erling Smørgravonly work on a finished and non-overflowed 170863edaecSDag-Erling Smørgrav.Fa sbuf . 171863edaecSDag-Erling Smørgrav.Pp 172863edaecSDag-Erling SmørgravFinally, the 173863edaecSDag-Erling Smørgrav.Fn sbuf_delete 174863edaecSDag-Erling Smørgravfunction clears the 175863edaecSDag-Erling Smørgrav.Fa sbuf 176863edaecSDag-Erling Smørgravand frees its storage buffer if it was allocated by 177863edaecSDag-Erling Smørgrav.Fn sbuf_new . 178863edaecSDag-Erling Smørgrav.Sh NOTES 179863edaecSDag-Erling SmørgravIf an operation caused an 180863edaecSDag-Erling Smørgrav.Fa sbuf 181863edaecSDag-Erling Smørgravto overflow, most subsequent operations (including 182863edaecSDag-Erling Smørgrav.Fn sbuf_finish ) 183863edaecSDag-Erling Smørgravon it will fail until the 184863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's 185863edaecSDag-Erling Smørgravposition is reset to a value between 0 and one less than the size of 186863edaecSDag-Erling Smørgravits storage buffer using 187863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 188863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using 189863edaecSDag-Erling Smørgrav.Fn sbuf_cpy . 190863edaecSDag-Erling Smørgrav.Sh RETURN VALUES 191863edaecSDag-Erling Smørgrav.Fn sbuf_new 192863edaecSDag-Erling Smørgravreturns \-1 if it failed to allocate a storage buffer, and zero 193863edaecSDag-Erling Smørgravotherwise. 194863edaecSDag-Erling Smørgrav.Pp 195863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 196863edaecSDag-Erling Smørgravreturns \-1 if 197863edaecSDag-Erling Smørgrav.Fa pos 198863edaecSDag-Erling Smørgravwas invalid, and zero otherwise. 199863edaecSDag-Erling Smørgrav.Pp 200863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 201863edaecSDag-Erling Smørgrav.Fn sbuf_cpy , 202863edaecSDag-Erling Smørgrav.Fn sbuf_printf , 203863edaecSDag-Erling Smørgrav.Fn sbuf_putc , 204863edaecSDag-Erling Smørgravand 205863edaecSDag-Erling Smørgrav.Fn sbuf_finish 206863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise. 207863edaecSDag-Erling Smørgrav.Pp 208863edaecSDag-Erling Smørgrav.Fn sbuf_data 209863edaecSDag-Erling Smørgravand 210863edaecSDag-Erling Smørgrav.Fn sbuf_len 211863edaecSDag-Erling Smørgravreturn 212863edaecSDag-Erling Smørgrav.Dv NULL 213863edaecSDag-Erling Smørgravand 0, respectively, if the buffer overflowed. 214863edaecSDag-Erling Smørgrav.Sh SEE ALSO 215863edaecSDag-Erling Smørgrav.Xr printf 3 , 216863edaecSDag-Erling Smørgrav.Xr strcat 3 , 217863edaecSDag-Erling Smørgrav.Xr strcpy 3 218863edaecSDag-Erling Smørgrav.Sh HISTORY 219863edaecSDag-Erling SmørgravThe 220863edaecSDag-Erling Smørgrav.Nm sbuf 221863edaecSDag-Erling Smørgravfamily of functions first appeared in 222863edaecSDag-Erling Smørgrav.Fx 4.3 . 223863edaecSDag-Erling Smørgrav.Sh AUTHORS 224863edaecSDag-Erling Smørgrav.An -nosplit 225863edaecSDag-Erling SmørgravThe 226863edaecSDag-Erling Smørgrav.Nm sbuf 227863edaecSDag-Erling Smørgravfamily of functions was designed by 228863edaecSDag-Erling Smørgrav.An Poul-Henning Kamp Aq phk@FreeBSD.org 229863edaecSDag-Erling Smørgravand implemented by 230863edaecSDag-Erling Smørgrav.An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org . 231863edaecSDag-Erling Smørgrav.Pp 232863edaecSDag-Erling SmørgravThis manual page was written by 233863edaecSDag-Erling Smørgrav.An Dag-Erling Co\(:idan Sm\(/orgrav . 234