1863edaecSDag-Erling Smørgrav.\"- 26e18fca1SUlrich Spörlein.\" 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.\" 28*f4d28142SIan Lepore.Dd March 14, 2015 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 , 37*f4d28142SIan Lepore.Nm sbuf_get_flags , 38*f4d28142SIan Lepore.Nm sbuf_set_flags , 39*f4d28142SIan Lepore.Nm sbuf_clear_flags , 40863edaecSDag-Erling Smørgrav.Nm sbuf_setpos , 41b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcat , 42b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcopyin , 43b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcpy , 44863edaecSDag-Erling Smørgrav.Nm sbuf_cat , 45b0def2b5SDag-Erling Smørgrav.Nm sbuf_copyin , 46863edaecSDag-Erling Smørgrav.Nm sbuf_cpy , 47863edaecSDag-Erling Smørgrav.Nm sbuf_printf , 48371c0c84SKelly Yancey.Nm sbuf_vprintf , 49863edaecSDag-Erling Smørgrav.Nm sbuf_putc , 504351ba27SMatthew D Fleming.Nm sbuf_set_drain , 51371c0c84SKelly Yancey.Nm sbuf_trim , 524d369413SMatthew D Fleming.Nm sbuf_error , 53863edaecSDag-Erling Smørgrav.Nm sbuf_finish , 54863edaecSDag-Erling Smørgrav.Nm sbuf_data , 55863edaecSDag-Erling Smørgrav.Nm sbuf_len , 56c5f9218bSPoul-Henning Kamp.Nm sbuf_done , 57f02db7b4SMikolaj Golub.Nm sbuf_delete , 58f02db7b4SMikolaj Golub.Nm sbuf_start_section , 59f02db7b4SMikolaj Golub.Nm sbuf_end_section 606ab7244aSPoul-Henning Kamp.Nd safe string composition 61863edaecSDag-Erling Smørgrav.Sh SYNOPSIS 6232eef9aeSRuslan Ermilov.In sys/types.h 6332eef9aeSRuslan Ermilov.In sys/sbuf.h 644351ba27SMatthew D Fleming.Ft typedef\ int ( sbuf_drain_func ) ( void\ *arg, const\ char\ *data, int\ len ) ; 654351ba27SMatthew D Fleming.Pp 668a5bc771SBruce Evans.Ft struct sbuf * 67e499b74dSDag-Erling Smørgrav.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags" 6872fba9d7SChristian Brueffer.Ft struct sbuf * 6972fba9d7SChristian Brueffer.Fn sbuf_new_auto 70e499b74dSDag-Erling Smørgrav.Ft void 71e499b74dSDag-Erling Smørgrav.Fn sbuf_clear "struct sbuf *s" 72863edaecSDag-Erling Smørgrav.Ft int 73*f4d28142SIan Lepore.Fn sbuf_get_flags "struct sbuf *s" 74*f4d28142SIan Lepore.Ft void 75*f4d28142SIan Lepore.Fn sbuf_set_flags "struct sbuf *s" "int flags" 76*f4d28142SIan Lepore.Ft void 77*f4d28142SIan Lepore.Fn sbuf_clear_flags "struct sbuf *s" "int flags" 78*f4d28142SIan Lepore.Ft int 79e499b74dSDag-Erling Smørgrav.Fn sbuf_setpos "struct sbuf *s" "int pos" 80863edaecSDag-Erling Smørgrav.Ft int 814aa90de3SDag-Erling Smørgrav.Fn sbuf_bcat "struct sbuf *s" "const void *buf" "size_t len" 82b0def2b5SDag-Erling Smørgrav.Ft int 83b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin "struct sbuf *s" "const void *uaddr" "size_t len" 84b0def2b5SDag-Erling Smørgrav.Ft int 854aa90de3SDag-Erling Smørgrav.Fn sbuf_bcpy "struct sbuf *s" "const void *buf" "size_t len" 86b0def2b5SDag-Erling Smørgrav.Ft int 873393f8daSKenneth D. Merry.Fn sbuf_cat "struct sbuf *s" "const char *str" 88863edaecSDag-Erling Smørgrav.Ft int 89b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin "struct sbuf *s" "const void *uaddr" "size_t len" 90b0def2b5SDag-Erling Smørgrav.Ft int 913393f8daSKenneth D. Merry.Fn sbuf_cpy "struct sbuf *s" "const char *str" 92863edaecSDag-Erling Smørgrav.Ft int 938cce2ebaSDag-Erling Smørgrav.Fn sbuf_printf "struct sbuf *s" "const char *fmt" "..." 94863edaecSDag-Erling Smørgrav.Ft int 95371c0c84SKelly Yancey.Fn sbuf_vprintf "struct sbuf *s" "const char *fmt" "va_list ap" 96371c0c84SKelly Yancey.Ft int 97863edaecSDag-Erling Smørgrav.Fn sbuf_putc "struct sbuf *s" "int c" 984351ba27SMatthew D Fleming.Ft void 994351ba27SMatthew D Fleming.Fn sbuf_set_drain "struct sbuf *s" "sbuf_drain_func *func" "void *arg" 100863edaecSDag-Erling Smørgrav.Ft int 101371c0c84SKelly Yancey.Fn sbuf_trim "struct sbuf *s" 102371c0c84SKelly Yancey.Ft int 1034d369413SMatthew D Fleming.Fn sbuf_error "struct sbuf *s" 1044351ba27SMatthew D Fleming.Ft int 105863edaecSDag-Erling Smørgrav.Fn sbuf_finish "struct sbuf *s" 106863edaecSDag-Erling Smørgrav.Ft char * 107863edaecSDag-Erling Smørgrav.Fn sbuf_data "struct sbuf *s" 108b397b9b7SMikolaj Golub.Ft ssize_t 109863edaecSDag-Erling Smørgrav.Fn sbuf_len "struct sbuf *s" 110c5f9218bSPoul-Henning Kamp.Ft int 111c5f9218bSPoul-Henning Kamp.Fn sbuf_done "struct sbuf *s" 112863edaecSDag-Erling Smørgrav.Ft void 113863edaecSDag-Erling Smørgrav.Fn sbuf_delete "struct sbuf *s" 114f02db7b4SMikolaj Golub.Ft void 115f02db7b4SMikolaj Golub.Fn sbuf_start_section "struct sbuf *s" "ssize_t *old_lenp" 116f02db7b4SMikolaj Golub.Ft ssize_t 117f02db7b4SMikolaj Golub.Fn sbuf_end_section "struct sbuf *s" "ssize_t old_len" "size_t pad" "int c" 1187c2dd57bSMatthew D Fleming.In sys/sysctl.h 1197c2dd57bSMatthew D Fleming.Ft struct sbuf * 1207c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl "struct sbuf *s" "char *buf" "int length" "struct sysctl_req *req" 121863edaecSDag-Erling Smørgrav.Sh DESCRIPTION 122863edaecSDag-Erling SmørgravThe 12381ae4b8dSRuslan Ermilov.Nm 1246ab7244aSPoul-Henning Kampfamily of functions allows one to safely allocate, compose and 1256ab7244aSPoul-Henning Kamprelease strings in kernel or user space. 1266ab7244aSPoul-Henning Kamp.Pp 127863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures 128863edaecSDag-Erling Smørgravcalled 129863edaecSDag-Erling Smørgrav.Fa sbufs , 130863edaecSDag-Erling Smørgravdefined in 131fe08efe6SRuslan Ermilov.In sys/sbuf.h . 132863edaecSDag-Erling Smørgrav.Pp 1336ab7244aSPoul-Henning KampAny errors encountered during the allocation or composition of the 1346ab7244aSPoul-Henning Kampstring will be latched in the data structure, 1356ab7244aSPoul-Henning Kampmaking a single error test at the end of the composition 1366ab7244aSPoul-Henning Kampsufficient to determine success or failure of the entire process. 1376ab7244aSPoul-Henning Kamp.Pp 138863edaecSDag-Erling SmørgravThe 139863edaecSDag-Erling Smørgrav.Fn sbuf_new 140863edaecSDag-Erling Smørgravfunction initializes the 141863edaecSDag-Erling Smørgrav.Fa sbuf 142863edaecSDag-Erling Smørgravpointed to by its first argument. 143d6479358SDag-Erling SmørgravIf that pointer is 144d6479358SDag-Erling Smørgrav.Dv NULL , 145d6479358SDag-Erling Smørgrav.Fn sbuf_new 146d6479358SDag-Erling Smørgravallocates a 147d68b700aSRuslan Ermilov.Vt struct sbuf 148d6479358SDag-Erling Smørgravusing 149d6479358SDag-Erling Smørgrav.Xr malloc 9 . 150863edaecSDag-Erling SmørgravThe 151863edaecSDag-Erling Smørgrav.Fa buf 152863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string; 153863edaecSDag-Erling Smørgravif it is 154863edaecSDag-Erling Smørgrav.Dv NULL , 155863edaecSDag-Erling Smørgrav.Fn sbuf_new 156863edaecSDag-Erling Smørgravwill allocate one using 157863edaecSDag-Erling Smørgrav.Xr malloc 9 . 158863edaecSDag-Erling SmørgravThe 159863edaecSDag-Erling Smørgrav.Fa length 160371c0c84SKelly Yanceyis the initial size of the storage buffer. 161863edaecSDag-Erling SmørgravThe fourth argument, 162863edaecSDag-Erling Smørgrav.Fa flags , 163371c0c84SKelly Yanceymay be comprised of the following flags: 1643b0536faSRuslan Ermilov.Bl -tag -width ".Dv SBUF_AUTOEXTEND" 165371c0c84SKelly Yancey.It Dv SBUF_FIXEDLEN 1663b0536faSRuslan ErmilovThe storage buffer is fixed at its initial size. 167371c0c84SKelly YanceyAttempting to extend the sbuf beyond this size results in an overflow condition. 168371c0c84SKelly Yancey.It Dv SBUF_AUTOEXTEND 169371c0c84SKelly YanceyThis indicates that the storage buffer may be extended as necessary, so long 170371c0c84SKelly Yanceyas resources allow, to hold additional data. 171*f4d28142SIan Lepore.It Dv SBUF_INCLUDENUL 172*f4d28142SIan LeporeThis causes the final nulterm byte to be counted in the length of the data. 173371c0c84SKelly Yancey.El 174863edaecSDag-Erling Smørgrav.Pp 175863edaecSDag-Erling SmørgravNote that if 176863edaecSDag-Erling Smørgrav.Fa buf 177863edaecSDag-Erling Smørgravis not 178863edaecSDag-Erling Smørgrav.Dv NULL , 179863edaecSDag-Erling Smørgravit must point to an array of at least 180863edaecSDag-Erling Smørgrav.Fa length 181863edaecSDag-Erling Smørgravcharacters. 18291c9d24eSDag-Erling SmørgravThe result of accessing that array directly while it is in use by the 18391c9d24eSDag-Erling Smørgravsbuf is undefined. 18491c9d24eSDag-Erling Smørgrav.Pp 18591c9d24eSDag-Erling SmørgravThe 18672fba9d7SChristian Brueffer.Fn sbuf_new_auto 18772fba9d7SChristian Bruefferfunction is a shortcut for creating a completely dynamic 18872fba9d7SChristian Brueffer.Nm . 18972fba9d7SChristian BruefferIt is the equivalent of calling 19072fba9d7SChristian Brueffer.Fn sbuf_new 19172fba9d7SChristian Bruefferwith values 19272fba9d7SChristian Brueffer.Dv NULL , 19372fba9d7SChristian Brueffer.Dv NULL , 19472fba9d7SChristian Brueffer.Dv 0 , 19572fba9d7SChristian Bruefferand 19672fba9d7SChristian Brueffer.Dv SBUF_AUTOEXTEND . 19772fba9d7SChristian Brueffer.Pp 19872fba9d7SChristian BruefferThe 1997c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl 2007c2dd57bSMatthew D Flemingfunction will set up an sbuf with a drain function to use 2017c2dd57bSMatthew D Fleming.Fn SYSCTL_OUT 2027c2dd57bSMatthew D Flemingwhen the internal buffer fills. 20300f0e671SMatthew D FlemingNote that if the various functions which append to an sbuf are used while 20400f0e671SMatthew D Fleminga non-sleepable lock is held, the user buffer should be wired using 20500f0e671SMatthew D Fleming.Fn sysctl_wire_old_buffer . 2067c2dd57bSMatthew D Fleming.Pp 2077c2dd57bSMatthew D FlemingThe 20891c9d24eSDag-Erling Smørgrav.Fn sbuf_delete 20991c9d24eSDag-Erling Smørgravfunction clears the 21091c9d24eSDag-Erling Smørgrav.Fa sbuf 21191c9d24eSDag-Erling Smørgravand frees any memory allocated for it. 21291c9d24eSDag-Erling SmørgravThere must be a call to 21391c9d24eSDag-Erling Smørgrav.Fn sbuf_delete 21491c9d24eSDag-Erling Smørgravfor every call to 21591c9d24eSDag-Erling Smørgrav.Fn sbuf_new . 21691c9d24eSDag-Erling SmørgravAny attempt to access the sbuf after it has been deleted will fail. 217863edaecSDag-Erling Smørgrav.Pp 218863edaecSDag-Erling SmørgravThe 219e499b74dSDag-Erling Smørgrav.Fn sbuf_clear 220e499b74dSDag-Erling Smørgravfunction invalidates the contents of the 221e499b74dSDag-Erling Smørgrav.Fa sbuf 222e499b74dSDag-Erling Smørgravand resets its position to zero. 223e499b74dSDag-Erling Smørgrav.Pp 224e499b74dSDag-Erling SmørgravThe 225*f4d28142SIan Lepore.Fn sbuf_get_flags 226*f4d28142SIan Leporefunction returns the current user flags. 227*f4d28142SIan LeporeThe 228*f4d28142SIan Lepore.Fn sbuf_set_flags 229*f4d28142SIan Leporeand 230*f4d28142SIan Lepore.Fn sbuf_get_flags 231*f4d28142SIan Leporefunctions set or clear one or more user flags, respectively. 232*f4d28142SIan LeporeThe user flags are described under the 233*f4d28142SIan Lepore.Fn sbuf_new 234*f4d28142SIan Leporefunction. 235*f4d28142SIan Lepore.Pp 236*f4d28142SIan LeporeThe 237863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 238863edaecSDag-Erling Smørgravfunction sets the 239863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's 240371c0c84SKelly Yanceyend position to 241863edaecSDag-Erling Smørgrav.Fa pos , 242863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the 243863edaecSDag-Erling Smørgravstorage buffer. 244371c0c84SKelly YanceyThis effectively truncates the sbuf at the new position. 245863edaecSDag-Erling Smørgrav.Pp 246863edaecSDag-Erling SmørgravThe 247b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat 248b0def2b5SDag-Erling Smørgravfunction appends the first 249b0def2b5SDag-Erling Smørgrav.Fa len 2504aa90de3SDag-Erling Smørgravbytes from the buffer 2514aa90de3SDag-Erling Smørgrav.Fa buf 252b0def2b5SDag-Erling Smørgravto the 253b0def2b5SDag-Erling Smørgrav.Fa sbuf . 254b0def2b5SDag-Erling Smørgrav.Pp 255b0def2b5SDag-Erling SmørgravThe 256b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin 257b0def2b5SDag-Erling Smørgravfunction copies 258b0def2b5SDag-Erling Smørgrav.Fa len 259b0def2b5SDag-Erling Smørgravbytes from the specified userland address into the 260b0def2b5SDag-Erling Smørgrav.Fa sbuf . 261b0def2b5SDag-Erling Smørgrav.Pp 262b0def2b5SDag-Erling SmørgravThe 263b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy 264b0def2b5SDag-Erling Smørgravfunction replaces the contents of the 265b0def2b5SDag-Erling Smørgrav.Fa sbuf 266b0def2b5SDag-Erling Smørgravwith the first 267b0def2b5SDag-Erling Smørgrav.Fa len 2684aa90de3SDag-Erling Smørgravbytes from the buffer 2694aa90de3SDag-Erling Smørgrav.Fa buf . 270b0def2b5SDag-Erling Smørgrav.Pp 271b0def2b5SDag-Erling SmørgravThe 272863edaecSDag-Erling Smørgrav.Fn sbuf_cat 273b0def2b5SDag-Erling Smørgravfunction appends the NUL-terminated string 274863edaecSDag-Erling Smørgrav.Fa str 275863edaecSDag-Erling Smørgravto the 276863edaecSDag-Erling Smørgrav.Fa sbuf 277863edaecSDag-Erling Smørgravat the current position. 278863edaecSDag-Erling Smørgrav.Pp 279863edaecSDag-Erling SmørgravThe 2804351ba27SMatthew D Fleming.Fn sbuf_set_drain 2814351ba27SMatthew D Flemingfunction sets a drain function 2824351ba27SMatthew D Fleming.Fa func 2834351ba27SMatthew D Flemingfor the 2844351ba27SMatthew D Fleming.Fa sbuf , 2854351ba27SMatthew D Flemingand records a pointer 2864351ba27SMatthew D Fleming.Fa arg 2874351ba27SMatthew D Flemingto be passed to the drain on callback. 2884351ba27SMatthew D FlemingThe drain function cannot be changed while 2894351ba27SMatthew D Fleming.Fa sbuf_len 2904351ba27SMatthew D Flemingis non-zero. 2914351ba27SMatthew D Fleming.Pp 2924351ba27SMatthew D FlemingThe registered drain function 2934351ba27SMatthew D Fleming.Vt sbuf_drain_func 2944351ba27SMatthew D Flemingwill be called with the argument 2954351ba27SMatthew D Fleming.Fa arg 2964351ba27SMatthew D Flemingprovided to 2974351ba27SMatthew D Fleming.Fn sbuf_set_drain , 2984351ba27SMatthew D Fleminga pointer 2994351ba27SMatthew D Fleming.Fa data 3004351ba27SMatthew D Flemingto a byte string that is the contents of the sbuf, and the length 3014351ba27SMatthew D Fleming.Fa len 3024351ba27SMatthew D Flemingof the data. 3034351ba27SMatthew D FlemingIf the drain function exists, it will be called when the sbuf internal 3044351ba27SMatthew D Flemingbuffer is full, or on behalf of 3054351ba27SMatthew D Fleming.Fn sbuf_finish . 3064351ba27SMatthew D FlemingThe drain function may drain some or all of the data, but must drain 3074351ba27SMatthew D Flemingat least 1 byte. 3084351ba27SMatthew D FlemingThe return value from the drain function, if positive, indicates how 3094351ba27SMatthew D Flemingmany bytes were drained. 3104351ba27SMatthew D FlemingIf negative, the return value indicates the negative error code which 3114351ba27SMatthew D Flemingwill be returned from this or a later call to 3124351ba27SMatthew D Fleming.Fn sbuf_finish . 3134351ba27SMatthew D FlemingThe returned drained length cannot be zero. 3144351ba27SMatthew D FlemingTo do unbuffered draining, initialize the sbuf with a two-byte buffer. 3154351ba27SMatthew D FlemingThe drain will be called for every byte added to the sbuf. 3164351ba27SMatthew D FlemingThe 3174351ba27SMatthew D Fleming.Fn sbuf_bcopyin , 3184351ba27SMatthew D Fleming.Fn sbuf_copyin , 3194351ba27SMatthew D Fleming.Fn sbuf_trim , 3204351ba27SMatthew D Flemingand 3214351ba27SMatthew D Fleming.Fn sbuf_data 3224351ba27SMatthew D Flemingfunctions cannot be used on an sbuf with a drain. 3234351ba27SMatthew D Fleming.Pp 3244351ba27SMatthew D FlemingThe 325b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin 326b0def2b5SDag-Erling Smørgravfunction copies a NUL-terminated string from the specified userland 327b0def2b5SDag-Erling Smørgravaddress into the 328b0def2b5SDag-Erling Smørgrav.Fa sbuf . 329b0def2b5SDag-Erling SmørgravIf the 330b0def2b5SDag-Erling Smørgrav.Fa len 331b0def2b5SDag-Erling Smørgravargument is non-zero, no more than 332b0def2b5SDag-Erling Smørgrav.Fa len 333b0def2b5SDag-Erling Smørgravcharacters (not counting the terminating NUL) are copied; otherwise 334b0def2b5SDag-Erling Smørgravthe entire string, or as much of it as can fit in the 335b0def2b5SDag-Erling Smørgrav.Fa sbuf , 336b0def2b5SDag-Erling Smørgravis copied. 337b0def2b5SDag-Erling Smørgrav.Pp 338b0def2b5SDag-Erling SmørgravThe 339863edaecSDag-Erling Smørgrav.Fn sbuf_cpy 340863edaecSDag-Erling Smørgravfunction replaces the contents of the 341863edaecSDag-Erling Smørgrav.Fa sbuf 342b0def2b5SDag-Erling Smørgravwith those of the NUL-terminated string 343863edaecSDag-Erling Smørgrav.Fa str . 344863edaecSDag-Erling SmørgravThis is equivalent to calling 345863edaecSDag-Erling Smørgrav.Fn sbuf_cat 346863edaecSDag-Erling Smørgravwith a fresh 347863edaecSDag-Erling Smørgrav.Fa sbuf 348863edaecSDag-Erling Smørgravor one which position has been reset to zero with 349e499b74dSDag-Erling Smørgrav.Fn sbuf_clear 350e499b74dSDag-Erling Smørgravor 351863edaecSDag-Erling Smørgrav.Fn sbuf_setpos . 352863edaecSDag-Erling Smørgrav.Pp 353863edaecSDag-Erling SmørgravThe 354863edaecSDag-Erling Smørgrav.Fn sbuf_printf 355863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed 356863edaecSDag-Erling Smørgravto by 357863edaecSDag-Erling Smørgrav.Fa fmt 358863edaecSDag-Erling Smørgravand appends the resulting string to the 359863edaecSDag-Erling Smørgrav.Fa sbuf 360863edaecSDag-Erling Smørgravat the current position. 361863edaecSDag-Erling Smørgrav.Pp 362863edaecSDag-Erling SmørgravThe 363371c0c84SKelly Yancey.Fn sbuf_vprintf 364371c0c84SKelly Yanceyfunction behaves the same as 365371c0c84SKelly Yancey.Fn sbuf_printf 366371c0c84SKelly Yanceyexcept that the arguments are obtained from the variable-length argument list 367371c0c84SKelly Yancey.Fa ap . 368371c0c84SKelly Yancey.Pp 369371c0c84SKelly YanceyThe 370863edaecSDag-Erling Smørgrav.Fn sbuf_putc 371863edaecSDag-Erling Smørgravfunction appends the character 372863edaecSDag-Erling Smørgrav.Fa c 373863edaecSDag-Erling Smørgravto the 374863edaecSDag-Erling Smørgrav.Fa sbuf 375863edaecSDag-Erling Smørgravat the current position. 376863edaecSDag-Erling Smørgrav.Pp 377863edaecSDag-Erling SmørgravThe 378371c0c84SKelly Yancey.Fn sbuf_trim 379371c0c84SKelly Yanceyfunction removes trailing whitespace from the 380371c0c84SKelly Yancey.Fa sbuf . 381371c0c84SKelly Yancey.Pp 382371c0c84SKelly YanceyThe 3834d369413SMatthew D Fleming.Fn sbuf_error 3844d369413SMatthew D Flemingfunction returns any error value that the 3854d369413SMatthew D Fleming.Fa sbuf 3864d369413SMatthew D Flemingmay have accumulated, either from the drain function, or ENOMEM if the 387e499b74dSDag-Erling Smørgrav.Fa sbuf 388e499b74dSDag-Erling Smørgravoverflowed. 3894d369413SMatthew D FlemingThis function is generally not needed and instead the error code from 3904d369413SMatthew D Fleming.Fn sbuf_finish 3914d369413SMatthew D Flemingis the preferred way to discover whether an sbuf had an error. 392e499b74dSDag-Erling Smørgrav.Pp 393e499b74dSDag-Erling SmørgravThe 394863edaecSDag-Erling Smørgrav.Fn sbuf_finish 3954351ba27SMatthew D Flemingfunction will call the attached drain function if one exists until all 3964351ba27SMatthew D Flemingthe data in the 397863edaecSDag-Erling Smørgrav.Fa sbuf 3984351ba27SMatthew D Flemingis flushed. 3994351ba27SMatthew D FlemingIf there is no attached drain, 4004351ba27SMatthew D Fleming.Fn sbuf_finish 4014351ba27SMatthew D FlemingNUL-terminates the 4024351ba27SMatthew D Fleming.Fa sbuf . 4034351ba27SMatthew D FlemingIn either case it marks the 4044351ba27SMatthew D Fleming.Fa sbuf 4054351ba27SMatthew D Flemingas finished, which means that it may no longer be modified using 406863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 407863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 4083393f8daSKenneth D. Merry.Fn sbuf_cpy , 409863edaecSDag-Erling Smørgrav.Fn sbuf_printf 410863edaecSDag-Erling Smørgravor 411db960cf0SMatthew D Fleming.Fn sbuf_putc , 412db960cf0SMatthew D Fleminguntil 413db960cf0SMatthew D Fleming.Fn sbuf_clear 414db960cf0SMatthew D Flemingis used to reset the sbuf. 415863edaecSDag-Erling Smørgrav.Pp 416863edaecSDag-Erling SmørgravThe 417863edaecSDag-Erling Smørgrav.Fn sbuf_data 4184351ba27SMatthew D Flemingfunction returns the actual string; 419e499b74dSDag-Erling Smørgrav.Fn sbuf_data 420e499b74dSDag-Erling Smørgravonly works on a finished 421863edaecSDag-Erling Smørgrav.Fa sbuf . 4224351ba27SMatthew D FlemingThe 423c0825cd5SJaakko Heinonen.Fn sbuf_len 424c0825cd5SJaakko Heinonenfunction returns the length of the string. 4254351ba27SMatthew D FlemingFor an 4264351ba27SMatthew D Fleming.Fa sbuf 4274351ba27SMatthew D Flemingwith an attached drain, 4284351ba27SMatthew D Fleming.Fn sbuf_len 4294351ba27SMatthew D Flemingreturns the length of the un-drained data. 4304351ba27SMatthew D Fleming.Fn sbuf_done 4314351ba27SMatthew D Flemingreturns non-zero if the 4324351ba27SMatthew D Fleming.Fa sbuf 4334351ba27SMatthew D Flemingis finished. 434f02db7b4SMikolaj Golub.Pp 435f02db7b4SMikolaj GolubThe 436f02db7b4SMikolaj Golub.Fn sbuf_start_section 437f02db7b4SMikolaj Goluband 438f02db7b4SMikolaj Golub.Fn sbuf_end_section 439f02db7b4SMikolaj Golubfunctions may be used for automatic section alignment. 440f02db7b4SMikolaj GolubThe arguments 441f02db7b4SMikolaj Golub.Fa pad 442f02db7b4SMikolaj Goluband 443f02db7b4SMikolaj Golub.Fa c 444f02db7b4SMikolaj Golubspecify the padding size and a character used for padding. 445f02db7b4SMikolaj GolubThe arguments 446f02db7b4SMikolaj Golub.Fa old_lenp 447f02db7b4SMikolaj Goluband 448f02db7b4SMikolaj Golub.Fa old_len 449f02db7b4SMikolaj Golubare to save and restore the current section length when nested sections 450f02db7b4SMikolaj Golubare used. 451f02db7b4SMikolaj GolubFor the top level section 452f02db7b4SMikolaj Golub.Dv NULL 453f02db7b4SMikolaj Goluband \-1 can be specified for 454f02db7b4SMikolaj Golub.Fa old_lenp 455f02db7b4SMikolaj Goluband 456f02db7b4SMikolaj Golub.Fa old_len 457f02db7b4SMikolaj Golubrespectively. 458863edaecSDag-Erling Smørgrav.Sh NOTES 459863edaecSDag-Erling SmørgravIf an operation caused an 460863edaecSDag-Erling Smørgrav.Fa sbuf 461e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the 462e499b74dSDag-Erling Smørgrav.Fa sbuf 463e499b74dSDag-Erling Smørgravis finished using 464e499b74dSDag-Erling Smørgrav.Fn sbuf_finish 465e499b74dSDag-Erling Smørgravor reset using 466e499b74dSDag-Erling Smørgrav.Fn sbuf_clear , 467e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the 468e499b74dSDag-Erling Smørgravsize of its storage buffer using 469863edaecSDag-Erling Smørgrav.Fn sbuf_setpos , 470863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using 471863edaecSDag-Erling Smørgrav.Fn sbuf_cpy . 4724351ba27SMatthew D Fleming.Pp 4734351ba27SMatthew D FlemingDrains in user-space will not always function as indicated. 4744351ba27SMatthew D FlemingWhile the drain function will be called immediately on overflow from 4754351ba27SMatthew D Flemingthe 4764351ba27SMatthew D Fleming.Fa sbuf_putc , 4774351ba27SMatthew D Fleming.Fa sbuf_bcat , 4784351ba27SMatthew D Fleming.Fa sbuf_cat 4794351ba27SMatthew D Flemingfunctions, 4804351ba27SMatthew D Fleming.Fa sbuf_printf 4814351ba27SMatthew D Flemingand 4824351ba27SMatthew D Fleming.Fa sbuf_vprintf 4834351ba27SMatthew D Flemingcurrently have no way to determine whether there will be an overflow 4844351ba27SMatthew D Fleminguntil after it occurs, and cannot do a partial expansion of the format 4854351ba27SMatthew D Flemingstring. 4864351ba27SMatthew D FlemingThus when using libsbuf the buffer may be extended to allow completion 4874351ba27SMatthew D Flemingof a single printf call, even though a drain is attached. 488863edaecSDag-Erling Smørgrav.Sh RETURN VALUES 48981ae4b8dSRuslan ErmilovThe 490863edaecSDag-Erling Smørgrav.Fn sbuf_new 49181ae4b8dSRuslan Ermilovfunction returns 492d6479358SDag-Erling Smørgrav.Dv NULL 493d6479358SDag-Erling Smørgravif it failed to allocate a storage buffer, and a pointer to the new 494d6479358SDag-Erling Smørgrav.Fa sbuf 495863edaecSDag-Erling Smørgravotherwise. 496863edaecSDag-Erling Smørgrav.Pp 49781ae4b8dSRuslan ErmilovThe 498863edaecSDag-Erling Smørgrav.Fn sbuf_setpos 49981ae4b8dSRuslan Ermilovfunction returns \-1 if 500863edaecSDag-Erling Smørgrav.Fa pos 501863edaecSDag-Erling Smørgravwas invalid, and zero otherwise. 502863edaecSDag-Erling Smørgrav.Pp 50381ae4b8dSRuslan ErmilovThe 504863edaecSDag-Erling Smørgrav.Fn sbuf_cat , 505863edaecSDag-Erling Smørgrav.Fn sbuf_cpy , 506863edaecSDag-Erling Smørgrav.Fn sbuf_printf , 507371c0c84SKelly Yancey.Fn sbuf_putc , 508863edaecSDag-Erling Smørgravand 509371c0c84SKelly Yancey.Fn sbuf_trim 51081ae4b8dSRuslan Ermilovfunctions 511863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise. 512863edaecSDag-Erling Smørgrav.Pp 51381ae4b8dSRuslan ErmilovThe 5144d369413SMatthew D Fleming.Fn sbuf_error 5154d369413SMatthew D Flemingfunction returns a non-zero value if the buffer has an overflow or 5164d369413SMatthew D Flemingdrain error, and zero otherwise. 517e499b74dSDag-Erling Smørgrav.Pp 51881ae4b8dSRuslan ErmilovThe 519863edaecSDag-Erling Smørgrav.Fn sbuf_len 520569dd0f3SJaakko Heinonenfunction returns \-1 if the buffer overflowed. 52149091c48SPoul-Henning Kamp.Pp 52281ae4b8dSRuslan ErmilovThe 52349091c48SPoul-Henning Kamp.Fn sbuf_copyin 52481ae4b8dSRuslan Ermilovfunction 52549091c48SPoul-Henning Kampreturns \-1 if copying string from userland failed, and number of bytes 52649091c48SPoul-Henning Kampcopied otherwise. 5276ab7244aSPoul-Henning Kamp.Pp 5284351ba27SMatthew D FlemingThe 529f02db7b4SMikolaj Golub.Fn sbuf_end_section 530f02db7b4SMikolaj Golubfunction returns the section length or \-1 if the buffer has an error. 531f02db7b4SMikolaj Golub.Pp 532f02db7b4SMikolaj GolubThe 5336ab7244aSPoul-Henning Kamp.Fn sbuf_finish 9 5346ab7244aSPoul-Henning Kampfunction (the kernel version) returns ENOMEM if the sbuf overflowed before 5356ab7244aSPoul-Henning Kampbeing finished, 5364351ba27SMatthew D Flemingor returns the error code from the drain if one is attached. 5376ab7244aSPoul-Henning Kamp.Pp 5386ab7244aSPoul-Henning KampThe 5396ab7244aSPoul-Henning Kamp.Fn sbuf_finish 3 5406ab7244aSPoul-Henning Kampfunction (the userland version) 5416ab7244aSPoul-Henning Kampwill return zero for success and \-1 and set errno on error. 5426ab7244aSPoul-Henning Kamp.Sh EXAMPLES 5436ab7244aSPoul-Henning Kamp.Bd -literal -compact 5446ab7244aSPoul-Henning Kamp#include <sys/sbuf.h> 5456ab7244aSPoul-Henning Kamp 5466ab7244aSPoul-Henning Kampstruct sbuf *sb; 5476ab7244aSPoul-Henning Kamp 5486ab7244aSPoul-Henning Kampsb = sbuf_new_auto(); 549c920aa7aSPoul-Henning Kampsbuf_cat(sb, "Customers found:\en"); 5506ab7244aSPoul-Henning KampTAILQ_FOREACH(foo, &foolist, list) { 551c920aa7aSPoul-Henning Kamp sbuf_printf(sb, " %4d %s\en", foo->index, foo->name); 552c920aa7aSPoul-Henning Kamp sbuf_printf(sb, " Address: %s\en", foo->address); 553c920aa7aSPoul-Henning Kamp sbuf_printf(sb, " Zip: %s\en", foo->zipcode); 5546ab7244aSPoul-Henning Kamp} 555c5a0813dSJaakko Heinonenif (sbuf_finish(sb) != 0) /* Check for any and all errors */ 5566ab7244aSPoul-Henning Kamp err(1, "Could not generate message"); 5576ab7244aSPoul-Henning Kamptransmit_msg(sbuf_data(sb), sbuf_len(sb)); 5586ab7244aSPoul-Henning Kampsbuf_delete(sb); 5596ab7244aSPoul-Henning Kamp.Ed 560863edaecSDag-Erling Smørgrav.Sh SEE ALSO 561863edaecSDag-Erling Smørgrav.Xr printf 3 , 562863edaecSDag-Erling Smørgrav.Xr strcat 3 , 563d68b700aSRuslan Ermilov.Xr strcpy 3 , 564d68b700aSRuslan Ermilov.Xr copyin 9 , 565e05f3134SAndrew R. Reiter.Xr copyinstr 9 , 566e05f3134SAndrew R. Reiter.Xr printf 9 567863edaecSDag-Erling Smørgrav.Sh HISTORY 568863edaecSDag-Erling SmørgravThe 56981ae4b8dSRuslan Ermilov.Nm 570863edaecSDag-Erling Smørgravfamily of functions first appeared in 57122cbe5e3SDag-Erling Smørgrav.Fx 4.4 . 572863edaecSDag-Erling Smørgrav.Sh AUTHORS 573863edaecSDag-Erling Smørgrav.An -nosplit 574863edaecSDag-Erling SmørgravThe 57581ae4b8dSRuslan Ermilov.Nm 576863edaecSDag-Erling Smørgravfamily of functions was designed by 5778a7314fcSBaptiste Daroussin.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org 578863edaecSDag-Erling Smørgravand implemented by 5798a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 580e499b74dSDag-Erling SmørgravAdditional improvements were suggested by 5818a7314fcSBaptiste Daroussin.An Justin T. Gibbs Aq Mt gibbs@FreeBSD.org . 582371c0c84SKelly YanceyAuto-extend support added by 5838a7314fcSBaptiste Daroussin.An Kelly Yancey Aq Mt kbyanc@FreeBSD.org . 5844351ba27SMatthew D FlemingDrain functionality added by 5858a7314fcSBaptiste Daroussin.An Matthew Fleming Aq Mt mdf@FreeBSD.org . 586863edaecSDag-Erling Smørgrav.Pp 587863edaecSDag-Erling SmørgravThis manual page was written by 5888a7314fcSBaptiste Daroussin.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org . 589