xref: /freebsd/share/man/man9/sbuf.9 (revision d68b700a78ba6d4f1436aa6d21e47378a00a439b)
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
295f95f24bSRuslan Ermilov.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 ,
35b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcat ,
36b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcopyin ,
37b0def2b5SDag-Erling Smørgrav.Nm sbuf_bcpy ,
38863edaecSDag-Erling Smørgrav.Nm sbuf_cat ,
39b0def2b5SDag-Erling Smørgrav.Nm sbuf_copyin ,
40863edaecSDag-Erling Smørgrav.Nm sbuf_cpy ,
41863edaecSDag-Erling Smørgrav.Nm sbuf_printf ,
42863edaecSDag-Erling Smørgrav.Nm sbuf_putc ,
43e499b74dSDag-Erling Smørgrav.Nm sbuf_overflowed ,
44863edaecSDag-Erling Smørgrav.Nm sbuf_finish ,
45863edaecSDag-Erling Smørgrav.Nm sbuf_data ,
46863edaecSDag-Erling Smørgrav.Nm sbuf_len ,
47863edaecSDag-Erling Smørgrav.Nm sbuf_delete
48863edaecSDag-Erling Smørgrav.Nd safe string formatting
49863edaecSDag-Erling Smørgrav.Sh SYNOPSIS
502ba5bb1fSBruce Evans.Fd #include <sys/types.h>
51863edaecSDag-Erling Smørgrav.Fd #include <sys/sbuf.h>
52d6479358SDag-Erling Smørgrav.Ft struct sbuf *s
53e499b74dSDag-Erling Smørgrav.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags"
54e499b74dSDag-Erling Smørgrav.Ft void
55e499b74dSDag-Erling Smørgrav.Fn sbuf_clear "struct sbuf *s"
56863edaecSDag-Erling Smørgrav.Ft int
57e499b74dSDag-Erling Smørgrav.Fn sbuf_setpos "struct sbuf *s" "int pos"
58863edaecSDag-Erling Smørgrav.Ft int
59b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat "struct sbuf *s" "const char *str" "size_t len"
60b0def2b5SDag-Erling Smørgrav.Ft int
61b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin "struct sbuf *s" "const void *uaddr" "size_t len"
62b0def2b5SDag-Erling Smørgrav.Ft int
63b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy "struct sbuf *s" "const char *str" "size_t len"
64b0def2b5SDag-Erling Smørgrav.Ft int
653393f8daSKenneth D. Merry.Fn sbuf_cat "struct sbuf *s" "const char *str"
66863edaecSDag-Erling Smørgrav.Ft int
67b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin "struct sbuf *s" "const void *uaddr" "size_t len"
68b0def2b5SDag-Erling Smørgrav.Ft int
693393f8daSKenneth D. Merry.Fn sbuf_cpy "struct sbuf *s" "const char *str"
70863edaecSDag-Erling Smørgrav.Ft int
71863edaecSDag-Erling Smørgrav.Fn sbuf_printf "struct sbuf *s" "char *fmt" "..."
72863edaecSDag-Erling Smørgrav.Ft int
73863edaecSDag-Erling Smørgrav.Fn sbuf_putc "struct sbuf *s" "int c"
74863edaecSDag-Erling Smørgrav.Ft int
75e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed "struct sbuf *s"
76e499b74dSDag-Erling Smørgrav.Ft void
77863edaecSDag-Erling Smørgrav.Fn sbuf_finish "struct sbuf *s"
78863edaecSDag-Erling Smørgrav.Ft char *
79863edaecSDag-Erling Smørgrav.Fn sbuf_data "struct sbuf *s"
80e499b74dSDag-Erling Smørgrav.Ft int
81863edaecSDag-Erling Smørgrav.Fn sbuf_len "struct sbuf *s"
82863edaecSDag-Erling Smørgrav.Ft void
83863edaecSDag-Erling Smørgrav.Fn sbuf_delete "struct sbuf *s"
84863edaecSDag-Erling Smørgrav.Sh DESCRIPTION
85863edaecSDag-Erling SmørgravThe
86863edaecSDag-Erling Smørgrav.Nm sbuf
8767e35919SSheldon Hearnfamily of functions allows one to safely allocate, construct and
88863edaecSDag-Erling Smørgravrelease bounded null-terminated strings in kernel space.
89863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures
90863edaecSDag-Erling Smørgravcalled
91863edaecSDag-Erling Smørgrav.Fa sbufs ,
92863edaecSDag-Erling Smørgravdefined in
93863edaecSDag-Erling Smørgrav.Aq Pa sys/sbuf.h .
94863edaecSDag-Erling Smørgrav.Pp
95863edaecSDag-Erling SmørgravThe
96863edaecSDag-Erling Smørgrav.Fn sbuf_new
97863edaecSDag-Erling Smørgravfunction initializes the
98863edaecSDag-Erling Smørgrav.Fa sbuf
99863edaecSDag-Erling Smørgravpointed to by its first argument.
100d6479358SDag-Erling SmørgravIf that pointer is
101d6479358SDag-Erling Smørgrav.Dv NULL ,
102d6479358SDag-Erling Smørgrav.Fn sbuf_new
103d6479358SDag-Erling Smørgravallocates a
104d68b700aSRuslan Ermilov.Vt struct sbuf
105d6479358SDag-Erling Smørgravusing
106d6479358SDag-Erling Smørgrav.Xr malloc 9 .
107863edaecSDag-Erling SmørgravThe
108863edaecSDag-Erling Smørgrav.Fa buf
109863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string;
110863edaecSDag-Erling Smørgravif it is
111863edaecSDag-Erling Smørgrav.Dv NULL ,
112863edaecSDag-Erling Smørgrav.Fn sbuf_new
113863edaecSDag-Erling Smørgravwill allocate one using
114863edaecSDag-Erling Smørgrav.Xr malloc 9 .
115863edaecSDag-Erling SmørgravThe
116863edaecSDag-Erling Smørgrav.Fa length
117863edaecSDag-Erling Smørgravis the intended size of the storage buffer.
118863edaecSDag-Erling SmørgravThe fourth argument,
119863edaecSDag-Erling Smørgrav.Fa flags ,
120863edaecSDag-Erling Smørgravis currently unused and should always be set to zero.
121863edaecSDag-Erling Smørgrav.Pp
122863edaecSDag-Erling SmørgravNote that if
123863edaecSDag-Erling Smørgrav.Fa buf
124863edaecSDag-Erling Smørgravis not
125863edaecSDag-Erling Smørgrav.Dv NULL ,
126863edaecSDag-Erling Smørgravit must point to an array of at least
127863edaecSDag-Erling Smørgrav.Fa length
128863edaecSDag-Erling Smørgravcharacters.
129863edaecSDag-Erling Smørgrav.Pp
130863edaecSDag-Erling SmørgravThe
131e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
132e499b74dSDag-Erling Smørgravfunction invalidates the contents of the
133e499b74dSDag-Erling Smørgrav.Fa sbuf
134e499b74dSDag-Erling Smørgravand resets its position to zero.
135e499b74dSDag-Erling Smørgrav.Pp
136e499b74dSDag-Erling SmørgravThe
137863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
138863edaecSDag-Erling Smørgravfunction sets the
139863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's
140863edaecSDag-Erling Smørgravcurrent position to
141863edaecSDag-Erling Smørgrav.Fa pos ,
142863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the
143863edaecSDag-Erling Smørgravstorage buffer.
144863edaecSDag-Erling Smørgrav.Pp
145863edaecSDag-Erling SmørgravThe
146b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcat
147b0def2b5SDag-Erling Smørgravfunction appends the first
148b0def2b5SDag-Erling Smørgrav.Fa len
149b0def2b5SDag-Erling Smørgravbytes from the byte string
150b0def2b5SDag-Erling Smørgrav.Fa str
151b0def2b5SDag-Erling Smørgravto the
152b0def2b5SDag-Erling Smørgrav.Fa sbuf .
153b0def2b5SDag-Erling Smørgrav.Pp
154b0def2b5SDag-Erling SmørgravThe
155b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcopyin
156b0def2b5SDag-Erling Smørgravfunction copies
157b0def2b5SDag-Erling Smørgrav.Fa len
158b0def2b5SDag-Erling Smørgravbytes from the specified userland address into the
159b0def2b5SDag-Erling Smørgrav.Fa sbuf .
160b0def2b5SDag-Erling Smørgrav.Pp
161b0def2b5SDag-Erling SmørgravThe
162b0def2b5SDag-Erling Smørgrav.Fn sbuf_bcpy
163b0def2b5SDag-Erling Smørgravfunction replaces the contents of the
164b0def2b5SDag-Erling Smørgrav.Fa sbuf
165b0def2b5SDag-Erling Smørgravwith the first
166b0def2b5SDag-Erling Smørgrav.Fa len
167b0def2b5SDag-Erling Smørgravbytes from the byte string
168b0def2b5SDag-Erling Smørgrav.Fa str .
169b0def2b5SDag-Erling Smørgrav.Pp
170b0def2b5SDag-Erling SmørgravThe
171863edaecSDag-Erling Smørgrav.Fn sbuf_cat
172b0def2b5SDag-Erling Smørgravfunction appends the NUL-terminated string
173863edaecSDag-Erling Smørgrav.Fa str
174863edaecSDag-Erling Smørgravto the
175863edaecSDag-Erling Smørgrav.Fa sbuf
176863edaecSDag-Erling Smørgravat the current position.
177863edaecSDag-Erling Smørgrav.Pp
178863edaecSDag-Erling SmørgravThe
179b0def2b5SDag-Erling Smørgrav.Fn sbuf_copyin
180b0def2b5SDag-Erling Smørgravfunction copies a NUL-terminated string from the specified userland
181b0def2b5SDag-Erling Smørgravaddress into the
182b0def2b5SDag-Erling Smørgrav.Fa sbuf .
183b0def2b5SDag-Erling SmørgravIf the
184b0def2b5SDag-Erling Smørgrav.Fa len
185b0def2b5SDag-Erling Smørgravargument is non-zero, no more than
186b0def2b5SDag-Erling Smørgrav.Fa len
187b0def2b5SDag-Erling Smørgravcharacters (not counting the terminating NUL) are copied; otherwise
188b0def2b5SDag-Erling Smørgravthe entire string, or as much of it as can fit in the
189b0def2b5SDag-Erling Smørgrav.Fa sbuf ,
190b0def2b5SDag-Erling Smørgravis copied.
191b0def2b5SDag-Erling Smørgrav.Pp
192b0def2b5SDag-Erling SmørgravThe
193863edaecSDag-Erling Smørgrav.Fn sbuf_cpy
194863edaecSDag-Erling Smørgravfunction replaces the contents of the
195863edaecSDag-Erling Smørgrav.Fa sbuf
196b0def2b5SDag-Erling Smørgravwith those of the NUL-terminated string
197863edaecSDag-Erling Smørgrav.Fa str .
198863edaecSDag-Erling SmørgravThis is equivalent to calling
199863edaecSDag-Erling Smørgrav.Fn sbuf_cat
200863edaecSDag-Erling Smørgravwith a fresh
201863edaecSDag-Erling Smørgrav.Fa sbuf
202863edaecSDag-Erling Smørgravor one which position has been reset to zero with
203e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
204e499b74dSDag-Erling Smørgravor
205863edaecSDag-Erling Smørgrav.Fn sbuf_setpos .
206863edaecSDag-Erling Smørgrav.Pp
207863edaecSDag-Erling SmørgravThe
208863edaecSDag-Erling Smørgrav.Fn sbuf_printf
209863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed
210863edaecSDag-Erling Smørgravto by
211863edaecSDag-Erling Smørgrav.Fa fmt
212863edaecSDag-Erling Smørgravand appends the resulting string to the
213863edaecSDag-Erling Smørgrav.Fa sbuf
214863edaecSDag-Erling Smørgravat the current position.
215863edaecSDag-Erling Smørgrav.Pp
216863edaecSDag-Erling SmørgravThe
217863edaecSDag-Erling Smørgrav.Fn sbuf_putc
218863edaecSDag-Erling Smørgravfunction appends the character
219863edaecSDag-Erling Smørgrav.Fa c
220863edaecSDag-Erling Smørgravto the
221863edaecSDag-Erling Smørgrav.Fa sbuf
222863edaecSDag-Erling Smørgravat the current position.
223863edaecSDag-Erling Smørgrav.Pp
224863edaecSDag-Erling SmørgravThe
225e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed
226e499b74dSDag-Erling Smørgravfunction returns a non-zero value if the
227e499b74dSDag-Erling Smørgrav.Fa sbuf
228e499b74dSDag-Erling Smørgravoverflowed.
229e499b74dSDag-Erling Smørgrav.Pp
230e499b74dSDag-Erling SmørgravThe
231863edaecSDag-Erling Smørgrav.Fn sbuf_finish
232863edaecSDag-Erling Smørgravfunction null-terminates the
233863edaecSDag-Erling Smørgrav.Fa sbuf
234863edaecSDag-Erling Smørgravand marks it as finished, which means that it may no longer be
235863edaecSDag-Erling Smørgravmodified using
236863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
237863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
2383393f8daSKenneth D. Merry.Fn sbuf_cpy ,
239863edaecSDag-Erling Smørgrav.Fn sbuf_printf
240863edaecSDag-Erling Smørgravor
241863edaecSDag-Erling Smørgrav.Fn sbuf_putc .
242863edaecSDag-Erling Smørgrav.Pp
243863edaecSDag-Erling SmørgravThe
244863edaecSDag-Erling Smørgrav.Fn sbuf_data
245863edaecSDag-Erling Smørgravand
246863edaecSDag-Erling Smørgrav.Fn sbuf_len
247e499b74dSDag-Erling Smørgravfunctions return the actual string and its length, respectively;
248e499b74dSDag-Erling Smørgrav.Fn sbuf_data
249e499b74dSDag-Erling Smørgravonly works on a finished
250863edaecSDag-Erling Smørgrav.Fa sbuf .
251863edaecSDag-Erling Smørgrav.Pp
252863edaecSDag-Erling SmørgravFinally, the
253863edaecSDag-Erling Smørgrav.Fn sbuf_delete
254863edaecSDag-Erling Smørgravfunction clears the
255863edaecSDag-Erling Smørgrav.Fa sbuf
256863edaecSDag-Erling Smørgravand frees its storage buffer if it was allocated by
257863edaecSDag-Erling Smørgrav.Fn sbuf_new .
258863edaecSDag-Erling Smørgrav.Sh NOTES
259863edaecSDag-Erling SmørgravIf an operation caused an
260863edaecSDag-Erling Smørgrav.Fa sbuf
261e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the
262e499b74dSDag-Erling Smørgrav.Fa sbuf
263e499b74dSDag-Erling Smørgravis finished using
264e499b74dSDag-Erling Smørgrav.Fn sbuf_finish
265e499b74dSDag-Erling Smørgravor reset using
266e499b74dSDag-Erling Smørgrav.Fn sbuf_clear ,
267e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the
268e499b74dSDag-Erling Smørgravsize of its storage buffer using
269863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
270863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using
271863edaecSDag-Erling Smørgrav.Fn sbuf_cpy .
272863edaecSDag-Erling Smørgrav.Sh RETURN VALUES
273863edaecSDag-Erling Smørgrav.Fn sbuf_new
274d6479358SDag-Erling Smørgravreturns
275d6479358SDag-Erling Smørgrav.Dv NULL
276d6479358SDag-Erling Smørgravif it failed to allocate a storage buffer, and a pointer to the new
277d6479358SDag-Erling Smørgrav.Fa sbuf
278863edaecSDag-Erling Smørgravotherwise.
279863edaecSDag-Erling Smørgrav.Pp
280863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
281863edaecSDag-Erling Smørgravreturns \-1 if
282863edaecSDag-Erling Smørgrav.Fa pos
283863edaecSDag-Erling Smørgravwas invalid, and zero otherwise.
284863edaecSDag-Erling Smørgrav.Pp
285863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
286863edaecSDag-Erling Smørgrav.Fn sbuf_cpy ,
287863edaecSDag-Erling Smørgrav.Fn sbuf_printf ,
288863edaecSDag-Erling Smørgravand
289e499b74dSDag-Erling Smørgrav.Fn sbuf_putc
290863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise.
291863edaecSDag-Erling Smørgrav.Pp
292e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed
293e499b74dSDag-Erling Smørgravreturns a non-zero value if the buffer overflowed, and zero otherwise.
294e499b74dSDag-Erling Smørgrav.Pp
295863edaecSDag-Erling Smørgrav.Fn sbuf_data
296863edaecSDag-Erling Smørgravand
297863edaecSDag-Erling Smørgrav.Fn sbuf_len
298863edaecSDag-Erling Smørgravreturn
299863edaecSDag-Erling Smørgrav.Dv NULL
300e499b74dSDag-Erling Smørgravand \-1, respectively, if the buffer overflowed.
301863edaecSDag-Erling Smørgrav.Sh SEE ALSO
302863edaecSDag-Erling Smørgrav.Xr printf 3 ,
303863edaecSDag-Erling Smørgrav.Xr strcat 3 ,
304d68b700aSRuslan Ermilov.Xr strcpy 3 ,
305d68b700aSRuslan Ermilov.Xr copyin 9 ,
306d68b700aSRuslan Ermilov.Xr copyinstr 9
307863edaecSDag-Erling Smørgrav.Sh HISTORY
308863edaecSDag-Erling SmørgravThe
309863edaecSDag-Erling Smørgrav.Nm sbuf
310863edaecSDag-Erling Smørgravfamily of functions first appeared in
3113a3799c9SRuslan Ermilov.Fx 5.0 .
312863edaecSDag-Erling Smørgrav.Sh AUTHORS
313863edaecSDag-Erling Smørgrav.An -nosplit
314863edaecSDag-Erling SmørgravThe
315863edaecSDag-Erling Smørgrav.Nm sbuf
316863edaecSDag-Erling Smørgravfamily of functions was designed by
317863edaecSDag-Erling Smørgrav.An Poul-Henning Kamp Aq phk@FreeBSD.org
318863edaecSDag-Erling Smørgravand implemented by
319863edaecSDag-Erling Smørgrav.An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org .
320e499b74dSDag-Erling SmørgravAdditional improvements were suggested by
321e499b74dSDag-Erling Smørgrav.An Justin T. Gibbs Aq gibbs@FreeBSD.org .
322863edaecSDag-Erling Smørgrav.Pp
323863edaecSDag-Erling SmørgravThis manual page was written by
324863edaecSDag-Erling Smørgrav.An Dag-Erling Co\(:idan Sm\(/orgrav .
325