]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_sbuf.c
ident(1): Normalizing date format
[FreeBSD/FreeBSD.git] / sys / kern / subr_sbuf.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000-2008 Poul-Henning Kamp
5  * Copyright (c) 2000-2008 Dag-Erling Coïdan Smørgrav
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35
36 #ifdef _KERNEL
37 #include <sys/ctype.h>
38 #include <sys/errno.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/systm.h>
43 #include <sys/uio.h>
44 #include <machine/stdarg.h>
45 #else /* _KERNEL */
46 #include <ctype.h>
47 #include <errno.h>
48 #include <limits.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #endif /* _KERNEL */
54
55 #include <sys/sbuf.h>
56
57 #ifdef _KERNEL
58 static MALLOC_DEFINE(M_SBUF, "sbuf", "string buffers");
59 #define SBMALLOC(size, flags)   malloc(size, M_SBUF, (flags) | M_ZERO)
60 #define SBFREE(buf)             free(buf, M_SBUF)
61 #else /* _KERNEL */
62 #define KASSERT(e, m)
63 #define SBMALLOC(size, flags)   calloc(1, size)
64 #define SBFREE(buf)             free(buf)
65 #endif /* _KERNEL */
66
67 /*
68  * Predicates
69  */
70 #define SBUF_ISDYNAMIC(s)       ((s)->s_flags & SBUF_DYNAMIC)
71 #define SBUF_ISDYNSTRUCT(s)     ((s)->s_flags & SBUF_DYNSTRUCT)
72 #define SBUF_ISFINISHED(s)      ((s)->s_flags & SBUF_FINISHED)
73 #define SBUF_ISDRAINATEOL(s)    ((s)->s_flags & SBUF_DRAINATEOL)
74 #define SBUF_HASROOM(s)         ((s)->s_len < (s)->s_size - 1)
75 #define SBUF_FREESPACE(s)       ((s)->s_size - ((s)->s_len + 1))
76 #define SBUF_CANEXTEND(s)       ((s)->s_flags & SBUF_AUTOEXTEND)
77 #define SBUF_ISSECTION(s)       ((s)->s_flags & SBUF_INSECTION)
78 #define SBUF_NULINCLUDED(s)     ((s)->s_flags & SBUF_INCLUDENUL)
79 #define SBUF_ISDRAINTOEOR(s)    ((s)->s_flags & SBUF_DRAINTOEOR)
80 #define SBUF_DODRAINTOEOR(s)    (SBUF_ISSECTION(s) && SBUF_ISDRAINTOEOR(s))
81 #define SBUF_MALLOCFLAG(s)      \
82         (((s)->s_flags & SBUF_NOWAIT) ? M_NOWAIT : M_WAITOK)
83
84 /*
85  * Set / clear flags
86  */
87 #define SBUF_SETFLAG(s, f)      do { (s)->s_flags |= (f); } while (0)
88 #define SBUF_CLEARFLAG(s, f)    do { (s)->s_flags &= ~(f); } while (0)
89
90 #define SBUF_MINSIZE             2              /* Min is 1 byte + nulterm. */
91 #define SBUF_MINEXTENDSIZE      16              /* Should be power of 2. */
92
93 #ifdef PAGE_SIZE
94 #define SBUF_MAXEXTENDSIZE      PAGE_SIZE
95 #define SBUF_MAXEXTENDINCR      PAGE_SIZE
96 #else
97 #define SBUF_MAXEXTENDSIZE      4096
98 #define SBUF_MAXEXTENDINCR      4096
99 #endif
100
101 /*
102  * Debugging support
103  */
104 #if defined(_KERNEL) && defined(INVARIANTS)
105
106 static void
107 _assert_sbuf_integrity(const char *fun, struct sbuf *s)
108 {
109
110         KASSERT(s != NULL,
111             ("%s called with a NULL sbuf pointer", fun));
112         KASSERT(s->s_buf != NULL,
113             ("%s called with uninitialized or corrupt sbuf", fun));
114         if (SBUF_ISFINISHED(s) && SBUF_NULINCLUDED(s)) {
115                 KASSERT(s->s_len <= s->s_size,
116                     ("wrote past end of sbuf (%jd >= %jd)",
117                     (intmax_t)s->s_len, (intmax_t)s->s_size));
118         } else {
119                 KASSERT(s->s_len < s->s_size,
120                     ("wrote past end of sbuf (%jd >= %jd)",
121                     (intmax_t)s->s_len, (intmax_t)s->s_size));
122         }
123 }
124
125 static void
126 _assert_sbuf_state(const char *fun, struct sbuf *s, int state)
127 {
128
129         KASSERT((s->s_flags & SBUF_FINISHED) == state,
130             ("%s called with %sfinished or corrupt sbuf", fun,
131             (state ? "un" : "")));
132 }
133
134 #define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s))
135 #define assert_sbuf_state(s, i)  _assert_sbuf_state(__func__, (s), (i))
136
137 #else /* _KERNEL && INVARIANTS */
138
139 #define assert_sbuf_integrity(s) do { } while (0)
140 #define assert_sbuf_state(s, i)  do { } while (0)
141
142 #endif /* _KERNEL && INVARIANTS */
143
144 #ifdef CTASSERT
145 CTASSERT(powerof2(SBUF_MAXEXTENDSIZE));
146 CTASSERT(powerof2(SBUF_MAXEXTENDINCR));
147 #endif
148
149 static int
150 sbuf_extendsize(int size)
151 {
152         int newsize;
153
154         if (size < (int)SBUF_MAXEXTENDSIZE) {
155                 newsize = SBUF_MINEXTENDSIZE;
156                 while (newsize < size)
157                         newsize *= 2;
158         } else {
159                 newsize = roundup2(size, SBUF_MAXEXTENDINCR);
160         }
161         KASSERT(newsize >= size, ("%s: %d < %d\n", __func__, newsize, size));
162         return (newsize);
163 }
164
165 /*
166  * Extend an sbuf.
167  */
168 static int
169 sbuf_extend(struct sbuf *s, int addlen)
170 {
171         char *newbuf;
172         int newsize;
173
174         if (!SBUF_CANEXTEND(s))
175                 return (-1);
176         newsize = sbuf_extendsize(s->s_size + addlen);
177         newbuf = SBMALLOC(newsize, SBUF_MALLOCFLAG(s));
178         if (newbuf == NULL)
179                 return (-1);
180         memcpy(newbuf, s->s_buf, s->s_size);
181         if (SBUF_ISDYNAMIC(s))
182                 SBFREE(s->s_buf);
183         else
184                 SBUF_SETFLAG(s, SBUF_DYNAMIC);
185         s->s_buf = newbuf;
186         s->s_size = newsize;
187         return (0);
188 }
189
190 /*
191  * Initialize an sbuf.
192  * If buf is non-NULL, it points to a static or already-allocated string
193  * big enough to hold at least length characters.
194  */
195 struct sbuf *
196 sbuf_new(struct sbuf *s, char *buf, int length, int flags)
197 {
198
199         KASSERT(length >= 0,
200             ("attempt to create an sbuf of negative length (%d)", length));
201         KASSERT((flags & ~SBUF_USRFLAGMSK) == 0,
202             ("%s called with invalid flags", __func__));
203         KASSERT((flags & SBUF_AUTOEXTEND) || length >= SBUF_MINSIZE,
204             ("sbuf buffer %d smaller than minimum %d bytes", length,
205             SBUF_MINSIZE));
206
207         flags &= SBUF_USRFLAGMSK;
208
209         /*
210          * Allocate 'DYNSTRUCT' sbuf from the heap, if NULL 's' was provided.
211          */
212         if (s == NULL) {
213                 s = SBMALLOC(sizeof(*s),
214                     (flags & SBUF_NOWAIT) ?  M_NOWAIT : M_WAITOK);
215                 if (s == NULL)
216                         goto out;
217                 SBUF_SETFLAG(s, SBUF_DYNSTRUCT);
218         } else {
219                 /*
220                  * DYNSTRUCT SBMALLOC sbufs are allocated with M_ZERO, but
221                  * user-provided sbuf objects must be initialized.
222                  */
223                 memset(s, 0, sizeof(*s));
224         }
225
226         s->s_flags |= flags;
227         s->s_size = length;
228         s->s_buf = buf;
229         /*
230          * Never-written sbufs do not need \n termination.
231          */
232         SBUF_SETFLAG(s, SBUF_DRAINATEOL);
233
234         /*
235          * Allocate DYNAMIC, i.e., heap data buffer backing the sbuf, if no
236          * buffer was provided.
237          */
238         if (s->s_buf == NULL) {
239                 if (SBUF_CANEXTEND(s))
240                         s->s_size = sbuf_extendsize(s->s_size);
241                 s->s_buf = SBMALLOC(s->s_size, SBUF_MALLOCFLAG(s));
242                 if (s->s_buf == NULL)
243                         goto out;
244                 SBUF_SETFLAG(s, SBUF_DYNAMIC);
245         }
246
247 out:
248         if (s != NULL && s->s_buf == NULL) {
249                 if (SBUF_ISDYNSTRUCT(s))
250                         SBFREE(s);
251                 s = NULL;
252         }
253         return (s);
254 }
255
256 #ifdef _KERNEL
257 /*
258  * Create an sbuf with uio data
259  */
260 struct sbuf *
261 sbuf_uionew(struct sbuf *s, struct uio *uio, int *error)
262 {
263
264         KASSERT(uio != NULL,
265             ("%s called with NULL uio pointer", __func__));
266         KASSERT(error != NULL,
267             ("%s called with NULL error pointer", __func__));
268
269         s = sbuf_new(s, NULL, uio->uio_resid + 1, 0);
270         if (s == NULL) {
271                 *error = ENOMEM;
272                 return (NULL);
273         }
274         *error = uiomove(s->s_buf, uio->uio_resid, uio);
275         if (*error != 0) {
276                 sbuf_delete(s);
277                 return (NULL);
278         }
279         s->s_len = s->s_size - 1;
280         if (SBUF_ISSECTION(s))
281                 s->s_sect_len = s->s_size - 1;
282         *error = 0;
283         return (s);
284 }
285 #endif
286
287 int
288 sbuf_get_flags(struct sbuf *s)
289 {
290
291         return (s->s_flags & SBUF_USRFLAGMSK);
292 }
293
294 void
295 sbuf_clear_flags(struct sbuf *s, int flags)
296 {
297
298         s->s_flags &= ~(flags & SBUF_USRFLAGMSK);
299 }
300
301 void
302 sbuf_set_flags(struct sbuf *s, int flags)
303 {
304
305         s->s_flags |= (flags & SBUF_USRFLAGMSK);
306 }
307
308 /*
309  * Clear an sbuf and reset its position.
310  */
311 void
312 sbuf_clear(struct sbuf *s)
313 {
314
315         assert_sbuf_integrity(s);
316         /* don't care if it's finished or not */
317         KASSERT(s->s_drain_func == NULL,
318             ("%s makes no sense on sbuf %p with drain", __func__, s));
319
320         SBUF_CLEARFLAG(s, SBUF_FINISHED);
321         s->s_error = 0;
322         s->s_len = 0;
323         s->s_rec_off = 0;
324         s->s_sect_len = 0;
325 }
326
327 /*
328  * Set the sbuf's end position to an arbitrary value.
329  * Effectively truncates the sbuf at the new position.
330  */
331 int
332 sbuf_setpos(struct sbuf *s, ssize_t pos)
333 {
334
335         assert_sbuf_integrity(s);
336         assert_sbuf_state(s, 0);
337
338         KASSERT(pos >= 0,
339             ("attempt to seek to a negative position (%jd)", (intmax_t)pos));
340         KASSERT(pos < s->s_size,
341             ("attempt to seek past end of sbuf (%jd >= %jd)",
342             (intmax_t)pos, (intmax_t)s->s_size));
343         KASSERT(!SBUF_ISSECTION(s),
344             ("attempt to seek when in a section"));
345
346         if (pos < 0 || pos > s->s_len)
347                 return (-1);
348         s->s_len = pos;
349         return (0);
350 }
351
352 /*
353  * Drain into a counter.  Counts amount of data without producing output.
354  * Useful for cases like sysctl, where user may first request only size.
355  * This allows to avoid pointless allocation/freeing of large buffers.
356  */
357 int
358 sbuf_count_drain(void *arg, const char *data __unused, int len)
359 {
360         size_t *sizep;
361
362         sizep = (size_t *)arg;
363         *sizep += len;
364         return (len);
365 }
366
367 /*
368  * Set up a drain function and argument on an sbuf to flush data to
369  * when the sbuf buffer overflows.
370  */
371 void
372 sbuf_set_drain(struct sbuf *s, sbuf_drain_func *func, void *ctx)
373 {
374
375         assert_sbuf_state(s, 0);
376         assert_sbuf_integrity(s);
377         KASSERT(func == s->s_drain_func || s->s_len == 0,
378             ("Cannot change drain to %p on non-empty sbuf %p", func, s));
379         s->s_drain_func = func;
380         s->s_drain_arg = ctx;
381 }
382
383 /*
384  * Call the drain and process the return.
385  */
386 static int
387 sbuf_drain(struct sbuf *s)
388 {
389         int len;
390
391         KASSERT(s->s_len > 0, ("Shouldn't drain empty sbuf %p", s));
392         KASSERT(s->s_error == 0, ("Called %s with error on %p", __func__, s));
393
394         if (SBUF_DODRAINTOEOR(s) && s->s_rec_off == 0)
395                 return (s->s_error = EDEADLK);
396         len = s->s_drain_func(s->s_drain_arg, s->s_buf,
397             SBUF_DODRAINTOEOR(s) ? s->s_rec_off : s->s_len);
398         if (len <= 0) {
399                 s->s_error = len ? -len : EDEADLK;
400                 return (s->s_error);
401         }
402         KASSERT(len > 0 && len <= s->s_len,
403             ("Bad drain amount %d for sbuf %p", len, s));
404         s->s_len -= len;
405         s->s_rec_off -= len;
406         /*
407          * Fast path for the expected case where all the data was
408          * drained.
409          */
410         if (s->s_len == 0) {
411                 /*
412                  * When the s_buf is entirely drained, we need to remember if
413                  * the last character was a '\n' or not for
414                  * sbuf_nl_terminate().
415                  */
416                 if (s->s_buf[len - 1] == '\n')
417                         SBUF_SETFLAG(s, SBUF_DRAINATEOL);
418                 else
419                         SBUF_CLEARFLAG(s, SBUF_DRAINATEOL);
420                 return (0);
421         }
422         /*
423          * Move the remaining characters to the beginning of the
424          * string.
425          */
426         memmove(s->s_buf, s->s_buf + len, s->s_len);
427         return (0);
428 }
429
430 /*
431  * Append bytes to an sbuf.  This is the core function for appending
432  * to an sbuf and is the main place that deals with extending the
433  * buffer and marking overflow.
434  */
435 static void
436 sbuf_put_bytes(struct sbuf *s, const char *buf, size_t len)
437 {
438         size_t n;
439
440         assert_sbuf_integrity(s);
441         assert_sbuf_state(s, 0);
442
443         if (s->s_error != 0)
444                 return;
445         while (len > 0) {
446                 if (SBUF_FREESPACE(s) <= 0) {
447                         /*
448                          * If there is a drain, use it, otherwise extend the
449                          * buffer.
450                          */
451                         if (s->s_drain_func != NULL)
452                                 (void)sbuf_drain(s);
453                         else if (sbuf_extend(s, len > INT_MAX ? INT_MAX : len)
454                             < 0)
455                                 s->s_error = ENOMEM;
456                         if (s->s_error != 0)
457                                 return;
458                 }
459                 n = SBUF_FREESPACE(s);
460                 if (len < n)
461                         n = len;
462                 memcpy(&s->s_buf[s->s_len], buf, n);
463                 s->s_len += n;
464                 if (SBUF_ISSECTION(s))
465                         s->s_sect_len += n;
466                 len -= n;
467                 buf += n;
468         }
469 }
470
471 static void
472 sbuf_put_byte(struct sbuf *s, char c)
473 {
474
475         sbuf_put_bytes(s, &c, 1);
476 }
477
478 /*
479  * Append a byte string to an sbuf.
480  */
481 int
482 sbuf_bcat(struct sbuf *s, const void *buf, size_t len)
483 {
484
485         sbuf_put_bytes(s, buf, len);
486         if (s->s_error != 0)
487                 return (-1);
488         return (0);
489 }
490
491 #ifdef _KERNEL
492 /*
493  * Copy a byte string from userland into an sbuf.
494  */
495 int
496 sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len)
497 {
498
499         assert_sbuf_integrity(s);
500         assert_sbuf_state(s, 0);
501         KASSERT(s->s_drain_func == NULL,
502             ("Nonsensical copyin to sbuf %p with a drain", s));
503
504         if (s->s_error != 0)
505                 return (-1);
506         if (len == 0)
507                 return (0);
508         if (len > SBUF_FREESPACE(s)) {
509                 sbuf_extend(s, len - SBUF_FREESPACE(s));
510                 if (SBUF_FREESPACE(s) < len)
511                         len = SBUF_FREESPACE(s);
512         }
513         if (copyin(uaddr, s->s_buf + s->s_len, len) != 0)
514                 return (-1);
515         s->s_len += len;
516
517         return (0);
518 }
519 #endif
520
521 /*
522  * Copy a byte string into an sbuf.
523  */
524 int
525 sbuf_bcpy(struct sbuf *s, const void *buf, size_t len)
526 {
527
528         assert_sbuf_integrity(s);
529         assert_sbuf_state(s, 0);
530
531         sbuf_clear(s);
532         return (sbuf_bcat(s, buf, len));
533 }
534
535 /*
536  * Append a string to an sbuf.
537  */
538 int
539 sbuf_cat(struct sbuf *s, const char *str)
540 {
541         size_t n;
542
543         n = strlen(str);
544         sbuf_put_bytes(s, str, n);
545         if (s->s_error != 0)
546                 return (-1);
547         return (0);
548 }
549
550 #ifdef _KERNEL
551 /*
552  * Append a string from userland to an sbuf.
553  */
554 int
555 sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len)
556 {
557         size_t done;
558
559         assert_sbuf_integrity(s);
560         assert_sbuf_state(s, 0);
561         KASSERT(s->s_drain_func == NULL,
562             ("Nonsensical copyin to sbuf %p with a drain", s));
563
564         if (s->s_error != 0)
565                 return (-1);
566
567         if (len == 0)
568                 len = SBUF_FREESPACE(s);        /* XXX return 0? */
569         if (len > SBUF_FREESPACE(s)) {
570                 sbuf_extend(s, len);
571                 if (SBUF_FREESPACE(s) < len)
572                         len = SBUF_FREESPACE(s);
573         }
574         switch (copyinstr(uaddr, s->s_buf + s->s_len, len + 1, &done)) {
575         case ENAMETOOLONG:
576                 s->s_error = ENOMEM;
577                 /* fall through */
578         case 0:
579                 s->s_len += done - 1;
580                 if (SBUF_ISSECTION(s))
581                         s->s_sect_len += done - 1;
582                 break;
583         default:
584                 return (-1);    /* XXX */
585         }
586
587         return (done);
588 }
589 #endif
590
591 /*
592  * Copy a string into an sbuf.
593  */
594 int
595 sbuf_cpy(struct sbuf *s, const char *str)
596 {
597
598         assert_sbuf_integrity(s);
599         assert_sbuf_state(s, 0);
600
601         sbuf_clear(s);
602         return (sbuf_cat(s, str));
603 }
604
605 /*
606  * Format the given argument list and append the resulting string to an sbuf.
607  */
608 #ifdef _KERNEL
609
610 /*
611  * Append a non-NUL character to an sbuf.  This prototype signature is
612  * suitable for use with kvprintf(9).
613  */
614 static void
615 sbuf_putc_func(int c, void *arg)
616 {
617
618         if (c != '\0')
619                 sbuf_put_byte(arg, c);
620 }
621
622 int
623 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
624 {
625
626         assert_sbuf_integrity(s);
627         assert_sbuf_state(s, 0);
628
629         KASSERT(fmt != NULL,
630             ("%s called with a NULL format string", __func__));
631
632         (void)kvprintf(fmt, sbuf_putc_func, s, 10, ap);
633         if (s->s_error != 0)
634                 return (-1);
635         return (0);
636 }
637 #else /* !_KERNEL */
638 int
639 sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap)
640 {
641         va_list ap_copy;
642         int error, len;
643
644         assert_sbuf_integrity(s);
645         assert_sbuf_state(s, 0);
646
647         KASSERT(fmt != NULL,
648             ("%s called with a NULL format string", __func__));
649
650         if (s->s_error != 0)
651                 return (-1);
652
653         /*
654          * For the moment, there is no way to get vsnprintf(3) to hand
655          * back a character at a time, to push everything into
656          * sbuf_putc_func() as was done for the kernel.
657          *
658          * In userspace, while drains are useful, there's generally
659          * not a problem attempting to malloc(3) on out of space.  So
660          * expand a userland sbuf if there is not enough room for the
661          * data produced by sbuf_[v]printf(3).
662          */
663
664         error = 0;
665         do {
666                 va_copy(ap_copy, ap);
667                 len = vsnprintf(&s->s_buf[s->s_len], SBUF_FREESPACE(s) + 1,
668                     fmt, ap_copy);
669                 if (len < 0) {
670                         s->s_error = errno;
671                         return (-1);
672                 }
673                 va_end(ap_copy);
674
675                 if (SBUF_FREESPACE(s) >= len)
676                         break;
677                 /* Cannot print with the current available space. */
678                 if (s->s_drain_func != NULL && s->s_len > 0)
679                         error = sbuf_drain(s); /* sbuf_drain() sets s_error. */
680                 else if (sbuf_extend(s, len - SBUF_FREESPACE(s)) != 0)
681                         s->s_error = error = ENOMEM;
682         } while (error == 0);
683
684         /*
685          * s->s_len is the length of the string, without the terminating nul.
686          * When updating s->s_len, we must subtract 1 from the length that
687          * we passed into vsnprintf() because that length includes the
688          * terminating nul.
689          *
690          * vsnprintf() returns the amount that would have been copied,
691          * given sufficient space, so don't over-increment s_len.
692          */
693         if (SBUF_FREESPACE(s) < len)
694                 len = SBUF_FREESPACE(s);
695         s->s_len += len;
696         if (SBUF_ISSECTION(s))
697                 s->s_sect_len += len;
698
699         KASSERT(s->s_len < s->s_size,
700             ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size));
701
702         if (s->s_error != 0)
703                 return (-1);
704         return (0);
705 }
706 #endif /* _KERNEL */
707
708 /*
709  * Format the given arguments and append the resulting string to an sbuf.
710  */
711 int
712 sbuf_printf(struct sbuf *s, const char *fmt, ...)
713 {
714         va_list ap;
715         int result;
716
717         va_start(ap, fmt);
718         result = sbuf_vprintf(s, fmt, ap);
719         va_end(ap);
720         return (result);
721 }
722
723 /*
724  * Append a character to an sbuf.
725  */
726 int
727 sbuf_putc(struct sbuf *s, int c)
728 {
729
730         sbuf_put_byte(s, c);
731         if (s->s_error != 0)
732                 return (-1);
733         return (0);
734 }
735
736 /*
737  * Append a trailing newline to a non-empty sbuf, if one is not already
738  * present.  Handles sbufs with drain functions correctly.
739  */
740 int
741 sbuf_nl_terminate(struct sbuf *s)
742 {
743
744         assert_sbuf_integrity(s);
745         assert_sbuf_state(s, 0);
746
747         /*
748          * If the s_buf isn't empty, the last byte is simply s_buf[s_len - 1].
749          *
750          * If the s_buf is empty because a drain function drained it, we
751          * remember if the last byte was a \n with the SBUF_DRAINATEOL flag in
752          * sbuf_drain().
753          *
754          * In either case, we only append a \n if the previous character was
755          * something else.
756          */
757         if (s->s_len == 0) {
758                 if (!SBUF_ISDRAINATEOL(s))
759                         sbuf_put_byte(s, '\n');
760         } else if (s->s_buf[s->s_len - 1] != '\n')
761                 sbuf_put_byte(s, '\n');
762
763         if (s->s_error != 0)
764                 return (-1);
765         return (0);
766 }
767
768 /*
769  * Trim whitespace characters from end of an sbuf.
770  */
771 int
772 sbuf_trim(struct sbuf *s)
773 {
774
775         assert_sbuf_integrity(s);
776         assert_sbuf_state(s, 0);
777         KASSERT(s->s_drain_func == NULL,
778             ("%s makes no sense on sbuf %p with drain", __func__, s));
779
780         if (s->s_error != 0)
781                 return (-1);
782
783         while (s->s_len > 0 && isspace(s->s_buf[s->s_len-1])) {
784                 --s->s_len;
785                 if (SBUF_ISSECTION(s))
786                         s->s_sect_len--;
787         }
788
789         return (0);
790 }
791
792 /*
793  * Check if an sbuf has an error.
794  */
795 int
796 sbuf_error(const struct sbuf *s)
797 {
798
799         return (s->s_error);
800 }
801
802 /*
803  * Finish off an sbuf.
804  */
805 int
806 sbuf_finish(struct sbuf *s)
807 {
808
809         assert_sbuf_integrity(s);
810         assert_sbuf_state(s, 0);
811
812         s->s_buf[s->s_len] = '\0';
813         if (SBUF_NULINCLUDED(s))
814                 s->s_len++;
815         if (s->s_drain_func != NULL) {
816                 while (s->s_len > 0 && s->s_error == 0)
817                         s->s_error = sbuf_drain(s);
818         }
819         SBUF_SETFLAG(s, SBUF_FINISHED);
820 #ifdef _KERNEL
821         return (s->s_error);
822 #else
823         if (s->s_error != 0) {
824                 errno = s->s_error;
825                 return (-1);
826         }
827         return (0);
828 #endif
829 }
830
831 /*
832  * Return a pointer to the sbuf data.
833  */
834 char *
835 sbuf_data(struct sbuf *s)
836 {
837
838         assert_sbuf_integrity(s);
839         assert_sbuf_state(s, SBUF_FINISHED);
840         KASSERT(s->s_drain_func == NULL,
841             ("%s makes no sense on sbuf %p with drain", __func__, s));
842
843         return (s->s_buf);
844 }
845
846 /*
847  * Return the length of the sbuf data.
848  */
849 ssize_t
850 sbuf_len(struct sbuf *s)
851 {
852
853         assert_sbuf_integrity(s);
854         /* don't care if it's finished or not */
855         KASSERT(s->s_drain_func == NULL,
856             ("%s makes no sense on sbuf %p with drain", __func__, s));
857
858         if (s->s_error != 0)
859                 return (-1);
860
861         /* If finished, nulterm is already in len, else add one. */
862         if (SBUF_NULINCLUDED(s) && !SBUF_ISFINISHED(s))
863                 return (s->s_len + 1);
864         return (s->s_len);
865 }
866
867 /*
868  * Clear an sbuf, free its buffer if necessary.
869  */
870 void
871 sbuf_delete(struct sbuf *s)
872 {
873         int isdyn;
874
875         assert_sbuf_integrity(s);
876         /* don't care if it's finished or not */
877
878         if (SBUF_ISDYNAMIC(s))
879                 SBFREE(s->s_buf);
880         isdyn = SBUF_ISDYNSTRUCT(s);
881         memset(s, 0, sizeof(*s));
882         if (isdyn)
883                 SBFREE(s);
884 }
885
886 /*
887  * Check if an sbuf has been finished.
888  */
889 int
890 sbuf_done(const struct sbuf *s)
891 {
892
893         return (SBUF_ISFINISHED(s));
894 }
895
896 /*
897  * Start a section.
898  */
899 void
900 sbuf_start_section(struct sbuf *s, ssize_t *old_lenp)
901 {
902
903         assert_sbuf_integrity(s);
904         assert_sbuf_state(s, 0);
905
906         if (!SBUF_ISSECTION(s)) {
907                 KASSERT(s->s_sect_len == 0,
908                     ("s_sect_len != 0 when starting a section"));
909                 if (old_lenp != NULL)
910                         *old_lenp = -1;
911                 s->s_rec_off = s->s_len;
912                 SBUF_SETFLAG(s, SBUF_INSECTION);
913         } else {
914                 KASSERT(old_lenp != NULL,
915                     ("s_sect_len should be saved when starting a subsection"));
916                 *old_lenp = s->s_sect_len;
917                 s->s_sect_len = 0;
918         }
919 }
920
921 /*
922  * End the section padding to the specified length with the specified
923  * character.
924  */
925 ssize_t
926 sbuf_end_section(struct sbuf *s, ssize_t old_len, size_t pad, int c)
927 {
928         ssize_t len;
929
930         assert_sbuf_integrity(s);
931         assert_sbuf_state(s, 0);
932         KASSERT(SBUF_ISSECTION(s),
933             ("attempt to end a section when not in a section"));
934
935         if (pad > 1) {
936                 len = roundup(s->s_sect_len, pad) - s->s_sect_len;
937                 for (; s->s_error == 0 && len > 0; len--)
938                         sbuf_put_byte(s, c);
939         }
940         len = s->s_sect_len;
941         if (old_len == -1) {
942                 s->s_rec_off = s->s_sect_len = 0;
943                 SBUF_CLEARFLAG(s, SBUF_INSECTION);
944         } else {
945                 s->s_sect_len += old_len;
946         }
947         if (s->s_error != 0)
948                 return (-1);
949         return (len);
950 }