1863edaecSDag-Erling Smørgrav.\"- 272fba9d7SChristian Brueffer.\" 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.\" 287c2dd57bSMatthew D Fleming.Dd January 25, 2011 295f95f24bSRuslan Ermilov.Dt SBUF 9 303d45e180SRuslan Ermilov.Os 31863edaecSDag-Erling Smørgrav.Sh NAME 3281ae4b8dSRuslan Ermilov.Nm sbuf , 33863edaecSDag-Erling Smørgrav.Nm sbuf_new , 3472fba9d7SChristian Brueffer.Nm sbuf_new_auto , 357c2dd57bSMatthew D Fleming.Nm sbuf_new_for_sysctl , 36e499b74dSDag-Erling Smørgrav.Nm sbuf_clear , 37863edaecSDag-Erling Smørgrav.Nm sbuf_setpos , 38b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcat , 39b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcopyin , 40b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcpy , 41863edaecSDag-Erling Smørgrav.Nm sbuf_cat , 42b0def2b5SDag-Erling Smørgrav.Nm sbuf_copyin , 43863edaecSDag-Erling Smørgrav.Nm sbuf_cpy , 44863edaecSDag-Erling Smørgrav.Nm sbuf_printf , 45371c0c84SKelly Yancey.Nm sbuf_vprintf , 46863edaecSDag-Erling Smørgrav.Nm sbuf_putc , 474351ba27SMatthew D Fleming.Nm sbuf_set_drain , 48371c0c84SKelly Yancey.Nm sbuf_trim , 494d369413SMatthew D Fleming.Nm sbuf_error , 50863edaecSDag-Erling Smørgrav.Nm sbuf_finish , 51863edaecSDag-Erling Smørgrav.Nm sbuf_data , 52863edaecSDag-Erling Smørgrav.Nm sbuf_len , 53c5f9218bSPoul-Henning Kamp.Nm sbuf_done , 54863edaecSDag-Erling Smørgrav.Nm sbuf_delete 55863edaecSDag-Erling Smørgrav.Nd safe string formatting 56863edaecSDag-Erling Smørgrav.Sh SYNOPSIS 5732eef9aeSRuslan Ermilov.In sys/types.h 5832eef9aeSRuslan Ermilov.In sys/sbuf.h 594351ba27SMatthew D Fleming.Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ; 604351ba27SMatthew D Fleming.Pp 618a5bc771SBruce Evans.Ft struct sbuf * 62e499b74dSDag-Erling Smørgrav.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags" 6372fba9d7SChristian Brueffer.Ft struct sbuf * 6472fba9d7SChristian Brueffer.Fn sbuf_new_auto 65e499b74dSDag-Erling Smørgrav.Ft void 66e499b74dSDag-Erling Smørgrav.Fn sbuf_clear "struct sbuf *s" 67863edaecSDag-Erling Smørgrav.Ft int 68e499b74dSDag-Erling Smørgrav.Fn sbuf_setpos "struct sbuf *s" "int pos" 69863edaecSDag-Erling Smørgrav.Ft int 704aa90de3SDag-Erling Smørgrav.Fn sbuf_bcat "struct sbuf *s" "const void *buf" "size_t len" 71b0def2b5SDag-Erling Smørgrav.Ft int 72b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin "struct sbuf *s" "const void *uaddr" "size_t len" 73b0def2b5SDag-Erling Smørgrav.Ft int 744aa90de3SDag-Erling Smørgrav.Fn sbuf_bcpy "struct sbuf *s" "const void *buf" "size_t len" 75b0def2b5SDag-Erling Smørgrav.Ft int 763393f8daSKenneth D. Merry.Fn sbuf_cat "struct sbuf *s" "const char *str" 77863edaecSDag-Erling Smørgrav.Ft int 78b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin "struct sbuf *s" "const void *uaddr" "size_t len" 79b0def2b5SDag-Erling Smørgrav.Ft int 803393f8daSKenneth D. Merry.Fn sbuf_cpy "struct sbuf *s" "const char *str" 81863edaecSDag-Erling Smørgrav.Ft int 828cce2ebaSDag-Erling Smørgrav.Fn sbuf_printf "struct sbuf *s" "const char *fmt" "..." 83863edaecSDag-Erling Smørgrav.Ft int 84371c0c84SKelly Yancey.Fn sbuf_vprintf "struct sbuf *s" "const char *fmt" "va_list ap" 85371c0c84SKelly Yancey.Ft int 86863edaecSDag-Erling Smørgrav.Fn sbuf_putc "struct sbuf *s" "int c" 874351ba27SMatthew D Fleming.Ft void 884351ba27SMatthew D Fleming.Fn sbuf_set_drain "struct sbuf *s" "sbuf_drain_func *func" "void *arg" 89863edaecSDag-Erling Smørgrav.Ft int 90371c0c84SKelly Yancey.Fn sbuf_trim "struct sbuf *s" 91371c0c84SKelly Yancey.Ft int 924d369413SMatthew D Fleming.Fn sbuf_error "struct sbuf *s" 934351ba27SMatthew D Fleming.Ft int 94863edaecSDag-Erling Smørgrav.Fn sbuf_finish "struct sbuf *s" 95863edaecSDag-Erling Smørgrav.Ft char * 96863edaecSDag-Erling Smørgrav.Fn sbuf_data "struct sbuf *s" 97e499b74dSDag-Erling Smørgrav.Ft int 98863edaecSDag-Erling Smørgrav.Fn sbuf_len "struct sbuf *s" 99c5f9218bSPoul-Henning Kamp.Ft int 100c5f9218bSPoul-Henning Kamp.Fn sbuf_done "struct sbuf *s" 101863edaecSDag-Erling Smørgrav.Ft void 102863edaecSDag-Erling Smørgrav.Fn sbuf_delete "struct sbuf *s" 1037c2dd57bSMatthew D Fleming.In sys/sysctl.h 1047c2dd57bSMatthew D Fleming.Ft struct sbuf * 1057c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl "struct sbuf *s" "char *buf" "int length" "struct sysctl_req *req" 106863edaecSDag-Erling Smørgrav.Sh DESCRIPTION 107863edaecSDag-Erling SmørgravThe 10881ae4b8dSRuslan Ermilov.Nm 10967e35919SSheldon Hearnfamily of functions allows one to safely allocate, construct and 110db960cf0SMatthew D Flemingrelease bounded NUL-terminated strings in kernel space. 111863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures 112863edaecSDag-Erling Smørgravcalled 113863edaecSDag-Erling Smørgrav.Fa sbufs , 114863edaecSDag-Erling Smørgravdefined in 115fe08efe6SRuslan Ermilov.In sys/sbuf.h . 116863edaecSDag-Erling Smørgrav.Pp 117863edaecSDag-Erling SmørgravThe 118863edaecSDag-Erling Smørgrav.Fn sbuf_new 119863edaecSDag-Erling Smørgravfunction initializes the 120863edaecSDag-Erling Smørgrav.Fa sbuf 121863edaecSDag-Erling Smørgravpointed to by its first argument. 122d6479358SDag-Erling SmørgravIf that pointer is 123d6479358SDag-Erling Smørgrav.Dv NULL , 124d6479358SDag-Erling Smørgrav.Fn sbuf_new 125d6479358SDag-Erling Smørgravallocates a 126d68b700aSRuslan Ermilov.Vt struct sbuf 127d6479358SDag-Erling Smørgravusing 128d6479358SDag-Erling Smørgrav.Xr malloc 9 . 129863edaecSDag-Erling SmørgravThe 130863edaecSDag-Erling Smørgrav.Fa buf 131863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string; 132863edaecSDag-Erling Smørgravif it is 133863edaecSDag-Erling Smørgrav.Dv NULL , 134863edaecSDag-Erling Smørgrav.Fn sbuf_new 135863edaecSDag-Erling Smørgravwill allocate one using 136863edaecSDag-Erling Smørgrav.Xr malloc 9 . 137863edaecSDag-Erling SmørgravThe 138863edaecSDag-Erling Smørgrav.Fa length 139371c0c84SKelly Yanceyis the initial size of the storage buffer. 140863edaecSDag-Erling SmørgravThe fourth argument, 141863edaecSDag-Erling Smørgrav.Fa flags , 142371c0c84SKelly Yanceymay be comprised of the following flags: 1433b0536faSRuslan Ermilov.Bl -tag -width ".Dv SBUF_AUTOEXTEND" 144371c0c84SKelly Yancey.It Dv SBUF_FIXEDLEN 1453b0536faSRuslan ErmilovThe storage buffer is fixed at its initial size. 146371c0c84SKelly YanceyAttempting to extend the sbuf beyond this size results in an overflow condition. 147371c0c84SKelly Yancey.It Dv SBUF_AUTOEXTEND 148371c0c84SKelly YanceyThis indicates that the storage buffer may be extended as necessary, so long 149371c0c84SKelly Yanceyas resources allow, to hold additional data. 150371c0c84SKelly Yancey.El 151863edaecSDag-Erling Smørgrav.Pp 152863edaecSDag-Erling SmørgravNote that if 153863edaecSDag-Erling Smørgrav.Fa buf 154863edaecSDag-Erling Smørgravis not 155863edaecSDag-Erling Smørgrav.Dv NULL , 156863edaecSDag-Erling Smørgravit must point to an array of at least 157863edaecSDag-Erling Smørgrav.Fa length 158863edaecSDag-Erling Smørgravcharacters. 15991c9d24eSDag-Erling SmørgravThe result of accessing that array directly while it is in use by the 16091c9d24eSDag-Erling Smørgravsbuf is undefined. 16191c9d24eSDag-Erling Smørgrav.Pp 16291c9d24eSDag-Erling SmørgravThe 16372fba9d7SChristian Brueffer.Fn sbuf_new_auto 16472fba9d7SChristian Bruefferfunction is a shortcut for creating a completely dynamic 16572fba9d7SChristian Brueffer.Nm . 16672fba9d7SChristian BruefferIt is the equivalent of calling 16772fba9d7SChristian Brueffer.Fn sbuf_new 16872fba9d7SChristian Bruefferwith values 16972fba9d7SChristian Brueffer.Dv NULL , 17072fba9d7SChristian Brueffer.Dv NULL , 17172fba9d7SChristian Brueffer.Dv 0 , 17272fba9d7SChristian Bruefferand 17372fba9d7SChristian Brueffer.Dv SBUF_AUTOEXTEND . 17472fba9d7SChristian Brueffer.Pp 17572fba9d7SChristian BruefferThe 1767c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl 1777c2dd57bSMatthew D Flemingfunction will set up an sbuf with a drain function to use 1787c2dd57bSMatthew D Fleming.Fn SYSCTL_OUT 1797c2dd57bSMatthew D Flemingwhen the internal buffer fills. 180*00f0e671SMatthew D FlemingNote that if the various functions which append to an sbuf are used while 181*00f0e671SMatthew D Fleminga non-sleepable lock is held, the user buffer should be wired using 182*00f0e671SMatthew D Fleming.Fn sysctl_wire_old_buffer . 1837c2dd57bSMatthew D Fleming.Pp 1847c2dd57bSMatthew D FlemingThe 18591c9d24eSDag-Erling Smørgrav.Fn sbuf_delete 18691c9d24eSDag-Erling Smørgravfunction clears the 18791c9d24eSDag-Erling Smørgrav.Fa sbuf 18891c9d24eSDag-Erling Smørgravand frees any memory allocated for it. 18991c9d24eSDag-Erling SmørgravThere must be a call to 19091c9d24eSDag-Erling Smørgrav.Fn sbuf_delete 19191c9d24eSDag-Erling Smørgravfor every call to 19291c9d24eSDag-Erling Smørgrav.Fn sbuf_new . 19391c9d24eSDag-Erling SmørgravAny attempt to access the sbuf after it has been deleted will fail. 194863edaecSDag-Erling Smørgrav.Pp 195863edaecSDag-Erling SmørgravThe 196e499b74dSDag-Erling Smørgrav.Fn sbuf_clear 197e499b74dSDag-Erling Smørgravfunction invalidates the contents of the 198e499b74dSDag-Erling Smørgrav.Fa sbuf 199e499b74dSDag-Erling Smørgravand resets its position to zero. 200e499b74dSDag-Erling Smørgrav.Pp 201e499b74dSDag-Erling SmørgravThe 202863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 203863edaecSDag-Erling Smørgravfunction sets the 204863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's 205371c0c84SKelly Yanceyend position to 206863edaecSDag-Erling Smørgrav.Fa pos , 207863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the 208863edaecSDag-Erling Smørgravstorage buffer. 209371c0c84SKelly YanceyThis effectively truncates the sbuf at the new position. 210863edaecSDag-Erling Smørgrav.Pp 211863edaecSDag-Erling SmørgravThe 212b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat 213b0def2b5SDag-Erling Smørgravfunction appends the first 214b0def2b5SDag-Erling Smørgrav.Fa len 2154aa90de3SDag-Erling Smørgravbytes from the buffer 2164aa90de3SDag-Erling Smørgrav.Fa buf 217b0def2b5SDag-Erling Smørgravto the 218b0def2b5SDag-Erling Smørgrav.Fa sbuf . 219b0def2b5SDag-Erling Smørgrav.Pp 220b0def2b5SDag-Erling SmørgravThe 221b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin 222b0def2b5SDag-Erling Smørgravfunction copies 223b0def2b5SDag-Erling Smørgrav.Fa len 224b0def2b5SDag-Erling Smørgravbytes from the specified userland address into the 225b0def2b5SDag-Erling Smørgrav.Fa sbuf . 226b0def2b5SDag-Erling Smørgrav.Pp 227b0def2b5SDag-Erling SmørgravThe 228b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy 229b0def2b5SDag-Erling Smørgravfunction replaces the contents of the 230b0def2b5SDag-Erling Smørgrav.Fa sbuf 231b0def2b5SDag-Erling Smørgravwith the first 232b0def2b5SDag-Erling Smørgrav.Fa len 2334aa90de3SDag-Erling Smørgravbytes from the buffer 2344aa90de3SDag-Erling Smørgrav.Fa buf . 235b0def2b5SDag-Erling Smørgrav.Pp 236b0def2b5SDag-Erling SmørgravThe 237863edaecSDag-Erling Smørgrav.Fn sbuf_cat 238b0def2b5SDag-Erling Smørgravfunction appends the NUL-terminated string 239863edaecSDag-Erling Smørgrav.Fa str 240863edaecSDag-Erling Smørgravto the 241863edaecSDag-Erling Smørgrav.Fa sbuf 242863edaecSDag-Erling Smørgravat the current position. 243863edaecSDag-Erling Smørgrav.Pp 244863edaecSDag-Erling SmørgravThe 2454351ba27SMatthew D Fleming.Fn sbuf_set_drain 2464351ba27SMatthew D Flemingfunction sets a drain function 2474351ba27SMatthew D Fleming.Fa func 2484351ba27SMatthew D Flemingfor the 2494351ba27SMatthew D Fleming.Fa sbuf , 2504351ba27SMatthew D Flemingand records a pointer 2514351ba27SMatthew D Fleming.Fa arg 2524351ba27SMatthew D Flemingto be passed to the drain on callback. 2534351ba27SMatthew D FlemingThe drain function cannot be changed while 2544351ba27SMatthew D Fleming.Fa sbuf_len 2554351ba27SMatthew D Flemingis non-zero. 2564351ba27SMatthew D Fleming.Pp 2574351ba27SMatthew D FlemingThe registered drain function 2584351ba27SMatthew D Fleming.Vt sbuf_drain_func 2594351ba27SMatthew D Flemingwill be called with the argument 2604351ba27SMatthew D Fleming.Fa arg 2614351ba27SMatthew D Flemingprovided to 2624351ba27SMatthew D Fleming.Fn sbuf_set_drain , 2634351ba27SMatthew D Fleminga pointer 2644351ba27SMatthew D Fleming.Fa data 2654351ba27SMatthew D Flemingto a byte string that is the contents of the sbuf, and the length 2664351ba27SMatthew D Fleming.Fa len 2674351ba27SMatthew D Flemingof the data. 2684351ba27SMatthew D FlemingIf the drain function exists, it will be called when the sbuf internal 2694351ba27SMatthew D Flemingbuffer is full, or on behalf of 2704351ba27SMatthew D Fleming.Fn sbuf_finish . 2714351ba27SMatthew D FlemingThe drain function may drain some or all of the data, but must drain 2724351ba27SMatthew D Flemingat least 1 byte. 2734351ba27SMatthew D FlemingThe return value from the drain function, if positive, indicates how 2744351ba27SMatthew D Flemingmany bytes were drained. 2754351ba27SMatthew D FlemingIf negative, the return value indicates the negative error code which 2764351ba27SMatthew D Flemingwill be returned from this or a later call to 2774351ba27SMatthew D Fleming.Fn sbuf_finish . 2784351ba27SMatthew D FlemingThe returned drained length cannot be zero. 2794351ba27SMatthew D FlemingTo do unbuffered draining, initialize the sbuf with a two-byte buffer. 2804351ba27SMatthew D FlemingThe drain will be called for every byte added to the sbuf. 2814351ba27SMatthew D FlemingThe 2824351ba27SMatthew D Fleming.Fn sbuf_bcopyin , 2834351ba27SMatthew D Fleming.Fn sbuf_copyin , 2844351ba27SMatthew D Fleming.Fn sbuf_trim , 2854351ba27SMatthew D Flemingand 2864351ba27SMatthew D Fleming.Fn sbuf_data 2874351ba27SMatthew D Flemingfunctions cannot be used on an sbuf with a drain. 2884351ba27SMatthew D Fleming.Pp 2894351ba27SMatthew D FlemingThe 290b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin 291b0def2b5SDag-Erling Smørgravfunction copies a NUL-terminated string from the specified userland 292b0def2b5SDag-Erling Smørgravaddress into the 293b0def2b5SDag-Erling Smørgrav.Fa sbuf . 294b0def2b5SDag-Erling SmørgravIf the 295b0def2b5SDag-Erling Smørgrav.Fa len 296b0def2b5SDag-Erling Smørgravargument is non-zero, no more than 297b0def2b5SDag-Erling Smørgrav.Fa len 298b0def2b5SDag-Erling Smørgravcharacters (not counting the terminating NUL) are copied; otherwise 299b0def2b5SDag-Erling Smørgravthe entire string, or as much of it as can fit in the 300b0def2b5SDag-Erling Smørgrav.Fa sbuf , 301b0def2b5SDag-Erling Smørgravis copied. 302b0def2b5SDag-Erling Smørgrav.Pp 303b0def2b5SDag-Erling SmørgravThe 304863edaecSDag-Erling Smørgrav.Fn sbuf_cpy 305863edaecSDag-Erling Smørgravfunction replaces the contents of the 306863edaecSDag-Erling Smørgrav.Fa sbuf 307b0def2b5SDag-Erling Smørgravwith those of the NUL-terminated string 308863edaecSDag-Erling Smørgrav.Fa str . 309863edaecSDag-Erling SmørgravThis is equivalent to calling 310863edaecSDag-Erling Smørgrav.Fn sbuf_cat 311863edaecSDag-Erling Smørgravwith a fresh 312863edaecSDag-Erling Smørgrav.Fa sbuf 313863edaecSDag-Erling Smørgravor one which position has been reset to zero with 314e499b74dSDag-Erling Smørgrav.Fn sbuf_clear 315e499b74dSDag-Erling Smørgravor 316863edaecSDag-Erling Smørgrav.Fn sbuf_setpos . 317863edaecSDag-Erling Smørgrav.Pp 318863edaecSDag-Erling SmørgravThe 319863edaecSDag-Erling Smørgrav.Fn sbuf_printf 320863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed 321863edaecSDag-Erling Smørgravto by 322863edaecSDag-Erling Smørgrav.Fa fmt 323863edaecSDag-Erling Smørgravand appends the resulting string to the 324863edaecSDag-Erling Smørgrav.Fa sbuf 325863edaecSDag-Erling Smørgravat the current position. 326863edaecSDag-Erling Smørgrav.Pp 327863edaecSDag-Erling SmørgravThe 328371c0c84SKelly Yancey.Fn sbuf_vprintf 329371c0c84SKelly Yanceyfunction behaves the same as 330371c0c84SKelly Yancey.Fn sbuf_printf 331371c0c84SKelly Yanceyexcept that the arguments are obtained from the variable-length argument list 332371c0c84SKelly Yancey.Fa ap . 333371c0c84SKelly Yancey.Pp 334371c0c84SKelly YanceyThe 335863edaecSDag-Erling Smørgrav.Fn sbuf_putc 336863edaecSDag-Erling Smørgravfunction appends the character 337863edaecSDag-Erling Smørgrav.Fa c 338863edaecSDag-Erling Smørgravto the 339863edaecSDag-Erling Smørgrav.Fa sbuf 340863edaecSDag-Erling Smørgravat the current position. 341863edaecSDag-Erling Smørgrav.Pp 342863edaecSDag-Erling SmørgravThe 343371c0c84SKelly Yancey.Fn sbuf_trim 344371c0c84SKelly Yanceyfunction removes trailing whitespace from the 345371c0c84SKelly Yancey.Fa sbuf . 346371c0c84SKelly Yancey.Pp 347371c0c84SKelly YanceyThe 3484d369413SMatthew D Fleming.Fn sbuf_error 3494d369413SMatthew D Flemingfunction returns any error value that the 3504d369413SMatthew D Fleming.Fa sbuf 3514d369413SMatthew D Flemingmay have accumulated, either from the drain function, or ENOMEM if the 352e499b74dSDag-Erling Smørgrav.Fa sbuf 353e499b74dSDag-Erling Smørgravoverflowed. 3544d369413SMatthew D FlemingThis function is generally not needed and instead the error code from 3554d369413SMatthew D Fleming.Fn sbuf_finish 3564d369413SMatthew D Flemingis the preferred way to discover whether an sbuf had an error. 357e499b74dSDag-Erling Smørgrav.Pp 358e499b74dSDag-Erling SmørgravThe 359863edaecSDag-Erling Smørgrav.Fn sbuf_finish 3604351ba27SMatthew D Flemingfunction will call the attached drain function if one exists until all 3614351ba27SMatthew D Flemingthe data in the 362863edaecSDag-Erling Smørgrav.Fa sbuf 3634351ba27SMatthew D Flemingis flushed. 3644351ba27SMatthew D FlemingIf there is no attached drain, 3654351ba27SMatthew D Fleming.Fn sbuf_finish 3664351ba27SMatthew D FlemingNUL-terminates the 3674351ba27SMatthew D Fleming.Fa sbuf . 3684351ba27SMatthew D FlemingIn either case it marks the 3694351ba27SMatthew D Fleming.Fa sbuf 3704351ba27SMatthew D Flemingas finished, which means that it may no longer be modified using 371863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 372863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 3733393f8daSKenneth D. Merry.Fn sbuf_cpy , 374863edaecSDag-Erling Smørgrav.Fn sbuf_printf 375863edaecSDag-Erling Smørgravor 376db960cf0SMatthew D Fleming.Fn sbuf_putc , 377db960cf0SMatthew D Fleminguntil 378db960cf0SMatthew D Fleming.Fn sbuf_clear 379db960cf0SMatthew D Flemingis used to reset the sbuf. 380863edaecSDag-Erling Smørgrav.Pp 381863edaecSDag-Erling SmørgravThe 382863edaecSDag-Erling Smørgrav.Fn sbuf_data 3834351ba27SMatthew D Flemingfunction returns the actual string; 384e499b74dSDag-Erling Smørgrav.Fn sbuf_data 385e499b74dSDag-Erling Smørgravonly works on a finished 386863edaecSDag-Erling Smørgrav.Fa sbuf . 3874351ba27SMatthew D FlemingThe 3884351ba27SMatthew D Fleming.Fn sbuf_len function returns the length of the string. 3894351ba27SMatthew D FlemingFor an 3904351ba27SMatthew D Fleming.Fa sbuf 3914351ba27SMatthew D Flemingwith an attached drain, 3924351ba27SMatthew D Fleming.Fn sbuf_len 3934351ba27SMatthew D Flemingreturns the length of the un-drained data. 3944351ba27SMatthew D Fleming.Fn sbuf_done 3954351ba27SMatthew D Flemingreturns non-zero if the 3964351ba27SMatthew D Fleming.Fa sbuf 3974351ba27SMatthew D Flemingis finished. 398c5f9218bSPoul-Henning Kamp.Fn sbuf_done 399db960cf0SMatthew D Flemingreturns non-zero if the 400db960cf0SMatthew D Fleming.Fa sbuf 401db960cf0SMatthew D Flemingis finished. 402863edaecSDag-Erling Smørgrav.Sh NOTES 403863edaecSDag-Erling SmørgravIf an operation caused an 404863edaecSDag-Erling Smørgrav.Fa sbuf 405e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the 406e499b74dSDag-Erling Smørgrav.Fa sbuf 407e499b74dSDag-Erling Smørgravis finished using 408e499b74dSDag-Erling Smørgrav.Fn sbuf_finish 409e499b74dSDag-Erling Smørgravor reset using 410e499b74dSDag-Erling Smørgrav.Fn sbuf_clear , 411e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the 412e499b74dSDag-Erling Smørgravsize of its storage buffer using 413863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 414863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using 415863edaecSDag-Erling Smørgrav.Fn sbuf_cpy . 4164351ba27SMatthew D Fleming.Pp 4174351ba27SMatthew D FlemingDrains in user-space will not always function as indicated. 4184351ba27SMatthew D FlemingWhile the drain function will be called immediately on overflow from 4194351ba27SMatthew D Flemingthe 4204351ba27SMatthew D Fleming.Fa sbuf_putc , 4214351ba27SMatthew D Fleming.Fa sbuf_bcat , 4224351ba27SMatthew D Fleming.Fa sbuf_cat 4234351ba27SMatthew D Flemingfunctions, 4244351ba27SMatthew D Fleming.Fa sbuf_printf 4254351ba27SMatthew D Flemingand 4264351ba27SMatthew D Fleming.Fa sbuf_vprintf 4274351ba27SMatthew D Flemingcurrently have no way to determine whether there will be an overflow 4284351ba27SMatthew D Fleminguntil after it occurs, and cannot do a partial expansion of the format 4294351ba27SMatthew D Flemingstring. 4304351ba27SMatthew D FlemingThus when using libsbuf the buffer may be extended to allow completion 4314351ba27SMatthew D Flemingof a single printf call, even though a drain is attached. 432863edaecSDag-Erling Smørgrav.Sh RETURN VALUES 43381ae4b8dSRuslan ErmilovThe 434863edaecSDag-Erling Smørgrav.Fn sbuf_new 43581ae4b8dSRuslan Ermilovfunction returns 436d6479358SDag-Erling Smørgrav.Dv NULL 437d6479358SDag-Erling Smørgravif it failed to allocate a storage buffer, and a pointer to the new 438d6479358SDag-Erling Smørgrav.Fa sbuf 439863edaecSDag-Erling Smørgravotherwise. 440863edaecSDag-Erling Smørgrav.Pp 44181ae4b8dSRuslan ErmilovThe 442863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 44381ae4b8dSRuslan Ermilovfunction returns \-1 if 444863edaecSDag-Erling Smørgrav.Fa pos 445863edaecSDag-Erling Smørgravwas invalid, and zero otherwise. 446863edaecSDag-Erling Smørgrav.Pp 44781ae4b8dSRuslan ErmilovThe 448863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 449863edaecSDag-Erling Smørgrav.Fn sbuf_cpy , 450863edaecSDag-Erling Smørgrav.Fn sbuf_printf , 451371c0c84SKelly Yancey.Fn sbuf_putc , 452863edaecSDag-Erling Smørgravand 453371c0c84SKelly Yancey.Fn sbuf_trim 45481ae4b8dSRuslan Ermilovfunctions 455863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise. 456863edaecSDag-Erling Smørgrav.Pp 45781ae4b8dSRuslan ErmilovThe 4584d369413SMatthew D Fleming.Fn sbuf_error 4594d369413SMatthew D Flemingfunction returns a non-zero value if the buffer has an overflow or 4604d369413SMatthew D Flemingdrain error, and zero otherwise. 461e499b74dSDag-Erling Smørgrav.Pp 46281ae4b8dSRuslan ErmilovThe 463863edaecSDag-Erling Smørgrav.Fn sbuf_data 464863edaecSDag-Erling Smørgravand 465863edaecSDag-Erling Smørgrav.Fn sbuf_len 46681ae4b8dSRuslan Ermilovfunctions return 467863edaecSDag-Erling Smørgrav.Dv NULL 468e499b74dSDag-Erling Smørgravand \-1, respectively, if the buffer overflowed. 46949091c48SPoul-Henning Kamp.Pp 47081ae4b8dSRuslan ErmilovThe 47149091c48SPoul-Henning Kamp.Fn sbuf_copyin 47281ae4b8dSRuslan Ermilovfunction 47349091c48SPoul-Henning Kampreturns \-1 if copying string from userland failed, and number of bytes 47449091c48SPoul-Henning Kampcopied otherwise. 4754351ba27SMatthew D FlemingThe 4764351ba27SMatthew D Fleming.Fn sbuf_finish 4774351ba27SMatthew D Flemingfunction returns ENOMEM if the sbuf overflowed before being finished, 4784351ba27SMatthew D Flemingor returns the error code from the drain if one is attached. 4794351ba27SMatthew D FlemingWhen used as 4804351ba27SMatthew D Fleming.Xr sbuf_finish 3 , 4814351ba27SMatthew D Fleming.Fn sbuf_finish 4824351ba27SMatthew D Flemingwill return \-1 and set errno on error instead. 483863edaecSDag-Erling Smørgrav.Sh SEE ALSO 484863edaecSDag-Erling Smørgrav.Xr printf 3 , 485863edaecSDag-Erling Smørgrav.Xr strcat 3 , 486d68b700aSRuslan Ermilov.Xr strcpy 3 , 487d68b700aSRuslan Ermilov.Xr copyin 9 , 488e05f3134SAndrew R. Reiter.Xr copyinstr 9 , 489e05f3134SAndrew R. Reiter.Xr printf 9 490863edaecSDag-Erling Smørgrav.Sh HISTORY 491863edaecSDag-Erling SmørgravThe 49281ae4b8dSRuslan Ermilov.Nm 493863edaecSDag-Erling Smørgravfamily of functions first appeared in 49422cbe5e3SDag-Erling Smørgrav.Fx 4.4 . 495863edaecSDag-Erling Smørgrav.Sh AUTHORS 496863edaecSDag-Erling Smørgrav.An -nosplit 497863edaecSDag-Erling SmørgravThe 49881ae4b8dSRuslan Ermilov.Nm 499863edaecSDag-Erling Smørgravfamily of functions was designed by 500863edaecSDag-Erling Smørgrav.An Poul-Henning Kamp Aq phk@FreeBSD.org 501863edaecSDag-Erling Smørgravand implemented by 50209e06539SDag-Erling Smørgrav.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org . 503e499b74dSDag-Erling SmørgravAdditional improvements were suggested by 504e499b74dSDag-Erling Smørgrav.An Justin T. Gibbs Aq gibbs@FreeBSD.org . 505371c0c84SKelly YanceyAuto-extend support added by 506371c0c84SKelly Yancey.An Kelly Yancey Aq kbyanc@FreeBSD.org . 5074351ba27SMatthew D FlemingDrain functionality added by 5084351ba27SMatthew D Fleming.An Matthew Fleming Aq mdf@FreeBSD.org . 509863edaecSDag-Erling Smørgrav.Pp 510863edaecSDag-Erling SmørgravThis manual page was written by 51109e06539SDag-Erling Smørgrav.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org . 512