subr_sbuf.c revision 141616
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
29116182Sobrien#include <sys/cdefs.h>
30116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_sbuf.c 141616 2005-02-10 12:02:37Z phk $");
31116182Sobrien
3269990Sdes#include <sys/param.h>
3374840Sken
3474840Sken#ifdef _KERNEL
3584097Sdes#include <sys/ctype.h>
3669990Sdes#include <sys/kernel.h>
3769990Sdes#include <sys/malloc.h>
3869990Sdes#include <sys/systm.h>
3984097Sdes#include <sys/uio.h>
4069990Sdes#include <machine/stdarg.h>
4174840Sken#else /* _KERNEL */
4284097Sdes#include <ctype.h>
4374840Sken#include <stdarg.h>
4488950Skbyanc#include <stdio.h>
4578340Sjlemon#include <stdlib.h>
4688950Skbyanc#include <string.h>
4774840Sken#endif /* _KERNEL */
4869990Sdes
4984097Sdes#include <sys/sbuf.h>
5084097Sdes
5174840Sken#ifdef _KERNEL
52141616Sphkstatic MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
53111119Simp#define	SBMALLOC(size)		malloc(size, M_SBUF, M_WAITOK)
5489121Skbyanc#define	SBFREE(buf)		free(buf, M_SBUF)
5574840Sken#else /* _KERNEL */
5689121Skbyanc#define	KASSERT(e, m)
5789121Skbyanc#define	SBMALLOC(size)		malloc(size)
5889121Skbyanc#define	SBFREE(buf)		free(buf)
5989121Skbyanc#define	min(x,y)		MIN(x,y)
6074840Sken#endif /* _KERNEL */
6169990Sdes
6271721Sdes/*
6371721Sdes * Predicates
6471721Sdes */
6589121Skbyanc#define	SBUF_ISDYNAMIC(s)	((s)->s_flags & SBUF_DYNAMIC)
6689121Skbyanc#define	SBUF_ISDYNSTRUCT(s)	((s)->s_flags & SBUF_DYNSTRUCT)
6789121Skbyanc#define	SBUF_ISFINISHED(s)	((s)->s_flags & SBUF_FINISHED)
6889121Skbyanc#define	SBUF_HASOVERFLOWED(s)	((s)->s_flags & SBUF_OVERFLOWED)
6989121Skbyanc#define	SBUF_HASROOM(s)		((s)->s_len < (s)->s_size - 1)
7088950Skbyanc#define	SBUF_FREESPACE(s)	((s)->s_size - (s)->s_len - 1)
7188950Skbyanc#define	SBUF_CANEXTEND(s)	((s)->s_flags & SBUF_AUTOEXTEND)
7271721Sdes
7371721Sdes/*
7471721Sdes * Set / clear flags
7571721Sdes */
7689121Skbyanc#define	SBUF_SETFLAG(s, f)	do { (s)->s_flags |= (f); } while (0)
7789121Skbyanc#define	SBUF_CLEARFLAG(s, f)	do { (s)->s_flags &= ~(f); } while (0)
7871721Sdes
7989121Skbyanc#define	SBUF_MINEXTENDSIZE	16		/* Should be power of 2. */
8089121Skbyanc#define	SBUF_MAXEXTENDSIZE	PAGE_SIZE
8189121Skbyanc#define	SBUF_MAXEXTENDINCR	PAGE_SIZE
8288950Skbyanc
8371721Sdes/*
8471721Sdes * Debugging support
8571721Sdes */
8674840Sken#if defined(_KERNEL) && defined(INVARIANTS)
8769990Sdesstatic void
8892664Speter_assert_sbuf_integrity(const char *fun, struct sbuf *s)
8969990Sdes{
9069990Sdes	KASSERT(s != NULL,
9173891Sdes	    ("%s called with a NULL sbuf pointer", fun));
9269990Sdes	KASSERT(s->s_buf != NULL,
9388950Skbyanc	    ("%s called with uninitialized or corrupt sbuf", fun));
9469990Sdes	KASSERT(s->s_len < s->s_size,
9569990Sdes	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
9669990Sdes}
9769990Sdes
9869990Sdesstatic void
9992664Speter_assert_sbuf_state(const char *fun, struct sbuf *s, int state)
10069990Sdes{
10169990Sdes	KASSERT((s->s_flags & SBUF_FINISHED) == state,
10273891Sdes	    ("%s called with %sfinished or corrupt sbuf", fun,
10369990Sdes	    (state ? "un" : "")));
10469990Sdes}
10589121Skbyanc#define	assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
10689121Skbyanc#define	assert_sbuf_state(s, i)	 _assert_sbuf_state(__func__, (s), (i))
10774840Sken#else /* _KERNEL && INVARIANTS */
10889121Skbyanc#define	assert_sbuf_integrity(s) do { } while (0)
10989121Skbyanc#define	assert_sbuf_state(s, i)	 do { } while (0)
11074840Sken#endif /* _KERNEL && INVARIANTS */
11169990Sdes
11288950Skbyancstatic int
11388950Skbyancsbuf_extendsize(int size)
11488950Skbyanc{
11588950Skbyanc	int newsize;
11688950Skbyanc
11788950Skbyanc	newsize = SBUF_MINEXTENDSIZE;
11888950Skbyanc	while (newsize < size) {
119103404Sphk		if (newsize < (int)SBUF_MAXEXTENDSIZE)
12088950Skbyanc			newsize *= 2;
12188950Skbyanc		else
12288950Skbyanc			newsize += SBUF_MAXEXTENDINCR;
12388950Skbyanc	}
12488950Skbyanc
12588950Skbyanc	return (newsize);
12688950Skbyanc}
12788950Skbyanc
12888950Skbyanc
12969990Sdes/*
13088950Skbyanc * Extend an sbuf.
13188950Skbyanc */
13288950Skbyancstatic int
13388950Skbyancsbuf_extend(struct sbuf *s, int addlen)
13488950Skbyanc{
13588950Skbyanc	char *newbuf;
13688950Skbyanc	int newsize;
13788950Skbyanc
13888950Skbyanc	if (!SBUF_CANEXTEND(s))
13988950Skbyanc		return (-1);
14088950Skbyanc
14188950Skbyanc	newsize = sbuf_extendsize(s->s_size + addlen);
14288950Skbyanc	newbuf = (char *)SBMALLOC(newsize);
14388950Skbyanc	if (newbuf == NULL)
14488950Skbyanc		return (-1);
14588950Skbyanc	bcopy(s->s_buf, newbuf, s->s_size);
14688950Skbyanc	if (SBUF_ISDYNAMIC(s))
14788950Skbyanc		SBFREE(s->s_buf);
14888950Skbyanc	else
14988950Skbyanc		SBUF_SETFLAG(s, SBUF_DYNAMIC);
15088950Skbyanc	s->s_buf = newbuf;
15188950Skbyanc	s->s_size = newsize;
15288950Skbyanc	return (0);
15388950Skbyanc}
15488950Skbyanc
15588950Skbyanc/*
15669990Sdes * Initialize an sbuf.
15769990Sdes * If buf is non-NULL, it points to a static or already-allocated string
15869990Sdes * big enough to hold at least length characters.
15969990Sdes */
16077989Sdesstruct sbuf *
16171721Sdessbuf_new(struct sbuf *s, char *buf, int length, int flags)
16269990Sdes{
16371721Sdes	KASSERT(length >= 0,
16471721Sdes	    ("attempt to create an sbuf of negative length (%d)", length));
16588950Skbyanc	KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
16688950Skbyanc	    ("%s called with invalid flags", __func__));
16769990Sdes
16888950Skbyanc	flags &= SBUF_USRFLAGMSK;
16977989Sdes	if (s == NULL) {
17077989Sdes		s = (struct sbuf *)SBMALLOC(sizeof *s);
17177989Sdes		if (s == NULL)
17277989Sdes			return (NULL);
17377989Sdes		bzero(s, sizeof *s);
17488950Skbyanc		s->s_flags = flags;
17577989Sdes		SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
17677989Sdes	} else {
17777989Sdes		bzero(s, sizeof *s);
17888950Skbyanc		s->s_flags = flags;
17977989Sdes	}
18069990Sdes	s->s_size = length;
18169990Sdes	if (buf) {
18269990Sdes		s->s_buf = buf;
18377989Sdes		return (s);
18469990Sdes	}
18588950Skbyanc	if (flags & SBUF_AUTOEXTEND)
18688950Skbyanc		s->s_size = sbuf_extendsize(s->s_size);
18774840Sken	s->s_buf = (char *)SBMALLOC(s->s_size);
18877989Sdes	if (s->s_buf == NULL) {
18977989Sdes		if (SBUF_ISDYNSTRUCT(s))
19077989Sdes			SBFREE(s);
19177989Sdes		return (NULL);
19277989Sdes	}
19369990Sdes	SBUF_SETFLAG(s, SBUF_DYNAMIC);
19477989Sdes	return (s);
19569990Sdes}
19669990Sdes
19784097Sdes#ifdef _KERNEL
19869990Sdes/*
19984097Sdes * Create an sbuf with uio data
20084097Sdes */
20184097Sdesstruct sbuf *
20284097Sdessbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
20384097Sdes{
20484097Sdes	KASSERT(uio != NULL,
20587594Sobrien	    ("%s called with NULL uio pointer", __func__));
20684097Sdes	KASSERT(error != NULL,
20787594Sobrien	    ("%s called with NULL error pointer", __func__));
20884097Sdes
20984097Sdes	s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
21084097Sdes	if (s == NULL) {
21184097Sdes		*error = ENOMEM;
21284097Sdes		return (NULL);
21384097Sdes	}
21484097Sdes	*error = uiomove(s->s_buf, uio->uio_resid, uio);
21584097Sdes	if (*error != 0) {
21684097Sdes		sbuf_delete(s);
21784097Sdes		return (NULL);
21884097Sdes	}
21984097Sdes	s->s_len = s->s_size - 1;
22084097Sdes	*error = 0;
22184097Sdes	return (s);
22284097Sdes}
22384097Sdes#endif
22484097Sdes
22584097Sdes/*
22688950Skbyanc * Clear an sbuf and reset its position.
22771721Sdes */
22871721Sdesvoid
22971721Sdessbuf_clear(struct sbuf *s)
23071721Sdes{
23171721Sdes	assert_sbuf_integrity(s);
23271724Sdes	/* don't care if it's finished or not */
23371721Sdes
23471721Sdes	SBUF_CLEARFLAG(s, SBUF_FINISHED);
23571721Sdes	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
23671721Sdes	s->s_len = 0;
23771721Sdes}
23871721Sdes
23971721Sdes/*
24088950Skbyanc * Set the sbuf's end position to an arbitrary value.
24188950Skbyanc * Effectively truncates the sbuf at the new position.
24269990Sdes */
24369990Sdesint
24471721Sdessbuf_setpos(struct sbuf *s, int pos)
24569990Sdes{
24669990Sdes	assert_sbuf_integrity(s);
24769990Sdes	assert_sbuf_state(s, 0);
248125937Sdes
24969990Sdes	KASSERT(pos >= 0,
25069990Sdes	    ("attempt to seek to a negative position (%d)", pos));
25169990Sdes	KASSERT(pos < s->s_size,
25269990Sdes	    ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size));
253125937Sdes
25469990Sdes	if (pos < 0 || pos > s->s_len)
25569990Sdes		return (-1);
25669990Sdes	s->s_len = pos;
25769990Sdes	return (0);
25869990Sdes}
25969990Sdes
26069990Sdes/*
26178077Sdes * Append a byte string to an sbuf.
26278077Sdes */
26378077Sdesint
264131869Sdessbuf_bcat(struct sbuf *s, const void *buf, size_t len)
26578077Sdes{
266131869Sdes	const char *str = buf;
267131868Sdes
26878077Sdes	assert_sbuf_integrity(s);
26978077Sdes	assert_sbuf_state(s, 0);
270125937Sdes
27178077Sdes	if (SBUF_HASOVERFLOWED(s))
27278077Sdes		return (-1);
273125937Sdes
27489765Sphk	for (; len; len--) {
27588950Skbyanc		if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0)
27688950Skbyanc			break;
27778077Sdes		s->s_buf[s->s_len++] = *str++;
27888950Skbyanc	}
27978077Sdes	if (len) {
28078077Sdes		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
28178077Sdes		return (-1);
28278077Sdes	}
28378077Sdes	return (0);
28478077Sdes}
28578077Sdes
28678077Sdes#ifdef _KERNEL
28778077Sdes/*
28878077Sdes * Copy a byte string from userland into an sbuf.
28978077Sdes */
29078077Sdesint
29178077Sdessbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
29278077Sdes{
29378077Sdes	assert_sbuf_integrity(s);
29478077Sdes	assert_sbuf_state(s, 0);
29578077Sdes
29678077Sdes	if (SBUF_HASOVERFLOWED(s))
29778077Sdes		return (-1);
29878077Sdes
29978077Sdes	if (len == 0)
30078077Sdes		return (0);
30188950Skbyanc	if (len > SBUF_FREESPACE(s)) {
30288950Skbyanc		sbuf_extend(s, len - SBUF_FREESPACE(s));
30388950Skbyanc		len = min(len, SBUF_FREESPACE(s));
30488950Skbyanc	}
30578092Sdes	if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
30678092Sdes		return (-1);
30778095Sdes	s->s_len += len;
308125937Sdes
30978077Sdes	return (0);
31078077Sdes}
31178077Sdes#endif
31278077Sdes
31378077Sdes/*
31478077Sdes * Copy a byte string into an sbuf.
31578077Sdes */
31678077Sdesint
317131869Sdessbuf_bcpy(struct sbuf *s, const void *buf, size_t len)
31878077Sdes{
31978077Sdes	assert_sbuf_integrity(s);
32078077Sdes	assert_sbuf_state(s, 0);
321125937Sdes
32278077Sdes	sbuf_clear(s);
323131869Sdes	return (sbuf_bcat(s, buf, len));
32478077Sdes}
32578077Sdes
32678077Sdes/*
32769990Sdes * Append a string to an sbuf.
32869990Sdes */
32969990Sdesint
33074840Skensbuf_cat(struct sbuf *s, const char *str)
33169990Sdes{
33269990Sdes	assert_sbuf_integrity(s);
33369990Sdes	assert_sbuf_state(s, 0);
334125937Sdes
33569990Sdes	if (SBUF_HASOVERFLOWED(s))
33669990Sdes		return (-1);
337125937Sdes
33888950Skbyanc	while (*str) {
33988950Skbyanc		if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0)
34088950Skbyanc			break;
34169990Sdes		s->s_buf[s->s_len++] = *str++;
34288950Skbyanc	}
34369990Sdes	if (*str) {
34469990Sdes		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
34569990Sdes		return (-1);
34669990Sdes	}
34769990Sdes	return (0);
34869990Sdes}
34969990Sdes
35078077Sdes#ifdef _KERNEL
35169990Sdes/*
35288950Skbyanc * Append a string from userland to an sbuf.
35378077Sdes */
35478077Sdesint
35578077Sdessbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
35678077Sdes{
35778077Sdes	size_t done;
358125937Sdes
35978077Sdes	assert_sbuf_integrity(s);
36078077Sdes	assert_sbuf_state(s, 0);
36178077Sdes
36278077Sdes	if (SBUF_HASOVERFLOWED(s))
36378077Sdes		return (-1);
36478077Sdes
36588950Skbyanc	if (len == 0)
36688950Skbyanc		len = SBUF_FREESPACE(s);	/* XXX return 0? */
36788950Skbyanc	if (len > SBUF_FREESPACE(s)) {
36888950Skbyanc		sbuf_extend(s, len);
36988950Skbyanc		len = min(len, SBUF_FREESPACE(s));
37088950Skbyanc	}
37178077Sdes	switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
37278077Sdes	case ENAMETOOLONG:
37378077Sdes		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
37478077Sdes		/* fall through */
37578077Sdes	case 0:
37678077Sdes		s->s_len += done - 1;
37778077Sdes		break;
37878077Sdes	default:
37978077Sdes		return (-1);	/* XXX */
38078077Sdes	}
381125937Sdes
38278077Sdes	return (0);
38378077Sdes}
38478077Sdes#endif
38578077Sdes
38678077Sdes/*
38769990Sdes * Copy a string into an sbuf.
38869990Sdes */
38969990Sdesint
39074840Skensbuf_cpy(struct sbuf *s, const char *str)
39169990Sdes{
39269990Sdes	assert_sbuf_integrity(s);
39369990Sdes	assert_sbuf_state(s, 0);
394125937Sdes
39571721Sdes	sbuf_clear(s);
39669990Sdes	return (sbuf_cat(s, str));
39769990Sdes}
39869990Sdes
39969990Sdes/*
40088950Skbyanc * Format the given argument list and append the resulting string to an sbuf.
40169990Sdes */
40269990Sdesint
40388950Skbyancsbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
40469990Sdes{
405115311Speter	va_list ap_copy;
40671721Sdes	int len;
40769990Sdes
40869990Sdes	assert_sbuf_integrity(s);
40969990Sdes	assert_sbuf_state(s, 0);
41088950Skbyanc
41169990Sdes	KASSERT(fmt != NULL,
41287594Sobrien	    ("%s called with a NULL format string", __func__));
41388950Skbyanc
41469990Sdes	if (SBUF_HASOVERFLOWED(s))
41569990Sdes		return (-1);
41669990Sdes
41788950Skbyanc	do {
418115311Speter		va_copy(ap_copy, ap);
41988950Skbyanc		len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
420115311Speter		    fmt, ap_copy);
421115311Speter		va_end(ap_copy);
42288950Skbyanc	} while (len > SBUF_FREESPACE(s) &&
42388950Skbyanc	    sbuf_extend(s, len - SBUF_FREESPACE(s)) == 0);
42469990Sdes
42574840Sken	/*
42674840Sken	 * s->s_len is the length of the string, without the terminating nul.
42774840Sken	 * When updating s->s_len, we must subtract 1 from the length that
42874840Sken	 * we passed into vsnprintf() because that length includes the
42974840Sken	 * terminating nul.
43074840Sken	 *
43174840Sken	 * vsnprintf() returns the amount that would have been copied,
43274840Sken	 * given sufficient space, hence the min() calculation below.
43374840Sken	 */
43488950Skbyanc	s->s_len += min(len, SBUF_FREESPACE(s));
43589646Sphk	if (!SBUF_HASROOM(s) && !SBUF_CANEXTEND(s))
43674840Sken		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
43774840Sken
43869990Sdes	KASSERT(s->s_len < s->s_size,
43969990Sdes	    ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
44069990Sdes
44169990Sdes	if (SBUF_HASOVERFLOWED(s))
44269990Sdes		return (-1);
44369990Sdes	return (0);
44469990Sdes}
44569990Sdes
44669990Sdes/*
44788950Skbyanc * Format the given arguments and append the resulting string to an sbuf.
44888950Skbyanc */
44988950Skbyancint
45088950Skbyancsbuf_printf(struct sbuf *s, const char *fmt, ...)
45188950Skbyanc{
45288950Skbyanc	va_list ap;
45388950Skbyanc	int result;
45488950Skbyanc
45588950Skbyanc	va_start(ap, fmt);
45688950Skbyanc	result = sbuf_vprintf(s, fmt, ap);
45788950Skbyanc	va_end(ap);
45888950Skbyanc	return(result);
45988950Skbyanc}
46088950Skbyanc
46188950Skbyanc/*
46269990Sdes * Append a character to an sbuf.
46369990Sdes */
46469990Sdesint
46569990Sdessbuf_putc(struct sbuf *s, int c)
46669990Sdes{
46769990Sdes	assert_sbuf_integrity(s);
46869990Sdes	assert_sbuf_state(s, 0);
469125937Sdes
47069990Sdes	if (SBUF_HASOVERFLOWED(s))
47169990Sdes		return (-1);
472125937Sdes
47388950Skbyanc	if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) {
47469990Sdes		SBUF_SETFLAG(s, SBUF_OVERFLOWED);
47569990Sdes		return (-1);
47669990Sdes	}
47773891Sdes	if (c != '\0')
47873891Sdes	    s->s_buf[s->s_len++] = c;
47969990Sdes	return (0);
48069990Sdes}
48169990Sdes
48269990Sdes/*
48388950Skbyanc * Trim whitespace characters from end of an sbuf.
48484097Sdes */
48584097Sdesint
48684097Sdessbuf_trim(struct sbuf *s)
48784097Sdes{
48884097Sdes	assert_sbuf_integrity(s);
48984097Sdes	assert_sbuf_state(s, 0);
490125937Sdes
49184097Sdes	if (SBUF_HASOVERFLOWED(s))
49284097Sdes		return (-1);
493125937Sdes
49484097Sdes	while (s->s_len && isspace(s->s_buf[s->s_len-1]))
49584097Sdes		--s->s_len;
49684097Sdes
49784097Sdes	return (0);
49884097Sdes}
49984097Sdes
50084097Sdes/*
50171721Sdes * Check if an sbuf overflowed
50271721Sdes */
50371721Sdesint
50471721Sdessbuf_overflowed(struct sbuf *s)
50571721Sdes{
50671721Sdes    return SBUF_HASOVERFLOWED(s);
50771721Sdes}
50871721Sdes
50971721Sdes/*
51069990Sdes * Finish off an sbuf.
51169990Sdes */
51271721Sdesvoid
51369990Sdessbuf_finish(struct sbuf *s)
51469990Sdes{
51569990Sdes	assert_sbuf_integrity(s);
51669990Sdes	assert_sbuf_state(s, 0);
517125937Sdes
51873891Sdes	s->s_buf[s->s_len] = '\0';
51971721Sdes	SBUF_CLEARFLAG(s, SBUF_OVERFLOWED);
52069990Sdes	SBUF_SETFLAG(s, SBUF_FINISHED);
52169990Sdes}
52269990Sdes
52369990Sdes/*
52469990Sdes * Return a pointer to the sbuf data.
52569990Sdes */
52669990Sdeschar *
52769990Sdessbuf_data(struct sbuf *s)
52869990Sdes{
52969990Sdes	assert_sbuf_integrity(s);
53069990Sdes	assert_sbuf_state(s, SBUF_FINISHED);
531125937Sdes
53269990Sdes	return s->s_buf;
53369990Sdes}
53469990Sdes
53569990Sdes/*
53669990Sdes * Return the length of the sbuf data.
53769990Sdes */
53871721Sdesint
53969990Sdessbuf_len(struct sbuf *s)
54069990Sdes{
54169990Sdes	assert_sbuf_integrity(s);
54271724Sdes	/* don't care if it's finished or not */
543125937Sdes
54469990Sdes	if (SBUF_HASOVERFLOWED(s))
54571721Sdes		return (-1);
54669990Sdes	return s->s_len;
54769990Sdes}
54869990Sdes
54969990Sdes/*
55069990Sdes * Clear an sbuf, free its buffer if necessary.
55169990Sdes */
55269990Sdesvoid
55369990Sdessbuf_delete(struct sbuf *s)
55469990Sdes{
55588219Sdillon	int isdyn;
55688219Sdillon
55769990Sdes	assert_sbuf_integrity(s);
55869990Sdes	/* don't care if it's finished or not */
559125937Sdes
56069990Sdes	if (SBUF_ISDYNAMIC(s))
56174840Sken		SBFREE(s->s_buf);
56288219Sdillon	isdyn = SBUF_ISDYNSTRUCT(s);
56369990Sdes	bzero(s, sizeof *s);
56488219Sdillon	if (isdyn)
56577989Sdes		SBFREE(s);
56669990Sdes}
567104449Sphk
568104449Sphk/*
569104449Sphk * Check if an sbuf has been finished.
570104449Sphk */
571104449Sphkint
572104449Sphksbuf_done(struct sbuf *s)
573104449Sphk{
574104449Sphk
575104449Sphk	return(SBUF_ISFINISHED(s));
576104449Sphk}
577