* Append a trailing newline to a non-empty sbuf, if one is not already * present. Handles sbufs with drain functions correctly. */
| 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. |
no test coverage detected