1.\"- 2.\" Copyright (c) 2000 Poul Henning Kamp and Dag-Erling Co�dan Sm�rgrav 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.\" $FreeBSD$ 27.\" 28.Dd January 28, 2001 29.Dt SBUF 9 30.Os FreeBSD 31.Sh NAME 32.Nm sbuf_new , 33.Nm sbuf_clear , 34.Nm sbuf_setpos , 35.Nm sbuf_cat , 36.Nm sbuf_cpy , 37.Nm sbuf_printf , 38.Nm sbuf_putc , 39.Nm sbuf_overflowed , 40.Nm sbuf_finish , 41.Nm sbuf_data , 42.Nm sbuf_len , 43.Nm sbuf_delete 44.Nd safe string formatting 45.Sh SYNOPSIS 46.Fd #include <sys/types.h> 47.Fd #include <sys/sbuf.h> 48.Ft int 49.Fn sbuf_new "struct sbuf *s" "char *buf" "int length" "int flags" 50.Ft void 51.Fn sbuf_clear "struct sbuf *s" 52.Ft int 53.Fn sbuf_setpos "struct sbuf *s" "int pos" 54.Ft int 55.Fn sbuf_cat "struct sbuf *s" "const char *str" 56.Ft int 57.Fn sbuf_cpy "struct sbuf *s" "const char *str" 58.Ft int 59.Fn sbuf_printf "struct sbuf *s" "char *fmt" "..." 60.Ft int 61.Fn sbuf_putc "struct sbuf *s" "int c" 62.Ft int 63.Fn sbuf_overflowed "struct sbuf *s" 64.Ft void 65.Fn sbuf_finish "struct sbuf *s" 66.Ft char * 67.Fn sbuf_data "struct sbuf *s" 68.Ft int 69.Fn sbuf_len "struct sbuf *s" 70.Ft void 71.Fn sbuf_delete "struct sbuf *s" 72.Sh DESCRIPTION 73The 74.Nm sbuf 75family of functions allows one to safely allocate, construct and 76release bounded null-terminated strings in kernel space. 77Instead of arrays of characters, these functions operate on structures 78called 79.Fa sbufs , 80defined in 81.Aq Pa sys/sbuf.h . 82.Pp 83The 84.Fn sbuf_new 85function initializes the 86.Fa sbuf 87pointed to by its first argument. 88The 89.Fa buf 90argument is a pointer to a buffer in which to store the actual string; 91if it is 92.Dv NULL , 93.Fn sbuf_new 94will allocate one using 95.Xr malloc 9 . 96The 97.Fa length 98is the intended size of the storage buffer. 99The fourth argument, 100.Fa flags , 101is currently unused and should always be set to zero. 102.Pp 103Note that if 104.Fa buf 105is not 106.Dv NULL , 107it must point to an array of at least 108.Fa length 109characters. 110.Pp 111The 112.Fn sbuf_clear 113function invalidates the contents of the 114.Fa sbuf 115and resets its position to zero. 116.Pp 117The 118.Fn sbuf_setpos 119function sets the 120.Fa sbuf Ns 's 121current position to 122.Fa pos , 123which is a value between zero and one less than the size of the 124storage buffer. 125.Pp 126The 127.Fn sbuf_cat 128function appends the string 129.Fa str 130to the 131.Fa sbuf 132at the current position. 133.Pp 134The 135.Fn sbuf_cpy 136function replaces the contents of the 137.Fa sbuf 138with those of the string 139.Fa str . 140This is equivalent to calling 141.Fn sbuf_cat 142with a fresh 143.Fa sbuf 144or one which position has been reset to zero with 145.Fn sbuf_clear 146or 147.Fn sbuf_setpos . 148.Pp 149The 150.Fn sbuf_printf 151function formats its arguments according to the format string pointed 152to by 153.Fa fmt 154and appends the resulting string to the 155.Fa sbuf 156at the current position. 157.Pp 158The 159.Fn sbuf_putc 160function appends the character 161.Fa c 162to the 163.Fa sbuf 164at the current position. 165.Pp 166The 167.Fn sbuf_overflowed 168function returns a non-zero value if the 169.Fa sbuf 170overflowed. 171.Pp 172The 173.Fn sbuf_finish 174function null-terminates the 175.Fa sbuf 176and marks it as finished, which means that it may no longer be 177modified using 178.Fn sbuf_setpos , 179.Fn sbuf_cat , 180.Fn sbuf_cpy , 181.Fn sbuf_printf 182or 183.Fn sbuf_putc . 184.Pp 185The 186.Fn sbuf_data 187and 188.Fn sbuf_len 189functions return the actual string and its length, respectively; 190.Fn sbuf_data 191only works on a finished 192.Fa sbuf . 193.Pp 194Finally, the 195.Fn sbuf_delete 196function clears the 197.Fa sbuf 198and frees its storage buffer if it was allocated by 199.Fn sbuf_new . 200.Sh NOTES 201If an operation caused an 202.Fa sbuf 203to overflow, most subsequent operations on it will fail until the 204.Fa sbuf 205is finished using 206.Fn sbuf_finish 207or reset using 208.Fn sbuf_clear , 209or its position is reset to a value between 0 and one less than the 210size of its storage buffer using 211.Fn sbuf_setpos , 212or it is reinitialized to a sufficiently short string using 213.Fn sbuf_cpy . 214.Sh RETURN VALUES 215.Fn sbuf_new 216returns \-1 if it failed to allocate a storage buffer, and zero 217otherwise. 218.Pp 219.Fn sbuf_setpos 220returns \-1 if 221.Fa pos 222was invalid, and zero otherwise. 223.Pp 224.Fn sbuf_cat , 225.Fn sbuf_cpy , 226.Fn sbuf_printf , 227and 228.Fn sbuf_putc 229all return \-1 if the buffer overflowed, and zero otherwise. 230.Pp 231.Fn sbuf_overflowed 232returns a non-zero value if the buffer overflowed, and zero otherwise. 233.Pp 234.Fn sbuf_data 235and 236.Fn sbuf_len 237return 238.Dv NULL 239and \-1, respectively, if the buffer overflowed. 240.Sh SEE ALSO 241.Xr printf 3 , 242.Xr strcat 3 , 243.Xr strcpy 3 244.Sh HISTORY 245The 246.Nm sbuf 247family of functions first appeared in 248.Fx 5.0 . 249.Sh AUTHORS 250.An -nosplit 251The 252.Nm sbuf 253family of functions was designed by 254.An Poul-Henning Kamp Aq phk@FreeBSD.org 255and implemented by 256.An Dag-Erling Co\(:idan Sm\(/orgrav Aq des@FreeBSD.org . 257Additional improvements were suggested by 258.An Justin T. Gibbs Aq gibbs@FreeBSD.org . 259.Pp 260This manual page was written by 261.An Dag-Erling Co\(:idan Sm\(/orgrav . 262