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.\" 28e499b74dSDag-Erling Smørgrav.Dd January 28, 2001 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 , 33e499b74dSDag-Erling Smørgrav.Nm sbuf_clear , 34863edaecSDag-Erling Smørgrav.Nm sbuf_setpos , 35863edaecSDag-Erling Smørgrav.Nm sbuf_cat , 36863edaecSDag-Erling Smørgrav.Nm sbuf_cpy , 37863edaecSDag-Erling Smørgrav.Nm sbuf_printf , 38863edaecSDag-Erling Smørgrav.Nm sbuf_putc , 39e499b74dSDag-Erling Smørgrav.Nm sbuf_overflowed , 40863edaecSDag-Erling Smørgrav.Nm sbuf_finish , 41863edaecSDag-Erling Smørgrav.Nm sbuf_data , 42863edaecSDag-Erling Smørgrav.Nm sbuf_len , 43863edaecSDag-Erling Smørgrav.Nm sbuf_delete 44863edaecSDag-Erling Smørgrav.Nd safe string formatting 45863edaecSDag-Erling Smørgrav.Sh SYNOPSIS 46863edaecSDag-Erling Smørgrav.Fd #include <sys/sbuf.h> 47863edaecSDag-Erling Smørgrav.Ft int 48e499b74dSDag-Erling Smørgrav.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags" 49e499b74dSDag-Erling Smørgrav.Ft void 50e499b74dSDag-Erling Smørgrav.Fn sbuf_clear "struct sbuf *s" 51863edaecSDag-Erling Smørgrav.Ft int 52e499b74dSDag-Erling Smørgrav.Fn sbuf_setpos "struct sbuf *s" "int pos" 53863edaecSDag-Erling Smørgrav.Ft int 54863edaecSDag-Erling Smørgrav.Fn sbuf_cat "struct sbuf *s" "char *str" 55863edaecSDag-Erling Smørgrav.Ft int 56863edaecSDag-Erling Smørgrav.Fn sbuf_cpy "struct sbuf *s" "char *str" 57863edaecSDag-Erling Smørgrav.Ft int 58863edaecSDag-Erling Smørgrav.Fn sbuf_printf "struct sbuf *s" "char *fmt" "..." 59863edaecSDag-Erling Smørgrav.Ft int 60863edaecSDag-Erling Smørgrav.Fn sbuf_putc "struct sbuf *s" "int c" 61863edaecSDag-Erling Smørgrav.Ft int 62e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed "struct sbuf *s" 63e499b74dSDag-Erling Smørgrav.Ft void 64863edaecSDag-Erling Smørgrav.Fn sbuf_finish "struct sbuf *s" 65863edaecSDag-Erling Smørgrav.Ft char * 66863edaecSDag-Erling Smørgrav.Fn sbuf_data "struct sbuf *s" 67e499b74dSDag-Erling Smørgrav.Ft int 68863edaecSDag-Erling Smørgrav.Fn sbuf_len "struct sbuf *s" 69863edaecSDag-Erling Smørgrav.Ft void 70863edaecSDag-Erling Smørgrav.Fn sbuf_delete "struct sbuf *s" 71863edaecSDag-Erling Smørgrav.Sh DESCRIPTION 72863edaecSDag-Erling SmørgravThe 73863edaecSDag-Erling Smørgrav.Nm sbuf 7467e35919SSheldon Hearnfamily of functions allows one to safely allocate, construct and 75863edaecSDag-Erling Smørgravrelease bounded null-terminated strings in kernel space. 76863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures 77863edaecSDag-Erling Smørgravcalled 78863edaecSDag-Erling Smørgrav.Fa sbufs , 79863edaecSDag-Erling Smørgravdefined in 80863edaecSDag-Erling Smørgrav.Aq Pa sys/sbuf.h . 81863edaecSDag-Erling Smørgrav.Pp 82863edaecSDag-Erling SmørgravThe 83863edaecSDag-Erling Smørgrav.Fn sbuf_new 84863edaecSDag-Erling Smørgravfunction initializes the 85863edaecSDag-Erling Smørgrav.Fa sbuf 86863edaecSDag-Erling Smørgravpointed to by its first argument. 87863edaecSDag-Erling SmørgravThe 88863edaecSDag-Erling Smørgrav.Fa buf 89863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string; 90863edaecSDag-Erling Smørgravif it is 91863edaecSDag-Erling Smørgrav.Dv NULL , 92863edaecSDag-Erling Smørgrav.Fn sbuf_new 93863edaecSDag-Erling Smørgravwill allocate one using 94863edaecSDag-Erling Smørgrav.Xr malloc 9 . 95863edaecSDag-Erling SmørgravThe 96863edaecSDag-Erling Smørgrav.Fa length 97863edaecSDag-Erling Smørgravis the intended size of the storage buffer. 98863edaecSDag-Erling SmørgravThe fourth argument, 99863edaecSDag-Erling Smørgrav.Fa flags , 100863edaecSDag-Erling Smørgravis currently unused and should always be set to zero. 101863edaecSDag-Erling Smørgrav.Pp 102863edaecSDag-Erling SmørgravNote that if 103863edaecSDag-Erling Smørgrav.Fa buf 104863edaecSDag-Erling Smørgravis not 105863edaecSDag-Erling Smørgrav.Dv NULL , 106863edaecSDag-Erling Smørgravit must point to an array of at least 107863edaecSDag-Erling Smørgrav.Fa length 108863edaecSDag-Erling Smørgravcharacters. 109863edaecSDag-Erling Smørgrav.Pp 110863edaecSDag-Erling SmørgravThe 111e499b74dSDag-Erling Smørgrav.Fn sbuf_clear 112e499b74dSDag-Erling Smørgravfunction invalidates the contents of the 113e499b74dSDag-Erling Smørgrav.Fa sbuf 114e499b74dSDag-Erling Smørgravand resets its position to zero. 115e499b74dSDag-Erling Smørgrav.Pp 116e499b74dSDag-Erling SmørgravThe 117863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 118863edaecSDag-Erling Smørgravfunction sets the 119863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's 120863edaecSDag-Erling Smørgravcurrent position to 121863edaecSDag-Erling Smørgrav.Fa pos , 122863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the 123863edaecSDag-Erling Smørgravstorage buffer. 124863edaecSDag-Erling Smørgrav.Pp 125863edaecSDag-Erling SmørgravThe 126863edaecSDag-Erling Smørgrav.Fn sbuf_cat 127863edaecSDag-Erling Smørgravfunction appends the string 128863edaecSDag-Erling Smørgrav.Fa str 129863edaecSDag-Erling Smørgravto the 130863edaecSDag-Erling Smørgrav.Fa sbuf 131863edaecSDag-Erling Smørgravat the current position. 132863edaecSDag-Erling Smørgrav.Pp 133863edaecSDag-Erling SmørgravThe 134863edaecSDag-Erling Smørgrav.Fn sbuf_cpy 135863edaecSDag-Erling Smørgravfunction replaces the contents of the 136863edaecSDag-Erling Smørgrav.Fa sbuf 137863edaecSDag-Erling Smørgravwith those of the string 138863edaecSDag-Erling Smørgrav.Fa str . 139863edaecSDag-Erling SmørgravThis is equivalent to calling 140863edaecSDag-Erling Smørgrav.Fn sbuf_cat 141863edaecSDag-Erling Smørgravwith a fresh 142863edaecSDag-Erling Smørgrav.Fa sbuf 143863edaecSDag-Erling Smørgravor one which position has been reset to zero with 144e499b74dSDag-Erling Smørgrav.Fn sbuf_clear 145e499b74dSDag-Erling Smørgravor 146863edaecSDag-Erling Smørgrav.Fn sbuf_setpos . 147863edaecSDag-Erling Smørgrav.Pp 148863edaecSDag-Erling SmørgravThe 149863edaecSDag-Erling Smørgrav.Fn sbuf_printf 150863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed 151863edaecSDag-Erling Smørgravto by 152863edaecSDag-Erling Smørgrav.Fa fmt 153863edaecSDag-Erling Smørgravand appends the resulting string to the 154863edaecSDag-Erling Smørgrav.Fa sbuf 155863edaecSDag-Erling Smørgravat the current position. 156863edaecSDag-Erling Smørgrav.Pp 157863edaecSDag-Erling SmørgravThe 158863edaecSDag-Erling Smørgrav.Fn sbuf_putc 159863edaecSDag-Erling Smørgravfunction appends the character 160863edaecSDag-Erling Smørgrav.Fa c 161863edaecSDag-Erling Smørgravto the 162863edaecSDag-Erling Smørgrav.Fa sbuf 163863edaecSDag-Erling Smørgravat the current position. 164863edaecSDag-Erling Smørgrav.Pp 165863edaecSDag-Erling SmørgravThe 166e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed 167e499b74dSDag-Erling Smørgravfunction returns a non-zero value if the 168e499b74dSDag-Erling Smørgrav.Fa sbuf 169e499b74dSDag-Erling Smørgravoverflowed. 170e499b74dSDag-Erling Smørgrav.Pp 171e499b74dSDag-Erling SmørgravThe 172863edaecSDag-Erling Smørgrav.Fn sbuf_finish 173863edaecSDag-Erling Smørgravfunction null-terminates the 174863edaecSDag-Erling Smørgrav.Fa sbuf 175863edaecSDag-Erling Smørgravand marks it as finished, which means that it may no longer be 176863edaecSDag-Erling Smørgravmodified using 177863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 178863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 179863edaecSDag-Erling Smørgrav.Fn sbuf_cpu , 180863edaecSDag-Erling Smørgrav.Fn sbuf_printf 181863edaecSDag-Erling Smørgravor 182863edaecSDag-Erling Smørgrav.Fn sbuf_putc . 183863edaecSDag-Erling Smørgrav.Pp 184863edaecSDag-Erling SmørgravThe 185863edaecSDag-Erling Smørgrav.Fn sbuf_data 186863edaecSDag-Erling Smørgravand 187863edaecSDag-Erling Smørgrav.Fn sbuf_len 188e499b74dSDag-Erling Smørgravfunctions return the actual string and its length, respectively; 189e499b74dSDag-Erling Smørgrav.Fn sbuf_data 190e499b74dSDag-Erling Smørgravonly works on a finished 191863edaecSDag-Erling Smørgrav.Fa sbuf . 192863edaecSDag-Erling Smørgrav.Pp 193863edaecSDag-Erling SmørgravFinally, the 194863edaecSDag-Erling Smørgrav.Fn sbuf_delete 195863edaecSDag-Erling Smørgravfunction clears the 196863edaecSDag-Erling Smørgrav.Fa sbuf 197863edaecSDag-Erling Smørgravand frees its storage buffer if it was allocated by 198863edaecSDag-Erling Smørgrav.Fn sbuf_new . 199863edaecSDag-Erling Smørgrav.Sh NOTES 200863edaecSDag-Erling SmørgravIf an operation caused an 201863edaecSDag-Erling Smørgrav.Fa sbuf 202e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the 203e499b74dSDag-Erling Smørgrav.Fa sbuf 204e499b74dSDag-Erling Smørgravis finished using 205e499b74dSDag-Erling Smørgrav.Fn sbuf_finish 206e499b74dSDag-Erling Smørgravor reset using 207e499b74dSDag-Erling Smørgrav.Fn sbuf_clear , 208e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the 209e499b74dSDag-Erling Smørgravsize of its storage buffer using 210863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 211863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using 212863edaecSDag-Erling Smørgrav.Fn sbuf_cpy . 213863edaecSDag-Erling Smørgrav.Sh RETURN VALUES 214863edaecSDag-Erling Smørgrav.Fn sbuf_new 215863edaecSDag-Erling Smørgravreturns \-1 if it failed to allocate a storage buffer, and zero 216863edaecSDag-Erling Smørgravotherwise. 217863edaecSDag-Erling Smørgrav.Pp 218863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 219863edaecSDag-Erling Smørgravreturns \-1 if 220863edaecSDag-Erling Smørgrav.Fa pos 221863edaecSDag-Erling Smørgravwas invalid, and zero otherwise. 222863edaecSDag-Erling Smørgrav.Pp 223863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 224863edaecSDag-Erling Smørgrav.Fn sbuf_cpy , 225863edaecSDag-Erling Smørgrav.Fn sbuf_printf , 226863edaecSDag-Erling Smørgravand 227e499b74dSDag-Erling Smørgrav.Fn sbuf_putc 228863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise. 229863edaecSDag-Erling Smørgrav.Pp 230e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed 231e499b74dSDag-Erling Smørgravreturns a non-zero value if the buffer overflowed, and zero otherwise. 232e499b74dSDag-Erling Smørgrav.Pp 233863edaecSDag-Erling Smørgrav.Fn sbuf_data 234863edaecSDag-Erling Smørgravand 235863edaecSDag-Erling Smørgrav.Fn sbuf_len 236863edaecSDag-Erling Smørgravreturn 237863edaecSDag-Erling Smørgrav.Dv NULL 238e499b74dSDag-Erling Smørgravand \-1, respectively, if the buffer overflowed. 239863edaecSDag-Erling Smørgrav.Sh SEE ALSO 240863edaecSDag-Erling Smørgrav.Xr printf 3 , 241863edaecSDag-Erling Smørgrav.Xr strcat 3 , 242863edaecSDag-Erling Smørgrav.Xr strcpy 3 243863edaecSDag-Erling Smørgrav.Sh HISTORY 244863edaecSDag-Erling SmørgravThe 245863edaecSDag-Erling Smørgrav.Nm sbuf 246863edaecSDag-Erling Smørgravfamily of functions first appeared in 247863edaecSDag-Erling Smørgrav.Fx 4.3 . 248863edaecSDag-Erling Smørgrav.Sh AUTHORS 249863edaecSDag-Erling Smørgrav.An -nosplit 250863edaecSDag-Erling SmørgravThe 251863edaecSDag-Erling Smørgrav.Nm sbuf 252863edaecSDag-Erling Smørgravfamily of functions was designed by 253863edaecSDag-Erling Smørgrav.An Poul-Henning Kamp Aq phk@FreeBSD.org 254863edaecSDag-Erling Smørgravand implemented by 255863edaecSDag-Erling Smørgrav.An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org . 256e499b74dSDag-Erling SmørgravAdditional improvements were suggested by 257e499b74dSDag-Erling Smørgrav.An Justin T. Gibbs Aq gibbs@FreeBSD.org . 258863edaecSDag-Erling Smørgrav.Pp 259863edaecSDag-Erling SmørgravThis manual page was written by 260863edaecSDag-Erling Smørgrav.An Dag-Erling Co\(:idan Sm\(/orgrav . 261