xref: /freebsd/share/man/man9/sbuf.9 (revision 3393f8daa3b4b18786cd585c1a37aa16dd03d410)
1863edaecSDag-Erling Smørgrav.\"-
2863edaecSDag-Erling Smørgrav.\" Copyright (c) 2000 Poul Henning Kamp and Dag-Erling Co�dan Sm�rgrav
3863edaecSDag-Erling Smørgrav.\" All rights reserved.
4863edaecSDag-Erling Smørgrav.\"
5863edaecSDag-Erling Smørgrav.\" Redistribution and use in source and binary forms, with or without
6863edaecSDag-Erling Smørgrav.\" modification, are permitted provided that the following conditions
7863edaecSDag-Erling Smørgrav.\" are met:
8863edaecSDag-Erling Smørgrav.\" 1. Redistributions of source code must retain the above copyright
9863edaecSDag-Erling Smørgrav.\"    notice, this list of conditions and the following disclaimer.
10863edaecSDag-Erling Smørgrav.\" 2. Redistributions in binary form must reproduce the above copyright
11863edaecSDag-Erling Smørgrav.\"    notice, this list of conditions and the following disclaimer in the
12863edaecSDag-Erling Smørgrav.\"    documentation and/or other materials provided with the distribution.
13863edaecSDag-Erling Smørgrav.\"
14863edaecSDag-Erling Smørgrav.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15863edaecSDag-Erling Smørgrav.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16863edaecSDag-Erling Smørgrav.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17863edaecSDag-Erling Smørgrav.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18863edaecSDag-Erling Smørgrav.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19863edaecSDag-Erling Smørgrav.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20863edaecSDag-Erling Smørgrav.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21863edaecSDag-Erling Smørgrav.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22863edaecSDag-Erling Smørgrav.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23863edaecSDag-Erling Smørgrav.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24863edaecSDag-Erling Smørgrav.\" SUCH DAMAGE.
25863edaecSDag-Erling Smørgrav.\"
26863edaecSDag-Erling Smørgrav.\" $FreeBSD$
27863edaecSDag-Erling Smørgrav.\"
28e499b74dSDag-Erling Smørgrav.Dd January 28, 2001
29863edaecSDag-Erling Smørgrav.Dt sbuf 9
30863edaecSDag-Erling Smørgrav.Os FreeBSD
31863edaecSDag-Erling Smørgrav.Sh NAME
32863edaecSDag-Erling Smørgrav.Nm sbuf_new ,
33e499b74dSDag-Erling Smørgrav.Nm sbuf_clear ,
34863edaecSDag-Erling Smørgrav.Nm sbuf_setpos ,
35863edaecSDag-Erling Smørgrav.Nm sbuf_cat ,
36863edaecSDag-Erling Smørgrav.Nm sbuf_cpy ,
37863edaecSDag-Erling Smørgrav.Nm sbuf_printf ,
38863edaecSDag-Erling Smørgrav.Nm sbuf_putc ,
39e499b74dSDag-Erling Smørgrav.Nm sbuf_overflowed ,
40863edaecSDag-Erling Smørgrav.Nm sbuf_finish ,
41863edaecSDag-Erling Smørgrav.Nm sbuf_data ,
42863edaecSDag-Erling Smørgrav.Nm sbuf_len ,
43863edaecSDag-Erling Smørgrav.Nm sbuf_delete
44863edaecSDag-Erling Smørgrav.Nd safe string formatting
45863edaecSDag-Erling Smørgrav.Sh SYNOPSIS
462ba5bb1fSBruce Evans.Fd #include <sys/types.h>
47863edaecSDag-Erling Smørgrav.Fd #include <sys/sbuf.h>
48863edaecSDag-Erling Smørgrav.Ft int
49e499b74dSDag-Erling Smørgrav.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags"
50e499b74dSDag-Erling Smørgrav.Ft void
51e499b74dSDag-Erling Smørgrav.Fn sbuf_clear "struct sbuf *s"
52863edaecSDag-Erling Smørgrav.Ft int
53e499b74dSDag-Erling Smørgrav.Fn sbuf_setpos "struct sbuf *s" "int pos"
54863edaecSDag-Erling Smørgrav.Ft int
553393f8daSKenneth D. Merry.Fn sbuf_cat "struct sbuf *s" "const char *str"
56863edaecSDag-Erling Smørgrav.Ft int
573393f8daSKenneth D. Merry.Fn sbuf_cpy "struct sbuf *s" "const char *str"
58863edaecSDag-Erling Smørgrav.Ft int
59863edaecSDag-Erling Smørgrav.Fn sbuf_printf "struct sbuf *s" "char *fmt" "..."
60863edaecSDag-Erling Smørgrav.Ft int
61863edaecSDag-Erling Smørgrav.Fn sbuf_putc "struct sbuf *s" "int c"
62863edaecSDag-Erling Smørgrav.Ft int
63e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed "struct sbuf *s"
64e499b74dSDag-Erling Smørgrav.Ft void
65863edaecSDag-Erling Smørgrav.Fn sbuf_finish "struct sbuf *s"
66863edaecSDag-Erling Smørgrav.Ft char *
67863edaecSDag-Erling Smørgrav.Fn sbuf_data "struct sbuf *s"
68e499b74dSDag-Erling Smørgrav.Ft int
69863edaecSDag-Erling Smørgrav.Fn sbuf_len "struct sbuf *s"
70863edaecSDag-Erling Smørgrav.Ft void
71863edaecSDag-Erling Smørgrav.Fn sbuf_delete "struct sbuf *s"
72863edaecSDag-Erling Smørgrav.Sh DESCRIPTION
73863edaecSDag-Erling SmørgravThe
74863edaecSDag-Erling Smørgrav.Nm sbuf
7567e35919SSheldon Hearnfamily of functions allows one to safely allocate, construct and
76863edaecSDag-Erling Smørgravrelease bounded null-terminated strings in kernel space.
77863edaecSDag-Erling SmørgravInstead of arrays of characters, these functions operate on structures
78863edaecSDag-Erling Smørgravcalled
79863edaecSDag-Erling Smørgrav.Fa sbufs ,
80863edaecSDag-Erling Smørgravdefined in
81863edaecSDag-Erling Smørgrav.Aq Pa sys/sbuf.h .
82863edaecSDag-Erling Smørgrav.Pp
83863edaecSDag-Erling SmørgravThe
84863edaecSDag-Erling Smørgrav.Fn sbuf_new
85863edaecSDag-Erling Smørgravfunction initializes the
86863edaecSDag-Erling Smørgrav.Fa sbuf
87863edaecSDag-Erling Smørgravpointed to by its first argument.
88863edaecSDag-Erling SmørgravThe
89863edaecSDag-Erling Smørgrav.Fa buf
90863edaecSDag-Erling Smørgravargument is a pointer to a buffer in which to store the actual string;
91863edaecSDag-Erling Smørgravif it is
92863edaecSDag-Erling Smørgrav.Dv NULL ,
93863edaecSDag-Erling Smørgrav.Fn sbuf_new
94863edaecSDag-Erling Smørgravwill allocate one using
95863edaecSDag-Erling Smørgrav.Xr malloc 9 .
96863edaecSDag-Erling SmørgravThe
97863edaecSDag-Erling Smørgrav.Fa length
98863edaecSDag-Erling Smørgravis the intended size of the storage buffer.
99863edaecSDag-Erling SmørgravThe fourth argument,
100863edaecSDag-Erling Smørgrav.Fa flags ,
101863edaecSDag-Erling Smørgravis currently unused and should always be set to zero.
102863edaecSDag-Erling Smørgrav.Pp
103863edaecSDag-Erling SmørgravNote that if
104863edaecSDag-Erling Smørgrav.Fa buf
105863edaecSDag-Erling Smørgravis not
106863edaecSDag-Erling Smørgrav.Dv NULL ,
107863edaecSDag-Erling Smørgravit must point to an array of at least
108863edaecSDag-Erling Smørgrav.Fa length
109863edaecSDag-Erling Smørgravcharacters.
110863edaecSDag-Erling Smørgrav.Pp
111863edaecSDag-Erling SmørgravThe
112e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
113e499b74dSDag-Erling Smørgravfunction invalidates the contents of the
114e499b74dSDag-Erling Smørgrav.Fa sbuf
115e499b74dSDag-Erling Smørgravand resets its position to zero.
116e499b74dSDag-Erling Smørgrav.Pp
117e499b74dSDag-Erling SmørgravThe
118863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
119863edaecSDag-Erling Smørgravfunction sets the
120863edaecSDag-Erling Smørgrav.Fa sbuf Ns 's
121863edaecSDag-Erling Smørgravcurrent position to
122863edaecSDag-Erling Smørgrav.Fa pos ,
123863edaecSDag-Erling Smørgravwhich is a value between zero and one less than the size of the
124863edaecSDag-Erling Smørgravstorage buffer.
125863edaecSDag-Erling Smørgrav.Pp
126863edaecSDag-Erling SmørgravThe
127863edaecSDag-Erling Smørgrav.Fn sbuf_cat
128863edaecSDag-Erling Smørgravfunction appends the string
129863edaecSDag-Erling Smørgrav.Fa str
130863edaecSDag-Erling Smørgravto the
131863edaecSDag-Erling Smørgrav.Fa sbuf
132863edaecSDag-Erling Smørgravat the current position.
133863edaecSDag-Erling Smørgrav.Pp
134863edaecSDag-Erling SmørgravThe
135863edaecSDag-Erling Smørgrav.Fn sbuf_cpy
136863edaecSDag-Erling Smørgravfunction replaces the contents of the
137863edaecSDag-Erling Smørgrav.Fa sbuf
138863edaecSDag-Erling Smørgravwith those of the string
139863edaecSDag-Erling Smørgrav.Fa str .
140863edaecSDag-Erling SmørgravThis is equivalent to calling
141863edaecSDag-Erling Smørgrav.Fn sbuf_cat
142863edaecSDag-Erling Smørgravwith a fresh
143863edaecSDag-Erling Smørgrav.Fa sbuf
144863edaecSDag-Erling Smørgravor one which position has been reset to zero with
145e499b74dSDag-Erling Smørgrav.Fn sbuf_clear
146e499b74dSDag-Erling Smørgravor
147863edaecSDag-Erling Smørgrav.Fn sbuf_setpos .
148863edaecSDag-Erling Smørgrav.Pp
149863edaecSDag-Erling SmørgravThe
150863edaecSDag-Erling Smørgrav.Fn sbuf_printf
151863edaecSDag-Erling Smørgravfunction formats its arguments according to the format string pointed
152863edaecSDag-Erling Smørgravto by
153863edaecSDag-Erling Smørgrav.Fa fmt
154863edaecSDag-Erling Smørgravand appends the resulting string to the
155863edaecSDag-Erling Smørgrav.Fa sbuf
156863edaecSDag-Erling Smørgravat the current position.
157863edaecSDag-Erling Smørgrav.Pp
158863edaecSDag-Erling SmørgravThe
159863edaecSDag-Erling Smørgrav.Fn sbuf_putc
160863edaecSDag-Erling Smørgravfunction appends the character
161863edaecSDag-Erling Smørgrav.Fa c
162863edaecSDag-Erling Smørgravto the
163863edaecSDag-Erling Smørgrav.Fa sbuf
164863edaecSDag-Erling Smørgravat the current position.
165863edaecSDag-Erling Smørgrav.Pp
166863edaecSDag-Erling SmørgravThe
167e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed
168e499b74dSDag-Erling Smørgravfunction returns a non-zero value if the
169e499b74dSDag-Erling Smørgrav.Fa sbuf
170e499b74dSDag-Erling Smørgravoverflowed.
171e499b74dSDag-Erling Smørgrav.Pp
172e499b74dSDag-Erling SmørgravThe
173863edaecSDag-Erling Smørgrav.Fn sbuf_finish
174863edaecSDag-Erling Smørgravfunction null-terminates the
175863edaecSDag-Erling Smørgrav.Fa sbuf
176863edaecSDag-Erling Smørgravand marks it as finished, which means that it may no longer be
177863edaecSDag-Erling Smørgravmodified using
178863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
179863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
1803393f8daSKenneth D. Merry.Fn sbuf_cpy ,
181863edaecSDag-Erling Smørgrav.Fn sbuf_printf
182863edaecSDag-Erling Smørgravor
183863edaecSDag-Erling Smørgrav.Fn sbuf_putc .
184863edaecSDag-Erling Smørgrav.Pp
185863edaecSDag-Erling SmørgravThe
186863edaecSDag-Erling Smørgrav.Fn sbuf_data
187863edaecSDag-Erling Smørgravand
188863edaecSDag-Erling Smørgrav.Fn sbuf_len
189e499b74dSDag-Erling Smørgravfunctions return the actual string and its length, respectively;
190e499b74dSDag-Erling Smørgrav.Fn sbuf_data
191e499b74dSDag-Erling Smørgravonly works on a finished
192863edaecSDag-Erling Smørgrav.Fa sbuf .
193863edaecSDag-Erling Smørgrav.Pp
194863edaecSDag-Erling SmørgravFinally, the
195863edaecSDag-Erling Smørgrav.Fn sbuf_delete
196863edaecSDag-Erling Smørgravfunction clears the
197863edaecSDag-Erling Smørgrav.Fa sbuf
198863edaecSDag-Erling Smørgravand frees its storage buffer if it was allocated by
199863edaecSDag-Erling Smørgrav.Fn sbuf_new .
200863edaecSDag-Erling Smørgrav.Sh NOTES
201863edaecSDag-Erling SmørgravIf an operation caused an
202863edaecSDag-Erling Smørgrav.Fa sbuf
203e499b74dSDag-Erling Smørgravto overflow, most subsequent operations on it will fail until the
204e499b74dSDag-Erling Smørgrav.Fa sbuf
205e499b74dSDag-Erling Smørgravis finished using
206e499b74dSDag-Erling Smørgrav.Fn sbuf_finish
207e499b74dSDag-Erling Smørgravor reset using
208e499b74dSDag-Erling Smørgrav.Fn sbuf_clear ,
209e499b74dSDag-Erling Smørgravor its position is reset to a value between 0 and one less than the
210e499b74dSDag-Erling Smørgravsize of its storage buffer using
211863edaecSDag-Erling Smørgrav.Fn sbuf_setpos ,
212863edaecSDag-Erling Smørgravor it is reinitialized to a sufficiently short string using
213863edaecSDag-Erling Smørgrav.Fn sbuf_cpy .
214863edaecSDag-Erling Smørgrav.Sh RETURN VALUES
215863edaecSDag-Erling Smørgrav.Fn sbuf_new
216863edaecSDag-Erling Smørgravreturns \-1 if it failed to allocate a storage buffer, and zero
217863edaecSDag-Erling Smørgravotherwise.
218863edaecSDag-Erling Smørgrav.Pp
219863edaecSDag-Erling Smørgrav.Fn sbuf_setpos
220863edaecSDag-Erling Smørgravreturns \-1 if
221863edaecSDag-Erling Smørgrav.Fa pos
222863edaecSDag-Erling Smørgravwas invalid, and zero otherwise.
223863edaecSDag-Erling Smørgrav.Pp
224863edaecSDag-Erling Smørgrav.Fn sbuf_cat ,
225863edaecSDag-Erling Smørgrav.Fn sbuf_cpy ,
226863edaecSDag-Erling Smørgrav.Fn sbuf_printf ,
227863edaecSDag-Erling Smørgravand
228e499b74dSDag-Erling Smørgrav.Fn sbuf_putc
229863edaecSDag-Erling Smørgravall return \-1 if the buffer overflowed, and zero otherwise.
230863edaecSDag-Erling Smørgrav.Pp
231e499b74dSDag-Erling Smørgrav.Fn sbuf_overflowed
232e499b74dSDag-Erling Smørgravreturns a non-zero value if the buffer overflowed, and zero otherwise.
233e499b74dSDag-Erling Smørgrav.Pp
234863edaecSDag-Erling Smørgrav.Fn sbuf_data
235863edaecSDag-Erling Smørgravand
236863edaecSDag-Erling Smørgrav.Fn sbuf_len
237863edaecSDag-Erling Smørgravreturn
238863edaecSDag-Erling Smørgrav.Dv NULL
239e499b74dSDag-Erling Smørgravand \-1, respectively, if the buffer overflowed.
240863edaecSDag-Erling Smørgrav.Sh SEE ALSO
241863edaecSDag-Erling Smørgrav.Xr printf 3 ,
242863edaecSDag-Erling Smørgrav.Xr strcat 3 ,
243863edaecSDag-Erling Smørgrav.Xr strcpy 3
244863edaecSDag-Erling Smørgrav.Sh HISTORY
245863edaecSDag-Erling SmørgravThe
246863edaecSDag-Erling Smørgrav.Nm sbuf
247863edaecSDag-Erling Smørgravfamily of functions first appeared in
248863edaecSDag-Erling Smørgrav.Fx 4.3 .
249863edaecSDag-Erling Smørgrav.Sh AUTHORS
250863edaecSDag-Erling Smørgrav.An -nosplit
251863edaecSDag-Erling SmørgravThe
252863edaecSDag-Erling Smørgrav.Nm sbuf
253863edaecSDag-Erling Smørgravfamily of functions was designed by
254863edaecSDag-Erling Smørgrav.An Poul-Henning Kamp Aq phk@FreeBSD.org
255863edaecSDag-Erling Smørgravand implemented by
256863edaecSDag-Erling Smørgrav.An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org .
257e499b74dSDag-Erling SmørgravAdditional improvements were suggested by
258e499b74dSDag-Erling Smørgrav.An Justin T. Gibbs Aq gibbs@FreeBSD.org .
259863edaecSDag-Erling Smørgrav.Pp
260863edaecSDag-Erling SmørgravThis manual page was written by
261863edaecSDag-Erling Smørgrav.An Dag-Erling Co\(:idan Sm\(/orgrav .
262