subr_sbuf.c (153678) | subr_sbuf.c (181462) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 2000 Poul-Henning Kamp and Dag-Erling Co�dan Sm�rgrav | 2 * Copyright (c) 2000-2008 Poul-Henning Kamp 3 * Copyright (c) 2000-2008 Dag-Erling Co��dan Sm��rgrav |
3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. | 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. |
14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. | |
16 * | 15 * |
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. |
27 */ 28 29#include <sys/cdefs.h> | 27 */ 28 29#include <sys/cdefs.h> |
30__FBSDID("$FreeBSD: head/sys/kern/subr_sbuf.c 153678 2005-12-23 11:49:53Z phk $"); | 30__FBSDID("$FreeBSD: head/sys/kern/subr_sbuf.c 181462 2008-08-09 10:26:21Z des $"); |
31 32#include <sys/param.h> 33 34#ifdef _KERNEL 35#include <sys/ctype.h> 36#include <sys/kernel.h> 37#include <sys/malloc.h> 38#include <sys/systm.h> --- 40 unchanged lines hidden (view full) --- 79#define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */ 80#define SBUF_MAXEXTENDSIZE PAGE_SIZE 81#define SBUF_MAXEXTENDINCR PAGE_SIZE 82 83/* 84 * Debugging support 85 */ 86#if defined(_KERNEL) && defined(INVARIANTS) | 31 32#include <sys/param.h> 33 34#ifdef _KERNEL 35#include <sys/ctype.h> 36#include <sys/kernel.h> 37#include <sys/malloc.h> 38#include <sys/systm.h> --- 40 unchanged lines hidden (view full) --- 79#define SBUF_MINEXTENDSIZE 16 /* Should be power of 2. */ 80#define SBUF_MAXEXTENDSIZE PAGE_SIZE 81#define SBUF_MAXEXTENDINCR PAGE_SIZE 82 83/* 84 * Debugging support 85 */ 86#if defined(_KERNEL) && defined(INVARIANTS) |
87 |
|
87static void 88_assert_sbuf_integrity(const char *fun, struct sbuf *s) 89{ | 88static void 89_assert_sbuf_integrity(const char *fun, struct sbuf *s) 90{ |
91 |
|
90 KASSERT(s != NULL, 91 ("%s called with a NULL sbuf pointer", fun)); 92 KASSERT(s->s_buf != NULL, 93 ("%s called with uninitialized or corrupt sbuf", fun)); 94 KASSERT(s->s_len < s->s_size, 95 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size)); 96} 97 98static void 99_assert_sbuf_state(const char *fun, struct sbuf *s, int state) 100{ | 92 KASSERT(s != NULL, 93 ("%s called with a NULL sbuf pointer", fun)); 94 KASSERT(s->s_buf != NULL, 95 ("%s called with uninitialized or corrupt sbuf", fun)); 96 KASSERT(s->s_len < s->s_size, 97 ("wrote past end of sbuf (%d >= %d)", s->s_len, s->s_size)); 98} 99 100static void 101_assert_sbuf_state(const char *fun, struct sbuf *s, int state) 102{ |
103 |
|
101 KASSERT((s->s_flags & SBUF_FINISHED) == state, 102 ("%s called with %sfinished or corrupt sbuf", fun, 103 (state ? "un" : ""))); 104} | 104 KASSERT((s->s_flags & SBUF_FINISHED) == state, 105 ("%s called with %sfinished or corrupt sbuf", fun, 106 (state ? "un" : ""))); 107} |
108 |
|
105#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s)) 106#define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i)) | 109#define assert_sbuf_integrity(s) _assert_sbuf_integrity(__func__, (s)) 110#define assert_sbuf_state(s, i) _assert_sbuf_state(__func__, (s), (i)) |
111 |
|
107#else /* _KERNEL && INVARIANTS */ | 112#else /* _KERNEL && INVARIANTS */ |
113 |
|
108#define assert_sbuf_integrity(s) do { } while (0) 109#define assert_sbuf_state(s, i) do { } while (0) | 114#define assert_sbuf_integrity(s) do { } while (0) 115#define assert_sbuf_state(s, i) do { } while (0) |
116 |
|
110#endif /* _KERNEL && INVARIANTS */ 111 112static int 113sbuf_extendsize(int size) 114{ 115 int newsize; 116 117 newsize = SBUF_MINEXTENDSIZE; 118 while (newsize < size) { 119 if (newsize < (int)SBUF_MAXEXTENDSIZE) 120 newsize *= 2; 121 else 122 newsize += SBUF_MAXEXTENDINCR; 123 } | 117#endif /* _KERNEL && INVARIANTS */ 118 119static int 120sbuf_extendsize(int size) 121{ 122 int newsize; 123 124 newsize = SBUF_MINEXTENDSIZE; 125 while (newsize < size) { 126 if (newsize < (int)SBUF_MAXEXTENDSIZE) 127 newsize *= 2; 128 else 129 newsize += SBUF_MAXEXTENDINCR; 130 } |
124 | |
125 return (newsize); 126} 127 128 129/* 130 * Extend an sbuf. 131 */ 132static int 133sbuf_extend(struct sbuf *s, int addlen) 134{ 135 char *newbuf; 136 int newsize; 137 138 if (!SBUF_CANEXTEND(s)) 139 return (-1); | 131 return (newsize); 132} 133 134 135/* 136 * Extend an sbuf. 137 */ 138static int 139sbuf_extend(struct sbuf *s, int addlen) 140{ 141 char *newbuf; 142 int newsize; 143 144 if (!SBUF_CANEXTEND(s)) 145 return (-1); |
140 | |
141 newsize = sbuf_extendsize(s->s_size + addlen); | 146 newsize = sbuf_extendsize(s->s_size + addlen); |
142 newbuf = (char *)SBMALLOC(newsize); | 147 newbuf = SBMALLOC(newsize); |
143 if (newbuf == NULL) 144 return (-1); 145 bcopy(s->s_buf, newbuf, s->s_size); 146 if (SBUF_ISDYNAMIC(s)) 147 SBFREE(s->s_buf); 148 else 149 SBUF_SETFLAG(s, SBUF_DYNAMIC); 150 s->s_buf = newbuf; --- 4 unchanged lines hidden (view full) --- 155/* 156 * Initialize an sbuf. 157 * If buf is non-NULL, it points to a static or already-allocated string 158 * big enough to hold at least length characters. 159 */ 160struct sbuf * 161sbuf_new(struct sbuf *s, char *buf, int length, int flags) 162{ | 148 if (newbuf == NULL) 149 return (-1); 150 bcopy(s->s_buf, newbuf, s->s_size); 151 if (SBUF_ISDYNAMIC(s)) 152 SBFREE(s->s_buf); 153 else 154 SBUF_SETFLAG(s, SBUF_DYNAMIC); 155 s->s_buf = newbuf; --- 4 unchanged lines hidden (view full) --- 160/* 161 * Initialize an sbuf. 162 * If buf is non-NULL, it points to a static or already-allocated string 163 * big enough to hold at least length characters. 164 */ 165struct sbuf * 166sbuf_new(struct sbuf *s, char *buf, int length, int flags) 167{ |
168 |
|
163 KASSERT(length >= 0, 164 ("attempt to create an sbuf of negative length (%d)", length)); 165 KASSERT((flags & ~SBUF_USRFLAGMSK) == 0, 166 ("%s called with invalid flags", __func__)); 167 168 flags &= SBUF_USRFLAGMSK; 169 if (s == NULL) { | 169 KASSERT(length >= 0, 170 ("attempt to create an sbuf of negative length (%d)", length)); 171 KASSERT((flags & ~SBUF_USRFLAGMSK) == 0, 172 ("%s called with invalid flags", __func__)); 173 174 flags &= SBUF_USRFLAGMSK; 175 if (s == NULL) { |
170 s = (struct sbuf *)SBMALLOC(sizeof *s); | 176 s = SBMALLOC(sizeof(*s)); |
171 if (s == NULL) 172 return (NULL); | 177 if (s == NULL) 178 return (NULL); |
173 bzero(s, sizeof *s); | 179 bzero(s, sizeof(*s)); |
174 s->s_flags = flags; 175 SBUF_SETFLAG(s, SBUF_DYNSTRUCT); 176 } else { | 180 s->s_flags = flags; 181 SBUF_SETFLAG(s, SBUF_DYNSTRUCT); 182 } else { |
177 bzero(s, sizeof *s); | 183 bzero(s, sizeof(*s)); |
178 s->s_flags = flags; 179 } 180 s->s_size = length; 181 if (buf) { 182 s->s_buf = buf; 183 return (s); 184 } 185 if (flags & SBUF_AUTOEXTEND) 186 s->s_size = sbuf_extendsize(s->s_size); | 184 s->s_flags = flags; 185 } 186 s->s_size = length; 187 if (buf) { 188 s->s_buf = buf; 189 return (s); 190 } 191 if (flags & SBUF_AUTOEXTEND) 192 s->s_size = sbuf_extendsize(s->s_size); |
187 s->s_buf = (char *)SBMALLOC(s->s_size); | 193 s->s_buf = SBMALLOC(s->s_size); |
188 if (s->s_buf == NULL) { 189 if (SBUF_ISDYNSTRUCT(s)) 190 SBFREE(s); 191 return (NULL); 192 } 193 SBUF_SETFLAG(s, SBUF_DYNAMIC); 194 return (s); 195} 196 197#ifdef _KERNEL 198/* 199 * Create an sbuf with uio data 200 */ 201struct sbuf * 202sbuf_uionew(struct sbuf *s, struct uio *uio, int *error) 203{ | 194 if (s->s_buf == NULL) { 195 if (SBUF_ISDYNSTRUCT(s)) 196 SBFREE(s); 197 return (NULL); 198 } 199 SBUF_SETFLAG(s, SBUF_DYNAMIC); 200 return (s); 201} 202 203#ifdef _KERNEL 204/* 205 * Create an sbuf with uio data 206 */ 207struct sbuf * 208sbuf_uionew(struct sbuf *s, struct uio *uio, int *error) 209{ |
210 |
|
204 KASSERT(uio != NULL, 205 ("%s called with NULL uio pointer", __func__)); 206 KASSERT(error != NULL, 207 ("%s called with NULL error pointer", __func__)); 208 209 s = sbuf_new(s, NULL, uio->uio_resid + 1, 0); 210 if (s == NULL) { 211 *error = ENOMEM; --- 11 unchanged lines hidden (view full) --- 223#endif 224 225/* 226 * Clear an sbuf and reset its position. 227 */ 228void 229sbuf_clear(struct sbuf *s) 230{ | 211 KASSERT(uio != NULL, 212 ("%s called with NULL uio pointer", __func__)); 213 KASSERT(error != NULL, 214 ("%s called with NULL error pointer", __func__)); 215 216 s = sbuf_new(s, NULL, uio->uio_resid + 1, 0); 217 if (s == NULL) { 218 *error = ENOMEM; --- 11 unchanged lines hidden (view full) --- 230#endif 231 232/* 233 * Clear an sbuf and reset its position. 234 */ 235void 236sbuf_clear(struct sbuf *s) 237{ |
238 |
|
231 assert_sbuf_integrity(s); 232 /* don't care if it's finished or not */ 233 234 SBUF_CLEARFLAG(s, SBUF_FINISHED); 235 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 236 s->s_len = 0; 237} 238 239/* 240 * Set the sbuf's end position to an arbitrary value. 241 * Effectively truncates the sbuf at the new position. 242 */ 243int 244sbuf_setpos(struct sbuf *s, int pos) 245{ | 239 assert_sbuf_integrity(s); 240 /* don't care if it's finished or not */ 241 242 SBUF_CLEARFLAG(s, SBUF_FINISHED); 243 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 244 s->s_len = 0; 245} 246 247/* 248 * Set the sbuf's end position to an arbitrary value. 249 * Effectively truncates the sbuf at the new position. 250 */ 251int 252sbuf_setpos(struct sbuf *s, int pos) 253{ |
254 |
|
246 assert_sbuf_integrity(s); 247 assert_sbuf_state(s, 0); 248 249 KASSERT(pos >= 0, 250 ("attempt to seek to a negative position (%d)", pos)); 251 KASSERT(pos < s->s_size, 252 ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size)); 253 --- 11 unchanged lines hidden (view full) --- 265{ 266 const char *str = buf; 267 268 assert_sbuf_integrity(s); 269 assert_sbuf_state(s, 0); 270 271 if (SBUF_HASOVERFLOWED(s)) 272 return (-1); | 255 assert_sbuf_integrity(s); 256 assert_sbuf_state(s, 0); 257 258 KASSERT(pos >= 0, 259 ("attempt to seek to a negative position (%d)", pos)); 260 KASSERT(pos < s->s_size, 261 ("attempt to seek past end of sbuf (%d >= %d)", pos, s->s_size)); 262 --- 11 unchanged lines hidden (view full) --- 274{ 275 const char *str = buf; 276 277 assert_sbuf_integrity(s); 278 assert_sbuf_state(s, 0); 279 280 if (SBUF_HASOVERFLOWED(s)) 281 return (-1); |
273 | |
274 for (; len; len--) { 275 if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0) 276 break; 277 s->s_buf[s->s_len++] = *str++; 278 } 279 if (len) { 280 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 281 return (-1); 282 } 283 return (0); 284} 285 286#ifdef _KERNEL 287/* 288 * Copy a byte string from userland into an sbuf. 289 */ 290int 291sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len) 292{ | 282 for (; len; len--) { 283 if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0) 284 break; 285 s->s_buf[s->s_len++] = *str++; 286 } 287 if (len) { 288 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 289 return (-1); 290 } 291 return (0); 292} 293 294#ifdef _KERNEL 295/* 296 * Copy a byte string from userland into an sbuf. 297 */ 298int 299sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len) 300{ |
301 |
|
293 assert_sbuf_integrity(s); 294 assert_sbuf_state(s, 0); 295 296 if (SBUF_HASOVERFLOWED(s)) 297 return (-1); | 302 assert_sbuf_integrity(s); 303 assert_sbuf_state(s, 0); 304 305 if (SBUF_HASOVERFLOWED(s)) 306 return (-1); |
298 | |
299 if (len == 0) 300 return (0); 301 if (len > SBUF_FREESPACE(s)) { 302 sbuf_extend(s, len - SBUF_FREESPACE(s)); 303 len = min(len, SBUF_FREESPACE(s)); 304 } 305 if (copyin(uaddr, s->s_buf + s->s_len, len) != 0) 306 return (-1); --- 4 unchanged lines hidden (view full) --- 311#endif 312 313/* 314 * Copy a byte string into an sbuf. 315 */ 316int 317sbuf_bcpy(struct sbuf *s, const void *buf, size_t len) 318{ | 307 if (len == 0) 308 return (0); 309 if (len > SBUF_FREESPACE(s)) { 310 sbuf_extend(s, len - SBUF_FREESPACE(s)); 311 len = min(len, SBUF_FREESPACE(s)); 312 } 313 if (copyin(uaddr, s->s_buf + s->s_len, len) != 0) 314 return (-1); --- 4 unchanged lines hidden (view full) --- 319#endif 320 321/* 322 * Copy a byte string into an sbuf. 323 */ 324int 325sbuf_bcpy(struct sbuf *s, const void *buf, size_t len) 326{ |
327 |
|
319 assert_sbuf_integrity(s); 320 assert_sbuf_state(s, 0); 321 322 sbuf_clear(s); 323 return (sbuf_bcat(s, buf, len)); 324} 325 326/* 327 * Append a string to an sbuf. 328 */ 329int 330sbuf_cat(struct sbuf *s, const char *str) 331{ | 328 assert_sbuf_integrity(s); 329 assert_sbuf_state(s, 0); 330 331 sbuf_clear(s); 332 return (sbuf_bcat(s, buf, len)); 333} 334 335/* 336 * Append a string to an sbuf. 337 */ 338int 339sbuf_cat(struct sbuf *s, const char *str) 340{ |
341 |
|
332 assert_sbuf_integrity(s); 333 assert_sbuf_state(s, 0); 334 335 if (SBUF_HASOVERFLOWED(s)) 336 return (-1); 337 338 while (*str) { 339 if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0) --- 44 unchanged lines hidden (view full) --- 384#endif 385 386/* 387 * Copy a string into an sbuf. 388 */ 389int 390sbuf_cpy(struct sbuf *s, const char *str) 391{ | 342 assert_sbuf_integrity(s); 343 assert_sbuf_state(s, 0); 344 345 if (SBUF_HASOVERFLOWED(s)) 346 return (-1); 347 348 while (*str) { 349 if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0) --- 44 unchanged lines hidden (view full) --- 394#endif 395 396/* 397 * Copy a string into an sbuf. 398 */ 399int 400sbuf_cpy(struct sbuf *s, const char *str) 401{ |
402 |
|
392 assert_sbuf_integrity(s); 393 assert_sbuf_state(s, 0); 394 395 sbuf_clear(s); 396 return (sbuf_cat(s, str)); 397} 398 399/* --- 50 unchanged lines hidden (view full) --- 450sbuf_printf(struct sbuf *s, const char *fmt, ...) 451{ 452 va_list ap; 453 int result; 454 455 va_start(ap, fmt); 456 result = sbuf_vprintf(s, fmt, ap); 457 va_end(ap); | 403 assert_sbuf_integrity(s); 404 assert_sbuf_state(s, 0); 405 406 sbuf_clear(s); 407 return (sbuf_cat(s, str)); 408} 409 410/* --- 50 unchanged lines hidden (view full) --- 461sbuf_printf(struct sbuf *s, const char *fmt, ...) 462{ 463 va_list ap; 464 int result; 465 466 va_start(ap, fmt); 467 result = sbuf_vprintf(s, fmt, ap); 468 va_end(ap); |
458 return(result); | 469 return (result); |
459} 460 461/* 462 * Append a character to an sbuf. 463 */ 464int 465sbuf_putc(struct sbuf *s, int c) 466{ | 470} 471 472/* 473 * Append a character to an sbuf. 474 */ 475int 476sbuf_putc(struct sbuf *s, int c) 477{ |
478 |
|
467 assert_sbuf_integrity(s); 468 assert_sbuf_state(s, 0); 469 470 if (SBUF_HASOVERFLOWED(s)) 471 return (-1); | 479 assert_sbuf_integrity(s); 480 assert_sbuf_state(s, 0); 481 482 if (SBUF_HASOVERFLOWED(s)) 483 return (-1); |
472 | |
473 if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) { 474 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 475 return (-1); 476 } 477 if (c != '\0') 478 s->s_buf[s->s_len++] = c; 479 return (0); 480} 481 482/* 483 * Trim whitespace characters from end of an sbuf. 484 */ 485int 486sbuf_trim(struct sbuf *s) 487{ | 484 if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) { 485 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 486 return (-1); 487 } 488 if (c != '\0') 489 s->s_buf[s->s_len++] = c; 490 return (0); 491} 492 493/* 494 * Trim whitespace characters from end of an sbuf. 495 */ 496int 497sbuf_trim(struct sbuf *s) 498{ |
499 |
|
488 assert_sbuf_integrity(s); 489 assert_sbuf_state(s, 0); 490 491 if (SBUF_HASOVERFLOWED(s)) 492 return (-1); 493 494 while (s->s_len && isspace(s->s_buf[s->s_len-1])) 495 --s->s_len; 496 497 return (0); 498} 499 500/* 501 * Check if an sbuf overflowed 502 */ 503int 504sbuf_overflowed(struct sbuf *s) 505{ | 500 assert_sbuf_integrity(s); 501 assert_sbuf_state(s, 0); 502 503 if (SBUF_HASOVERFLOWED(s)) 504 return (-1); 505 506 while (s->s_len && isspace(s->s_buf[s->s_len-1])) 507 --s->s_len; 508 509 return (0); 510} 511 512/* 513 * Check if an sbuf overflowed 514 */ 515int 516sbuf_overflowed(struct sbuf *s) 517{ |
506 return SBUF_HASOVERFLOWED(s); | 518 519 return (SBUF_HASOVERFLOWED(s)); |
507} 508 509/* 510 * Finish off an sbuf. 511 */ 512void 513sbuf_finish(struct sbuf *s) 514{ | 520} 521 522/* 523 * Finish off an sbuf. 524 */ 525void 526sbuf_finish(struct sbuf *s) 527{ |
528 |
|
515 assert_sbuf_integrity(s); 516 assert_sbuf_state(s, 0); 517 518 s->s_buf[s->s_len] = '\0'; 519 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 520 SBUF_SETFLAG(s, SBUF_FINISHED); 521} 522 523/* 524 * Return a pointer to the sbuf data. 525 */ 526char * 527sbuf_data(struct sbuf *s) 528{ | 529 assert_sbuf_integrity(s); 530 assert_sbuf_state(s, 0); 531 532 s->s_buf[s->s_len] = '\0'; 533 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 534 SBUF_SETFLAG(s, SBUF_FINISHED); 535} 536 537/* 538 * Return a pointer to the sbuf data. 539 */ 540char * 541sbuf_data(struct sbuf *s) 542{ |
543 |
|
529 assert_sbuf_integrity(s); 530 assert_sbuf_state(s, SBUF_FINISHED); 531 | 544 assert_sbuf_integrity(s); 545 assert_sbuf_state(s, SBUF_FINISHED); 546 |
532 return s->s_buf; | 547 return (s->s_buf); |
533} 534 535/* 536 * Return the length of the sbuf data. 537 */ 538int 539sbuf_len(struct sbuf *s) 540{ | 548} 549 550/* 551 * Return the length of the sbuf data. 552 */ 553int 554sbuf_len(struct sbuf *s) 555{ |
556 |
|
541 assert_sbuf_integrity(s); 542 /* don't care if it's finished or not */ 543 544 if (SBUF_HASOVERFLOWED(s)) 545 return (-1); | 557 assert_sbuf_integrity(s); 558 /* don't care if it's finished or not */ 559 560 if (SBUF_HASOVERFLOWED(s)) 561 return (-1); |
546 return s->s_len; | 562 return (s->s_len); |
547} 548 549/* 550 * Clear an sbuf, free its buffer if necessary. 551 */ 552void 553sbuf_delete(struct sbuf *s) 554{ 555 int isdyn; 556 557 assert_sbuf_integrity(s); 558 /* don't care if it's finished or not */ 559 560 if (SBUF_ISDYNAMIC(s)) 561 SBFREE(s->s_buf); 562 isdyn = SBUF_ISDYNSTRUCT(s); | 563} 564 565/* 566 * Clear an sbuf, free its buffer if necessary. 567 */ 568void 569sbuf_delete(struct sbuf *s) 570{ 571 int isdyn; 572 573 assert_sbuf_integrity(s); 574 /* don't care if it's finished or not */ 575 576 if (SBUF_ISDYNAMIC(s)) 577 SBFREE(s->s_buf); 578 isdyn = SBUF_ISDYNSTRUCT(s); |
563 bzero(s, sizeof *s); | 579 bzero(s, sizeof(*s)); |
564 if (isdyn) 565 SBFREE(s); 566} 567 568/* 569 * Check if an sbuf has been finished. 570 */ 571int 572sbuf_done(struct sbuf *s) 573{ 574 | 580 if (isdyn) 581 SBFREE(s); 582} 583 584/* 585 * Check if an sbuf has been finished. 586 */ 587int 588sbuf_done(struct sbuf *s) 589{ 590 |
575 return(SBUF_ISFINISHED(s)); | 591 return (SBUF_ISFINISHED(s)); |
576} | 592} |