xref: /freebsd/share/man/man9/sbuf.9 (revision c5a0813d43ab213aba5bfbf8ad2a3bbd04c7f2c7)
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*c5a0813dSJaakko Heinonen.Dd December 21, 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
556ab7244aSPoul-Henning Kamp.Nd safe string composition
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
1096ab7244aSPoul-Henning Kampfamily of functions allows one to safely allocate, compose and
1106ab7244aSPoul-Henning Kamprelease strings in kernel or user space.
1116ab7244aSPoul-Henning Kamp.Pp
112863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures
113863edaecSDag-Erling Smørgravcalled
114863edaecSDag-Erling Smørgrav.Fa sbufs ,
115863edaecSDag-Erling Smørgravdefined in
116fe08efe6SRuslan Ermilov.In sys/sbuf.h .
117863edaecSDag-Erling Smørgrav.Pp
1186ab7244aSPoul-Henning KampAny errors encountered during the allocation or composition of the
1196ab7244aSPoul-Henning Kampstring will be latched in the data structure,
1206ab7244aSPoul-Henning Kampmaking a single error test at the end of the composition
1216ab7244aSPoul-Henning Kampsufficient to determine success or failure of the entire process.
1226ab7244aSPoul-Henning Kamp.Pp
123863edaecSDag-Erling SmørgravThe
124863edaecSDag-Erling Smørgrav.Fn sbuf_new
125863edaecSDag-Erling Smørgravfunction initializes the
126863edaecSDag-Erling Smørgrav.Fa sbuf
127863edaecSDag-Erling Smørgravpointed to by its first argument.
128d6479358SDag-Erling SmørgravIf that pointer is
129d6479358SDag-Erling Smørgrav.Dv NULL ,
130d6479358SDag-Erling Smørgrav.Fn sbuf_new
131d6479358SDag-Erling Smørgravallocates a
132d68b700aSRuslan Ermilov.Vt struct sbuf
133d6479358SDag-Erling Smørgravusing
134d6479358SDag-Erling Smørgrav.Xr malloc 9 .
135863edaecSDag-Erling SmørgravThe
136863edaecSDag-Erling Smørgrav.Fa buf
137863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string;
138863edaecSDag-Erling Smørgravif it is
139863edaecSDag-Erling Smørgrav.Dv NULL ,
140863edaecSDag-Erling Smørgrav.Fn sbuf_new
141863edaecSDag-Erling Smørgravwill allocate one using
142863edaecSDag-Erling Smørgrav.Xr malloc 9 .
143863edaecSDag-Erling SmørgravThe
144863edaecSDag-Erling Smørgrav.Fa length
145371c0c84SKelly Yanceyis the initial size of the storage buffer.
146863edaecSDag-Erling SmørgravThe fourth argument,
147863edaecSDag-Erling Smørgrav.Fa flags ,
148371c0c84SKelly Yanceymay be comprised of the following flags:
1493b0536faSRuslan Ermilov.Bl -tag -width ".Dv SBUF_AUTOEXTEND"
150371c0c84SKelly Yancey.It Dv SBUF_FIXEDLEN
1513b0536faSRuslan ErmilovThe storage buffer is fixed at its initial size.
152371c0c84SKelly YanceyAttempting to extend the sbuf beyond this size results in an overflow condition.
153371c0c84SKelly Yancey.It Dv SBUF_AUTOEXTEND
154371c0c84SKelly YanceyThis indicates that the storage buffer may be extended as necessary, so long
155371c0c84SKelly Yanceyas resources allow, to hold additional data.
156371c0c84SKelly Yancey.El
157863edaecSDag-Erling Smørgrav.Pp
158863edaecSDag-Erling SmørgravNote that if
159863edaecSDag-Erling Smørgrav.Fa buf
160863edaecSDag-Erling Smørgravis not
161863edaecSDag-Erling Smørgrav.Dv NULL ,
162863edaecSDag-Erling Smørgravit must point to an array of at least
163863edaecSDag-Erling Smørgrav.Fa length
164863edaecSDag-Erling Smørgravcharacters.
16591c9d24eSDag-Erling SmørgravThe result of accessing that array directly while it is in use by the
16691c9d24eSDag-Erling Smørgravsbuf is undefined.
16791c9d24eSDag-Erling Smørgrav.Pp
16891c9d24eSDag-Erling SmørgravThe
16972fba9d7SChristian Brueffer.Fn sbuf_new_auto
17072fba9d7SChristian Bruefferfunction is a shortcut for creating a completely dynamic
17172fba9d7SChristian Brueffer.Nm .
17272fba9d7SChristian BruefferIt is the equivalent of calling
17372fba9d7SChristian Brueffer.Fn sbuf_new
17472fba9d7SChristian Bruefferwith values
17572fba9d7SChristian Brueffer.Dv NULL ,
17672fba9d7SChristian Brueffer.Dv NULL ,
17772fba9d7SChristian Brueffer.Dv 0 ,
17872fba9d7SChristian Bruefferand
17972fba9d7SChristian Brueffer.Dv SBUF_AUTOEXTEND .
18072fba9d7SChristian Brueffer.Pp
18172fba9d7SChristian BruefferThe
1827c2dd57bSMatthew D Fleming.Fn sbuf_new_for_sysctl
1837c2dd57bSMatthew D Flemingfunction will set up an sbuf with a drain function to use
1847c2dd57bSMatthew D Fleming.Fn SYSCTL_OUT
1857c2dd57bSMatthew D Flemingwhen the internal buffer fills.
18600f0e671SMatthew D FlemingNote that if the various functions which append to an sbuf are used while
18700f0e671SMatthew D Fleminga non-sleepable lock is held, the user buffer should be wired using
18800f0e671SMatthew D Fleming.Fn sysctl_wire_old_buffer .
1897c2dd57bSMatthew D Fleming.Pp
1907c2dd57bSMatthew D FlemingThe
19191c9d24eSDag-Erling Smørgrav.Fn sbuf_delete
19291c9d24eSDag-Erling Smørgravfunction clears the
19391c9d24eSDag-Erling Smørgrav.Fa sbuf
19491c9d24eSDag-Erling Smørgravand frees any memory allocated for it.
19591c9d24eSDag-Erling SmørgravThere must be a call to
19691c9d24eSDag-Erling Smørgrav.Fn sbuf_delete
19791c9d24eSDag-Erling Smørgravfor every call to
19891c9d24eSDag-Erling Smørgrav.Fn sbuf_new .
19991c9d24eSDag-Erling SmørgravAny attempt to access the sbuf after it has been deleted will fail.
200863edaecSDag-Erling Smørgrav.Pp
201863edaecSDag-Erling SmørgravThe
202e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
203e499b74dSDag-Erling Smørgravfunction invalidates the contents of the
204e499b74dSDag-Erling Smørgrav.Fa sbuf
205e499b74dSDag-Erling Smørgravand resets its position to zero.
206e499b74dSDag-Erling Smørgrav.Pp
207e499b74dSDag-Erling SmørgravThe
208863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
209863edaecSDag-Erling Smørgravfunction sets the
210863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's
211371c0c84SKelly Yanceyend position to
212863edaecSDag-Erling Smørgrav.Fa pos ,
213863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the
214863edaecSDag-Erling Smørgravstorage buffer.
215371c0c84SKelly YanceyThis effectively truncates the sbuf at the new position.
216863edaecSDag-Erling Smørgrav.Pp
217863edaecSDag-Erling SmørgravThe
218b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat
219b0def2b5SDag-Erling Smørgravfunction appends the first
220b0def2b5SDag-Erling Smørgrav.Fa len
2214aa90de3SDag-Erling Smørgravbytes from the buffer
2224aa90de3SDag-Erling Smørgrav.Fa buf
223b0def2b5SDag-Erling Smørgravto the
224b0def2b5SDag-Erling Smørgrav.Fa sbuf .
225b0def2b5SDag-Erling Smørgrav.Pp
226b0def2b5SDag-Erling SmørgravThe
227b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin
228b0def2b5SDag-Erling Smørgravfunction copies
229b0def2b5SDag-Erling Smørgrav.Fa len
230b0def2b5SDag-Erling Smørgravbytes from the specified userland address into the
231b0def2b5SDag-Erling Smørgrav.Fa sbuf .
232b0def2b5SDag-Erling Smørgrav.Pp
233b0def2b5SDag-Erling SmørgravThe
234b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy
235b0def2b5SDag-Erling Smørgravfunction replaces the contents of the
236b0def2b5SDag-Erling Smørgrav.Fa sbuf
237b0def2b5SDag-Erling Smørgravwith the first
238b0def2b5SDag-Erling Smørgrav.Fa len
2394aa90de3SDag-Erling Smørgravbytes from the buffer
2404aa90de3SDag-Erling Smørgrav.Fa buf .
241b0def2b5SDag-Erling Smørgrav.Pp
242b0def2b5SDag-Erling SmørgravThe
243863edaecSDag-Erling Smørgrav.Fn sbuf_cat
244b0def2b5SDag-Erling Smørgravfunction appends the NUL-terminated string
245863edaecSDag-Erling Smørgrav.Fa str
246863edaecSDag-Erling Smørgravto the
247863edaecSDag-Erling Smørgrav.Fa sbuf
248863edaecSDag-Erling Smørgravat the current position.
249863edaecSDag-Erling Smørgrav.Pp
250863edaecSDag-Erling SmørgravThe
2514351ba27SMatthew D Fleming.Fn sbuf_set_drain
2524351ba27SMatthew D Flemingfunction sets a drain function
2534351ba27SMatthew D Fleming.Fa func
2544351ba27SMatthew D Flemingfor the
2554351ba27SMatthew D Fleming.Fa sbuf ,
2564351ba27SMatthew D Flemingand records a pointer
2574351ba27SMatthew D Fleming.Fa arg
2584351ba27SMatthew D Flemingto be passed to the drain on callback.
2594351ba27SMatthew D FlemingThe drain function cannot be changed while
2604351ba27SMatthew D Fleming.Fa sbuf_len
2614351ba27SMatthew D Flemingis non-zero.
2624351ba27SMatthew D Fleming.Pp
2634351ba27SMatthew D FlemingThe registered drain function
2644351ba27SMatthew D Fleming.Vt sbuf_drain_func
2654351ba27SMatthew D Flemingwill be called with the argument
2664351ba27SMatthew D Fleming.Fa arg
2674351ba27SMatthew D Flemingprovided to
2684351ba27SMatthew D Fleming.Fn sbuf_set_drain ,
2694351ba27SMatthew D Fleminga pointer
2704351ba27SMatthew D Fleming.Fa data
2714351ba27SMatthew D Flemingto a byte string that is the contents of the sbuf, and the length
2724351ba27SMatthew D Fleming.Fa len
2734351ba27SMatthew D Flemingof the data.
2744351ba27SMatthew D FlemingIf the drain function exists, it will be called when the sbuf internal
2754351ba27SMatthew D Flemingbuffer is full, or on behalf of
2764351ba27SMatthew D Fleming.Fn sbuf_finish .
2774351ba27SMatthew D FlemingThe drain function may drain some or all of the data, but must drain
2784351ba27SMatthew D Flemingat least 1 byte.
2794351ba27SMatthew D FlemingThe return value from the drain function, if positive, indicates how
2804351ba27SMatthew D Flemingmany bytes were drained.
2814351ba27SMatthew D FlemingIf negative, the return value indicates the negative error code which
2824351ba27SMatthew D Flemingwill be returned from this or a later call to
2834351ba27SMatthew D Fleming.Fn sbuf_finish .
2844351ba27SMatthew D FlemingThe returned drained length cannot be zero.
2854351ba27SMatthew D FlemingTo do unbuffered draining, initialize the sbuf with a two-byte buffer.
2864351ba27SMatthew D FlemingThe drain will be called for every byte added to the sbuf.
2874351ba27SMatthew D FlemingThe
2884351ba27SMatthew D Fleming.Fn sbuf_bcopyin ,
2894351ba27SMatthew D Fleming.Fn sbuf_copyin ,
2904351ba27SMatthew D Fleming.Fn sbuf_trim ,
2914351ba27SMatthew D Flemingand
2924351ba27SMatthew D Fleming.Fn sbuf_data
2934351ba27SMatthew D Flemingfunctions cannot be used on an sbuf with a drain.
2944351ba27SMatthew D Fleming.Pp
2954351ba27SMatthew D FlemingThe
296b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin
297b0def2b5SDag-Erling Smørgravfunction copies a NUL-terminated string from the specified userland
298b0def2b5SDag-Erling Smørgravaddress into the
299b0def2b5SDag-Erling Smørgrav.Fa sbuf .
300b0def2b5SDag-Erling SmørgravIf the
301b0def2b5SDag-Erling Smørgrav.Fa len
302b0def2b5SDag-Erling Smørgravargument is non-zero, no more than
303b0def2b5SDag-Erling Smørgrav.Fa len
304b0def2b5SDag-Erling Smørgravcharacters (not counting the terminating NUL) are copied; otherwise
305b0def2b5SDag-Erling Smørgravthe entire string, or as much of it as can fit in the
306b0def2b5SDag-Erling Smørgrav.Fa sbuf ,
307b0def2b5SDag-Erling Smørgravis copied.
308b0def2b5SDag-Erling Smørgrav.Pp
309b0def2b5SDag-Erling SmørgravThe
310863edaecSDag-Erling Smørgrav.Fn sbuf_cpy
311863edaecSDag-Erling Smørgravfunction replaces the contents of the
312863edaecSDag-Erling Smørgrav.Fa sbuf
313b0def2b5SDag-Erling Smørgravwith those of the NUL-terminated string
314863edaecSDag-Erling Smørgrav.Fa str .
315863edaecSDag-Erling SmørgravThis is equivalent to calling
316863edaecSDag-Erling Smørgrav.Fn sbuf_cat
317863edaecSDag-Erling Smørgravwith a fresh
318863edaecSDag-Erling Smørgrav.Fa sbuf
319863edaecSDag-Erling Smørgravor one which position has been reset to zero with
320e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
321e499b74dSDag-Erling Smørgravor
322863edaecSDag-Erling Smørgrav.Fn sbuf_setpos .
323863edaecSDag-Erling Smørgrav.Pp
324863edaecSDag-Erling SmørgravThe
325863edaecSDag-Erling Smørgrav.Fn sbuf_printf
326863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed
327863edaecSDag-Erling Smørgravto by
328863edaecSDag-Erling Smørgrav.Fa fmt
329863edaecSDag-Erling Smørgravand appends the resulting string to the
330863edaecSDag-Erling Smørgrav.Fa sbuf
331863edaecSDag-Erling Smørgravat the current position.
332863edaecSDag-Erling Smørgrav.Pp
333863edaecSDag-Erling SmørgravThe
334371c0c84SKelly Yancey.Fn sbuf_vprintf
335371c0c84SKelly Yanceyfunction behaves the same as
336371c0c84SKelly Yancey.Fn sbuf_printf
337371c0c84SKelly Yanceyexcept that the arguments are obtained from the variable-length argument list
338371c0c84SKelly Yancey.Fa ap .
339371c0c84SKelly Yancey.Pp
340371c0c84SKelly YanceyThe
341863edaecSDag-Erling Smørgrav.Fn sbuf_putc
342863edaecSDag-Erling Smørgravfunction appends the character
343863edaecSDag-Erling Smørgrav.Fa c
344863edaecSDag-Erling Smørgravto the
345863edaecSDag-Erling Smørgrav.Fa sbuf
346863edaecSDag-Erling Smørgravat the current position.
347863edaecSDag-Erling Smørgrav.Pp
348863edaecSDag-Erling SmørgravThe
349371c0c84SKelly Yancey.Fn sbuf_trim
350371c0c84SKelly Yanceyfunction removes trailing whitespace from the
351371c0c84SKelly Yancey.Fa sbuf .
352371c0c84SKelly Yancey.Pp
353371c0c84SKelly YanceyThe
3544d369413SMatthew D Fleming.Fn sbuf_error
3554d369413SMatthew D Flemingfunction returns any error value that the
3564d369413SMatthew D Fleming.Fa sbuf
3574d369413SMatthew D Flemingmay have accumulated, either from the drain function, or ENOMEM if the
358e499b74dSDag-Erling Smørgrav.Fa sbuf
359e499b74dSDag-Erling Smørgravoverflowed.
3604d369413SMatthew D FlemingThis function is generally not needed and instead the error code from
3614d369413SMatthew D Fleming.Fn sbuf_finish
3624d369413SMatthew D Flemingis the preferred way to discover whether an sbuf had an error.
363e499b74dSDag-Erling Smørgrav.Pp
364e499b74dSDag-Erling SmørgravThe
365863edaecSDag-Erling Smørgrav.Fn sbuf_finish
3664351ba27SMatthew D Flemingfunction will call the attached drain function if one exists until all
3674351ba27SMatthew D Flemingthe data in the
368863edaecSDag-Erling Smørgrav.Fa sbuf
3694351ba27SMatthew D Flemingis flushed.
3704351ba27SMatthew D FlemingIf there is no attached drain,
3714351ba27SMatthew D Fleming.Fn sbuf_finish
3724351ba27SMatthew D FlemingNUL-terminates the
3734351ba27SMatthew D Fleming.Fa sbuf .
3744351ba27SMatthew D FlemingIn either case it marks the
3754351ba27SMatthew D Fleming.Fa sbuf
3764351ba27SMatthew D Flemingas finished, which means that it may no longer be modified using
377863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
378863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
3793393f8daSKenneth D. Merry.Fn sbuf_cpy ,
380863edaecSDag-Erling Smørgrav.Fn sbuf_printf
381863edaecSDag-Erling Smørgravor
382db960cf0SMatthew D Fleming.Fn sbuf_putc ,
383db960cf0SMatthew D Fleminguntil
384db960cf0SMatthew D Fleming.Fn sbuf_clear
385db960cf0SMatthew D Flemingis used to reset the sbuf.
386863edaecSDag-Erling Smørgrav.Pp
387863edaecSDag-Erling SmørgravThe
388863edaecSDag-Erling Smørgrav.Fn sbuf_data
3894351ba27SMatthew D Flemingfunction returns the actual string;
390e499b74dSDag-Erling Smørgrav.Fn sbuf_data
391e499b74dSDag-Erling Smørgravonly works on a finished
392863edaecSDag-Erling Smørgrav.Fa sbuf .
3934351ba27SMatthew D FlemingThe
394c0825cd5SJaakko Heinonen.Fn sbuf_len
395c0825cd5SJaakko Heinonenfunction returns the length of the string.
3964351ba27SMatthew D FlemingFor an
3974351ba27SMatthew D Fleming.Fa sbuf
3984351ba27SMatthew D Flemingwith an attached drain,
3994351ba27SMatthew D Fleming.Fn sbuf_len
4004351ba27SMatthew D Flemingreturns the length of the un-drained data.
4014351ba27SMatthew D Fleming.Fn sbuf_done
4024351ba27SMatthew D Flemingreturns non-zero if the
4034351ba27SMatthew D Fleming.Fa sbuf
4044351ba27SMatthew D Flemingis finished.
405863edaecSDag-Erling Smørgrav.Sh NOTES
406863edaecSDag-Erling SmørgravIf an operation caused an
407863edaecSDag-Erling Smørgrav.Fa sbuf
408e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the
409e499b74dSDag-Erling Smørgrav.Fa sbuf
410e499b74dSDag-Erling Smørgravis finished using
411e499b74dSDag-Erling Smørgrav.Fn sbuf_finish
412e499b74dSDag-Erling Smørgravor reset using
413e499b74dSDag-Erling Smørgrav.Fn sbuf_clear ,
414e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the
415e499b74dSDag-Erling Smørgravsize of its storage buffer using
416863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
417863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using
418863edaecSDag-Erling Smørgrav.Fn sbuf_cpy .
4194351ba27SMatthew D Fleming.Pp
4204351ba27SMatthew D FlemingDrains in user-space will not always function as indicated.
4214351ba27SMatthew D FlemingWhile the drain function will be called immediately on overflow from
4224351ba27SMatthew D Flemingthe
4234351ba27SMatthew D Fleming.Fa sbuf_putc ,
4244351ba27SMatthew D Fleming.Fa sbuf_bcat ,
4254351ba27SMatthew D Fleming.Fa sbuf_cat
4264351ba27SMatthew D Flemingfunctions,
4274351ba27SMatthew D Fleming.Fa sbuf_printf
4284351ba27SMatthew D Flemingand
4294351ba27SMatthew D Fleming.Fa sbuf_vprintf
4304351ba27SMatthew D Flemingcurrently have no way to determine whether there will be an overflow
4314351ba27SMatthew D Fleminguntil after it occurs, and cannot do a partial expansion of the format
4324351ba27SMatthew D Flemingstring.
4334351ba27SMatthew D FlemingThus when using libsbuf the buffer may be extended to allow completion
4344351ba27SMatthew D Flemingof a single printf call, even though a drain is attached.
435863edaecSDag-Erling Smørgrav.Sh RETURN VALUES
43681ae4b8dSRuslan ErmilovThe
437863edaecSDag-Erling Smørgrav.Fn sbuf_new
43881ae4b8dSRuslan Ermilovfunction returns
439d6479358SDag-Erling Smørgrav.Dv NULL
440d6479358SDag-Erling Smørgravif it failed to allocate a storage buffer, and a pointer to the new
441d6479358SDag-Erling Smørgrav.Fa sbuf
442863edaecSDag-Erling Smørgravotherwise.
443863edaecSDag-Erling Smørgrav.Pp
44481ae4b8dSRuslan ErmilovThe
445863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
44681ae4b8dSRuslan Ermilovfunction returns \-1 if
447863edaecSDag-Erling Smørgrav.Fa pos
448863edaecSDag-Erling Smørgravwas invalid, and zero otherwise.
449863edaecSDag-Erling Smørgrav.Pp
45081ae4b8dSRuslan ErmilovThe
451863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
452863edaecSDag-Erling Smørgrav.Fn sbuf_cpy ,
453863edaecSDag-Erling Smørgrav.Fn sbuf_printf ,
454371c0c84SKelly Yancey.Fn sbuf_putc ,
455863edaecSDag-Erling Smørgravand
456371c0c84SKelly Yancey.Fn sbuf_trim
45781ae4b8dSRuslan Ermilovfunctions
458863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise.
459863edaecSDag-Erling Smørgrav.Pp
46081ae4b8dSRuslan ErmilovThe
4614d369413SMatthew D Fleming.Fn sbuf_error
4624d369413SMatthew D Flemingfunction returns a non-zero value if the buffer has an overflow or
4634d369413SMatthew D Flemingdrain error, and zero otherwise.
464e499b74dSDag-Erling Smørgrav.Pp
46581ae4b8dSRuslan ErmilovThe
466863edaecSDag-Erling Smørgrav.Fn sbuf_len
467569dd0f3SJaakko Heinonenfunction returns \-1 if the buffer overflowed.
46849091c48SPoul-Henning Kamp.Pp
46981ae4b8dSRuslan ErmilovThe
47049091c48SPoul-Henning Kamp.Fn sbuf_copyin
47181ae4b8dSRuslan Ermilovfunction
47249091c48SPoul-Henning Kampreturns \-1 if copying string from userland failed, and number of bytes
47349091c48SPoul-Henning Kampcopied otherwise.
4746ab7244aSPoul-Henning Kamp.Pp
4754351ba27SMatthew D FlemingThe
4766ab7244aSPoul-Henning Kamp.Fn sbuf_finish 9
4776ab7244aSPoul-Henning Kampfunction (the kernel version) returns ENOMEM if the sbuf overflowed before
4786ab7244aSPoul-Henning Kampbeing finished,
4794351ba27SMatthew D Flemingor returns the error code from the drain if one is attached.
4806ab7244aSPoul-Henning Kamp.Pp
4816ab7244aSPoul-Henning KampThe
4826ab7244aSPoul-Henning Kamp.Fn sbuf_finish 3
4836ab7244aSPoul-Henning Kampfunction (the userland version)
4846ab7244aSPoul-Henning Kampwill return zero for success and \-1 and set errno on error.
4856ab7244aSPoul-Henning Kamp.Sh EXAMPLES
4866ab7244aSPoul-Henning Kamp.Bd -literal -compact
4876ab7244aSPoul-Henning Kamp#include <sys/sbuf.h>
4886ab7244aSPoul-Henning Kamp
4896ab7244aSPoul-Henning Kampstruct sbuf *sb;
4906ab7244aSPoul-Henning Kamp
4916ab7244aSPoul-Henning Kampsb = sbuf_new_auto();
492c920aa7aSPoul-Henning Kampsbuf_cat(sb, "Customers found:\en");
4936ab7244aSPoul-Henning KampTAILQ_FOREACH(foo, &foolist, list) {
494c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "   %4d %s\en", foo->index, foo->name);
495c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "      Address: %s\en", foo->address);
496c920aa7aSPoul-Henning Kamp	sbuf_printf(sb, "      Zip: %s\en", foo->zipcode);
4976ab7244aSPoul-Henning Kamp}
498*c5a0813dSJaakko Heinonenif (sbuf_finish(sb) != 0) /* Check for any and all errors */
4996ab7244aSPoul-Henning Kamp	err(1, "Could not generate message");
5006ab7244aSPoul-Henning Kamptransmit_msg(sbuf_data(sb), sbuf_len(sb));
5016ab7244aSPoul-Henning Kampsbuf_delete(sb);
5026ab7244aSPoul-Henning Kamp.Ed
503863edaecSDag-Erling Smørgrav.Sh SEE ALSO
504863edaecSDag-Erling Smørgrav.Xr printf 3 ,
505863edaecSDag-Erling Smørgrav.Xr strcat 3 ,
506d68b700aSRuslan Ermilov.Xr strcpy 3 ,
507d68b700aSRuslan Ermilov.Xr copyin 9 ,
508e05f3134SAndrew R. Reiter.Xr copyinstr 9 ,
509e05f3134SAndrew R. Reiter.Xr printf 9
510863edaecSDag-Erling Smørgrav.Sh HISTORY
511863edaecSDag-Erling SmørgravThe
51281ae4b8dSRuslan Ermilov.Nm
513863edaecSDag-Erling Smørgravfamily of functions first appeared in
51422cbe5e3SDag-Erling Smørgrav.Fx 4.4 .
515863edaecSDag-Erling Smørgrav.Sh AUTHORS
516863edaecSDag-Erling Smørgrav.An -nosplit
517863edaecSDag-Erling SmørgravThe
51881ae4b8dSRuslan Ermilov.Nm
519863edaecSDag-Erling Smørgravfamily of functions was designed by
520863edaecSDag-Erling Smørgrav.An Poul-Henning Kamp Aq phk@FreeBSD.org
521863edaecSDag-Erling Smørgravand implemented by
52209e06539SDag-Erling Smørgrav.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
523e499b74dSDag-Erling SmørgravAdditional improvements were suggested by
524e499b74dSDag-Erling Smørgrav.An Justin T. Gibbs Aq gibbs@FreeBSD.org .
525371c0c84SKelly YanceyAuto-extend support added by
526371c0c84SKelly Yancey.An Kelly Yancey Aq kbyanc@FreeBSD.org .
5274351ba27SMatthew D FlemingDrain functionality added by
5284351ba27SMatthew D Fleming.An Matthew Fleming Aq mdf@FreeBSD.org .
529863edaecSDag-Erling Smørgrav.Pp
530863edaecSDag-Erling SmørgravThis manual page was written by
53109e06539SDag-Erling Smørgrav.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
532