subr_sbuf.c revision 111119
169990Sdes/*-
269990Sdes * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Co�dan Sm�rgrav
369990Sdes * All rights reserved.
469990Sdes *
569990Sdes * Redistribution and use in source and binary forms, with or without
669990Sdes * modification, are permitted provided that the following conditions
769990Sdes * are met:
869990Sdes * 1. Redistributions of source code must retain the above copyright
969990Sdes *    notice, this list of conditions and the following disclaimer
1069990Sdes *    in this position and unchanged.
1169990Sdes * 2. Redistributions in binary form must reproduce the above copyright
1269990Sdes *    notice, this list of conditions and the following disclaimer in the
1369990Sdes *    documentation and/or other materials provided with the distribution.
1469990Sdes * 3. The name of the author may not be used to endorse or promote products
1569990Sdes *    derived from this software without specific prior written permission.
1669990Sdes *
1769990Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1869990Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1969990Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2069990Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2169990Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2269990Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2369990Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2469990Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2569990Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2669990Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2769990Sdes *
2869990Sdes *      $FreeBSD: head/sys/kern/subr_sbuf.c 111119 2003-02-19 05:47:46Z imp $
2969990Sdes */
3069990Sdes
3169990Sdes#include <sys/param.h>
3274840Sken
3374840Sken#ifdef _KERNEL
3484097Sdes#include <sys/ctype.h>
3569990Sdes#include <sys/kernel.h>
3669990Sdes#include <sys/malloc.h>
3769990Sdes#include <sys/systm.h>
3884097Sdes#include <sys/uio.h>
3969990Sdes#include <machine/stdarg.h>
4074840Sken#else /* _KERNEL */
4184097Sdes#include <ctype.h>
4274840Sken#include <stdarg.h>
4388950Skbyanc#include <stdio.h>
4478340Sjlemon#include <stdlib.h>
4588950Skbyanc#include <string.h>
4674840Sken#endif /* _KERNEL */
4769990Sdes
4884097Sdes#include <sys/sbuf.h>
4984097Sdes
5074840Sken#ifdef _KERNEL
5169990SdesMALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
52111119Simp#define	SBMALLOC(size)		malloc(size, M_SBUF, M_WAITOK)
5389121Skbyanc#define	SBFREE(buf)		free(buf, M_SBUF)
5474840Sken#else /* _KERNEL */
5589121Skbyanc#define	KASSERT(e, m)
5689121Skbyanc#define	SBMALLOC(size)		malloc(size)
5789121Skbyanc#define	SBFREE(buf)		free(buf)
5889121Skbyanc#define	min(x,y)		MIN(x,y)
5974840Sken#endif /* _KERNEL */
6069990Sdes
6171721Sdes/*
6271721Sdes * Predicates
6371721Sdes */
6489121Skbyanc#define	SBUF_ISDYNAMIC(s)	((s)->s_flags & SBUF_DYNAMIC)
6589121Skbyanc#define	SBUF_ISDYNSTRUCT(s)	((s)->s_flags & SBUF_DYNSTRUCT)
6689121Skbyanc#define	SBUF_ISFINISHED(s)	((s)->s_flags & SBUF_FINISHED)
6789121Skbyanc#define	SBUF_HASOVERFLOWED(s)	((s)->s_flags & SBUF_OVERFLOWED)
6889121Skbyanc#define	SBUF_HASROOM(s)		((s)->s_len < (s)->s_size - 1)
6988950Skbyanc#define	SBUF_FREESPACE(s)	((s)->s_size - (s)->s_len - 1)
7088950Skbyanc#define	SBUF_CANEXTEND(s)	((s)->s_flags & SBUF_AUTOEXTEND)
7171721Sdes
7271721Sdes/*
7371721Sdes * Set / clear flags
7471721Sdes */
7589121Skbyanc#define	SBUF_SETFLAG(s, f)	do { (s)->s_flags |= (f); } while (0)
7689121Skbyanc#define	SBUF_CLEARFLAG(s, f)	do { (s)->s_flags &= ~(f); } while (0)
7771721Sdes
7889121Skbyanc#define	SBUF_MINEXTENDSIZE	16		/* Should be power of 2. */
7989121Skbyanc#define	SBUF_MAXEXTENDSIZE	PAGE_SIZE
8089121Skbyanc#define	SBUF_MAXEXTENDINCR	PAGE_SIZE
8188950Skbyanc
8271721Sdes/*
8371721Sdes * Debugging support
8471721Sdes */
8574840Sken#if defined(_KERNEL) && defined(INVARIANTS)
8669990Sdesstatic void
8792664Speter_assert_sbuf_integrity(const char *fun, struct sbuf *s)
8869990Sdes{
8969990Sdes	KASSERT(s != NULL,
9073891Sdes	    ("%s called with a NULL sbuf pointer", fun));
9169990Sdes	KASSERT(s->s_buf != NULL,
9288950Skbyanc	    ("%s called with uninitialized or corrupt sbuf", fun));
9369990Sdes	KASSERT(s->s_len < s->s_size,
9469990Sdes	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
9569990Sdes}
9669990Sdes
9769990Sdesstatic void
9892664Speter_assert_sbuf_state(const char *fun, struct sbuf *s, int state)
9969990Sdes{
10069990Sdes	KASSERT((s->s_flags & SBUF_FINISHED) == state,
10173891Sdes	    ("%s called with %sfinished or corrupt sbuf", fun,
10269990Sdes	    (state ? "un" : "")));
10369990Sdes}
10489121Skbyanc#define	assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
10589121Skbyanc#define	assert_sbuf_state(s, i)	 _assert_sbuf_state(__func__, (s), (i))
10674840Sken#else /* _KERNEL && INVARIANTS */
10789121Skbyanc#define	assert_sbuf_integrity(s) do { } while (0)
10889121Skbyanc#define	assert_sbuf_state(s, i)	 do { } while (0)
10974840Sken#endif /* _KERNEL && INVARIANTS */
11069990Sdes
11188950Skbyancstatic int
11288950Skbyancsbuf_extendsize(int size)
11388950Skbyanc{
11488950Skbyanc	int newsize;
11588950Skbyanc
11688950Skbyanc	newsize = SBUF_MINEXTENDSIZE;
11788950Skbyanc	while (newsize < size) {
118103404Sphk		if (newsize < (int)SBUF_MAXEXTENDSIZE)
11988950Skbyanc			newsize *= 2;
12088950Skbyanc		else
12188950Skbyanc			newsize += SBUF_MAXEXTENDINCR;
12288950Skbyanc	}
12388950Skbyanc
12488950Skbyanc	return (newsize);
12588950Skbyanc}
12688950Skbyanc
12788950Skbyanc
12869990Sdes/*
12988950Skbyanc * Extend an sbuf.
13088950Skbyanc */
13188950Skbyancstatic int
13288950Skbyancsbuf_extend(struct sbuf *s, int addlen)
13388950Skbyanc{
13488950Skbyanc	char *newbuf;
13588950Skbyanc	int newsize;
13688950Skbyanc
13788950Skbyanc	if (!SBUF_CANEXTEND(s))
13888950Skbyanc		return (-1);
13988950Skbyanc
14088950Skbyanc	newsize = sbuf_extendsize(s->s_size + addlen);
14188950Skbyanc	newbuf = (char *)SBMALLOC(newsize);
14288950Skbyanc	if (newbuf == NULL)
14388950Skbyanc		return (-1);
14488950Skbyanc	bcopy(s->s_buf, newbuf, s->s_size);
14588950Skbyanc	if (SBUF_ISDYNAMIC(s))
14688950Skbyanc		SBFREE(s->s_buf);
14788950Skbyanc	else
14888950Skbyanc		SBUF_SETFLAG(s, SBUF_DYNAMIC);
14988950Skbyanc	s->s_buf = newbuf;
15088950Skbyanc	s->s_size = newsize;
15188950Skbyanc	return (0);
15288950Skbyanc}
15388950Skbyanc
15488950Skbyanc/*
15569990Sdes * Initialize an sbuf.
15669990Sdes * If buf is non-NULL, it points to a static or already-allocated string
15769990Sdes * big enough to hold at least length characters.
15869990Sdes */
15977989Sdesstruct sbuf *
16071721Sdessbuf_new(struct sbuf *s, char *buf, int length, int flags)
16169990Sdes{
16271721Sdes	KASSERT(length >= 0,
16371721Sdes	    ("attempt to create an sbuf of negative length (%d)", length));
16488950Skbyanc	KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
16588950Skbyanc	    ("%s called with invalid flags", __func__));
16669990Sdes
16788950Skbyanc	flags &= SBUF_USRFLAGMSK;
16877989Sdes	if (s == NULL) {
16977989Sdes		s = (struct sbuf *)SBMALLOC(sizeof *s);
17077989Sdes		if (s == NULL)
17177989Sdes			return (NULL);
17277989Sdes		bzero(s, sizeof *s);
17388950Skbyanc		s->s_flags = flags;
17477989Sdes		SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
17577989Sdes	} else {
17677989Sdes		bzero(s, sizeof *s);
17788950Skbyanc		s->s_flags = flags;
17877989Sdes	}
17969990Sdes	s->s_size = length;
18069990Sdes	if (buf) {
18169990Sdes		s->s_buf = buf;
18277989Sdes		return (s);
18369990Sdes	}
18488950Skbyanc	if (flags & SBUF_AUTOEXTEND)
18588950Skbyanc		s->s_size = sbuf_extendsize(s->s_size);
18674840Sken	s->s_buf = (char *)SBMALLOC(s->s_size);
18777989Sdes	if (s->s_buf == NULL) {
18877989Sdes		if (SBUF_ISDYNSTRUCT(s))
18977989Sdes			SBFREE(s);
19077989Sdes		return (NULL);
19177989Sdes	}
19269990Sdes	SBUF_SETFLAG(s, SBUF_DYNAMIC);
19377989Sdes	return (s);
19469990Sdes}
19569990Sdes
19684097Sdes#ifdef _KERNEL
19769990Sdes/*
19884097Sdes * Create an sbuf with uio data
19984097Sdes */
20084097Sdesstruct sbuf *
20184097Sdessbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
20284097Sdes{
20384097Sdes	KASSERT(uio != NULL,
20487594Sobrien	    ("%s called with NULL uio pointer", __func__));
20584097Sdes	KASSERT(error != NULL,
20687594Sobrien	    ("%s called with NULL error pointer", __func__));
20784097Sdes
20884097Sdes	s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
20984097Sdes	if (s == NULL) {
21084097Sdes		*error = ENOMEM;
21184097Sdes		return (NULL);
21284097Sdes	}
21384097Sdes	*error = uiomove(s->s_buf, uio->uio_resid, uio);
21484097Sdes	if (*error != 0) {
21584097Sdes		sbuf_delete(s);
21684097Sdes		return (NULL);
21784097Sdes	}
21884097Sdes	s->s_len = s->s_size - 1;
21984097Sdes	*error = 0;
22084097Sdes	return (s);
22184097Sdes}
22284097Sdes#endif
22384097Sdes
22484097Sdes/*
22588950Skbyanc * Clear an sbuf and reset its position.
22671721Sdes */
22771721Sdesvoid
22871721Sdessbuf_clear(struct sbuf *s)
22971721Sdes{
23071721Sdes	assert_sbuf_integrity(s);
23171724Sdes	/* don't care if it's finished or not */
23271721Sdes
23371721Sdes	SBUF_CLEARFLAG(s, SBUF_FINISHED);
23471721Sdes	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
23571721Sdes	s->s_len = 0;
23671721Sdes}
23771721Sdes
23871721Sdes/*
23988950Skbyanc * Set the sbuf's end position to an arbitrary value.
24088950Skbyanc * Effectively truncates the sbuf at the new position.
24169990Sdes */
24269990Sdesint
24371721Sdessbuf_setpos(struct sbuf *s, int pos)
24469990Sdes{
24569990Sdes	assert_sbuf_integrity(s);
24669990Sdes	assert_sbuf_state(s, 0);
24769990Sdes
24869990Sdes	KASSERT(pos >= 0,
24969990Sdes	    ("attempt to seek to a negative position (%d)", pos));
25069990Sdes	KASSERT(pos < s->s_size,
25169990Sdes	    ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
25269990Sdes
25369990Sdes	if (pos < 0 || pos > s->s_len)
25469990Sdes		return (-1);
25569990Sdes	s->s_len = pos;
25669990Sdes	return (0);
25769990Sdes}
25869990Sdes
25969990Sdes/*
26078077Sdes * Append a byte string to an sbuf.
26178077Sdes */
26278077Sdesint
26378077Sdessbuf_bcat(struct sbuf *s, const char *str, size_t len)
26478077Sdes{
26578077Sdes	assert_sbuf_integrity(s);
26678077Sdes	assert_sbuf_state(s, 0);
26778077Sdes
26878077Sdes	if (SBUF_HASOVERFLOWED(s))
26978077Sdes		return (-1);
27078077Sdes
27189765Sphk	for (; len; len--) {
27288950Skbyanc		if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0)
27388950Skbyanc			break;
27478077Sdes		s->s_buf[s->s_len++] = *str++;
27588950Skbyanc	}
27678077Sdes	if (len) {
27778077Sdes		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
27878077Sdes		return (-1);
27978077Sdes	}
28078077Sdes	return (0);
28178077Sdes}
28278077Sdes
28378077Sdes#ifdef _KERNEL
28478077Sdes/*
28578077Sdes * Copy a byte string from userland into an sbuf.
28678077Sdes */
28778077Sdesint
28878077Sdessbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
28978077Sdes{
29078077Sdes	assert_sbuf_integrity(s);
29178077Sdes	assert_sbuf_state(s, 0);
29278077Sdes
29378077Sdes	if (SBUF_HASOVERFLOWED(s))
29478077Sdes		return (-1);
29578077Sdes
29678077Sdes	if (len == 0)
29778077Sdes		return (0);
29888950Skbyanc	if (len > SBUF_FREESPACE(s)) {
29988950Skbyanc		sbuf_extend(s, len - SBUF_FREESPACE(s));
30088950Skbyanc		len = min(len, SBUF_FREESPACE(s));
30188950Skbyanc	}
30278092Sdes	if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
30378092Sdes		return (-1);
30478095Sdes	s->s_len += len;
30578077Sdes
30678077Sdes	return (0);
30778077Sdes}
30878077Sdes#endif
30978077Sdes
31078077Sdes/*
31178077Sdes * Copy a byte string into an sbuf.
31278077Sdes */
31378077Sdesint
31478077Sdessbuf_bcpy(struct sbuf *s, const char *str, size_t len)
31578077Sdes{
31678077Sdes	assert_sbuf_integrity(s);
31778077Sdes	assert_sbuf_state(s, 0);
31878077Sdes
31978077Sdes	sbuf_clear(s);
32078077Sdes	return (sbuf_bcat(s, str, len));
32178077Sdes}
32278077Sdes
32378077Sdes/*
32469990Sdes * Append a string to an sbuf.
32569990Sdes */
32669990Sdesint
32774840Skensbuf_cat(struct sbuf *s, const char *str)
32869990Sdes{
32969990Sdes	assert_sbuf_integrity(s);
33069990Sdes	assert_sbuf_state(s, 0);
33169990Sdes
33269990Sdes	if (SBUF_HASOVERFLOWED(s))
33369990Sdes		return (-1);
33469990Sdes
33588950Skbyanc	while (*str) {
33688950Skbyanc		if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0)
33788950Skbyanc			break;
33869990Sdes		s->s_buf[s->s_len++] = *str++;
33988950Skbyanc	}
34069990Sdes	if (*str) {
34169990Sdes		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
34269990Sdes		return (-1);
34369990Sdes	}
34469990Sdes	return (0);
34569990Sdes}
34669990Sdes
34778077Sdes#ifdef _KERNEL
34869990Sdes/*
34988950Skbyanc * Append a string from userland to an sbuf.
35078077Sdes */
35178077Sdesint
35278077Sdessbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
35378077Sdes{
35478077Sdes	size_t done;
35578077Sdes
35678077Sdes	assert_sbuf_integrity(s);
35778077Sdes	assert_sbuf_state(s, 0);
35878077Sdes
35978077Sdes	if (SBUF_HASOVERFLOWED(s))
36078077Sdes		return (-1);
36178077Sdes
36288950Skbyanc	if (len == 0)
36388950Skbyanc		len = SBUF_FREESPACE(s);	/* XXX return 0? */
36488950Skbyanc	if (len > SBUF_FREESPACE(s)) {
36588950Skbyanc		sbuf_extend(s, len);
36688950Skbyanc		len = min(len, SBUF_FREESPACE(s));
36788950Skbyanc	}
36878077Sdes	switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
36978077Sdes	case ENAMETOOLONG:
37078077Sdes		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
37178077Sdes		/* fall through */
37278077Sdes	case 0:
37378077Sdes		s->s_len += done - 1;
37478077Sdes		break;
37578077Sdes	default:
37678077Sdes		return (-1);	/* XXX */
37778077Sdes	}
37878077Sdes
37978077Sdes	return (0);
38078077Sdes}
38178077Sdes#endif
38278077Sdes
38378077Sdes/*
38469990Sdes * Copy a string into an sbuf.
38569990Sdes */
38669990Sdesint
38774840Skensbuf_cpy(struct sbuf *s, const char *str)
38869990Sdes{
38969990Sdes	assert_sbuf_integrity(s);
39069990Sdes	assert_sbuf_state(s, 0);
39169990Sdes
39271721Sdes	sbuf_clear(s);
39369990Sdes	return (sbuf_cat(s, str));
39469990Sdes}
39569990Sdes
39669990Sdes/*
39788950Skbyanc * Format the given argument list and append the resulting string to an sbuf.
39869990Sdes */
39969990Sdesint
40088950Skbyancsbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
40169990Sdes{
40271721Sdes	int len;
40369990Sdes
40469990Sdes	assert_sbuf_integrity(s);
40569990Sdes	assert_sbuf_state(s, 0);
40688950Skbyanc
40769990Sdes	KASSERT(fmt != NULL,
40887594Sobrien	    ("%s called with a NULL format string", __func__));
40988950Skbyanc
41069990Sdes	if (SBUF_HASOVERFLOWED(s))
41169990Sdes		return (-1);
41269990Sdes
41388950Skbyanc	do {
41488950Skbyanc		len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
41588950Skbyanc		    fmt, ap);
41688950Skbyanc	} while (len > SBUF_FREESPACE(s) &&
41788950Skbyanc	    sbuf_extend(s, len - SBUF_FREESPACE(s)) == 0);
41869990Sdes
41974840Sken	/*
42074840Sken	 * s->s_len is the length of the string, without the terminating nul.
42174840Sken	 * When updating s->s_len, we must subtract 1 from the length that
42274840Sken	 * we passed into vsnprintf() because that length includes the
42374840Sken	 * terminating nul.
42474840Sken	 *
42574840Sken	 * vsnprintf() returns the amount that would have been copied,
42674840Sken	 * given sufficient space, hence the min() calculation below.
42774840Sken	 */
42888950Skbyanc	s->s_len += min(len, SBUF_FREESPACE(s));
42989646Sphk	if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
43074840Sken		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
43174840Sken
43269990Sdes	KASSERT(s->s_len < s->s_size,
43369990Sdes	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
43469990Sdes
43569990Sdes	if (SBUF_HASOVERFLOWED(s))
43669990Sdes		return (-1);
43769990Sdes	return (0);
43869990Sdes}
43969990Sdes
44069990Sdes/*
44188950Skbyanc * Format the given arguments and append the resulting string to an sbuf.
44288950Skbyanc */
44388950Skbyancint
44488950Skbyancsbuf_printf(struct sbuf *s, const char *fmt, ...)
44588950Skbyanc{
44688950Skbyanc	va_list ap;
44788950Skbyanc	int result;
44888950Skbyanc
44988950Skbyanc	va_start(ap, fmt);
45088950Skbyanc	result = sbuf_vprintf(s, fmt, ap);
45188950Skbyanc	va_end(ap);
45288950Skbyanc	return(result);
45388950Skbyanc}
45488950Skbyanc
45588950Skbyanc/*
45669990Sdes * Append a character to an sbuf.
45769990Sdes */
45869990Sdesint
45969990Sdessbuf_putc(struct sbuf *s, int c)
46069990Sdes{
46169990Sdes	assert_sbuf_integrity(s);
46269990Sdes	assert_sbuf_state(s, 0);
46369990Sdes
46469990Sdes	if (SBUF_HASOVERFLOWED(s))
46569990Sdes		return (-1);
46669990Sdes
46788950Skbyanc	if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) {
46869990Sdes		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
46969990Sdes		return (-1);
47069990Sdes	}
47173891Sdes	if (c != '\0')
47273891Sdes	    s->s_buf[s->s_len++] = c;
47369990Sdes	return (0);
47469990Sdes}
47569990Sdes
47669990Sdes/*
47788950Skbyanc * Trim whitespace characters from end of an sbuf.
47884097Sdes */
47984097Sdesint
48084097Sdessbuf_trim(struct sbuf *s)
48184097Sdes{
48284097Sdes	assert_sbuf_integrity(s);
48384097Sdes	assert_sbuf_state(s, 0);
48484097Sdes
48584097Sdes	if (SBUF_HASOVERFLOWED(s))
48684097Sdes		return (-1);
48784097Sdes
48884097Sdes	while (s->s_len && isspace(s->s_buf[s->s_len-1]))
48984097Sdes		--s->s_len;
49084097Sdes
49184097Sdes	return (0);
49284097Sdes}
49384097Sdes
49484097Sdes/*
49571721Sdes * Check if an sbuf overflowed
49671721Sdes */
49771721Sdesint
49871721Sdessbuf_overflowed(struct sbuf *s)
49971721Sdes{
50071721Sdes    return SBUF_HASOVERFLOWED(s);
50171721Sdes}
50271721Sdes
50371721Sdes/*
50469990Sdes * Finish off an sbuf.
50569990Sdes */
50671721Sdesvoid
50769990Sdessbuf_finish(struct sbuf *s)
50869990Sdes{
50969990Sdes	assert_sbuf_integrity(s);
51069990Sdes	assert_sbuf_state(s, 0);
51169990Sdes
51273891Sdes	s->s_buf[s->s_len] = '\0';
51371721Sdes	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
51469990Sdes	SBUF_SETFLAG(s, SBUF_FINISHED);
51569990Sdes}
51669990Sdes
51769990Sdes/*
51869990Sdes * Return a pointer to the sbuf data.
51969990Sdes */
52069990Sdeschar *
52169990Sdessbuf_data(struct sbuf *s)
52269990Sdes{
52369990Sdes	assert_sbuf_integrity(s);
52469990Sdes	assert_sbuf_state(s, SBUF_FINISHED);
52569990Sdes
52669990Sdes	return s->s_buf;
52769990Sdes}
52869990Sdes
52969990Sdes/*
53069990Sdes * Return the length of the sbuf data.
53169990Sdes */
53271721Sdesint
53369990Sdessbuf_len(struct sbuf *s)
53469990Sdes{
53569990Sdes	assert_sbuf_integrity(s);
53671724Sdes	/* don't care if it's finished or not */
53769990Sdes
53869990Sdes	if (SBUF_HASOVERFLOWED(s))
53971721Sdes		return (-1);
54069990Sdes	return s->s_len;
54169990Sdes}
54269990Sdes
54369990Sdes/*
54469990Sdes * Clear an sbuf, free its buffer if necessary.
54569990Sdes */
54669990Sdesvoid
54769990Sdessbuf_delete(struct sbuf *s)
54869990Sdes{
54988219Sdillon	int isdyn;
55088219Sdillon
55169990Sdes	assert_sbuf_integrity(s);
55269990Sdes	/* don't care if it's finished or not */
55369990Sdes
55469990Sdes	if (SBUF_ISDYNAMIC(s))
55574840Sken		SBFREE(s->s_buf);
55688219Sdillon	isdyn = SBUF_ISDYNSTRUCT(s);
55769990Sdes	bzero(s, sizeof *s);
55888219Sdillon	if (isdyn)
55977989Sdes		SBFREE(s);
56069990Sdes}
561104449Sphk
562104449Sphk/*
563104449Sphk * Check if an sbuf has been finished.
564104449Sphk */
565104449Sphkint
566104449Sphksbuf_done(struct sbuf *s)
567104449Sphk{
568104449Sphk
569104449Sphk	return(SBUF_ISFINISHED(s));
570104449Sphk}
571