* Call the drain and process the return. */
| 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 |
no test coverage detected