MCPcopy Create free account
hub / github.com/F-Stack/f-stack / sbuf_nl_terminate

Function sbuf_nl_terminate

freebsd/kern/subr_sbuf.c:740–766  ·  view source on GitHub ↗

* Append a trailing newline to a non-empty sbuf, if one is not already * present. Handles sbufs with drain functions correctly. */

Source from the content-addressed store, hash-verified

738 * present. Handles sbufs with drain functions correctly.
739 */
740int
741sbuf_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.

Callers 1

stack_sbuf_print_flagsFunction · 0.85

Calls 1

sbuf_put_byteFunction · 0.85

Tested by

no test coverage detected