subr_sbuf.c (116182) | subr_sbuf.c (125937) |
---|---|
1/*- 2 * Copyright (c) 2000 Poul-Henning Kamp and 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 --- 13 unchanged lines hidden (view full) --- 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. 27 */ 28 29#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2000 Poul-Henning Kamp and 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 --- 13 unchanged lines hidden (view full) --- 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. 27 */ 28 29#include <sys/cdefs.h> |
30__FBSDID("$FreeBSD: head/sys/kern/subr_sbuf.c 116182 2003-06-11 00:56:59Z obrien $"); | 30__FBSDID("$FreeBSD: head/sys/kern/subr_sbuf.c 125937 2004-02-17 10:21:03Z 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> --- 201 unchanged lines hidden (view full) --- 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{ 246 assert_sbuf_integrity(s); 247 assert_sbuf_state(s, 0); | 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> --- 201 unchanged lines hidden (view full) --- 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{ 246 assert_sbuf_integrity(s); 247 assert_sbuf_state(s, 0); |
248 | 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)); | 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 | 253 |
254 if (pos < 0 || pos > s->s_len) 255 return (-1); 256 s->s_len = pos; 257 return (0); 258} 259 260/* 261 * Append a byte string to an sbuf. 262 */ 263int 264sbuf_bcat(struct sbuf *s, const char *str, size_t len) 265{ 266 assert_sbuf_integrity(s); 267 assert_sbuf_state(s, 0); | 254 if (pos < 0 || pos > s->s_len) 255 return (-1); 256 s->s_len = pos; 257 return (0); 258} 259 260/* 261 * Append a byte string to an sbuf. 262 */ 263int 264sbuf_bcat(struct sbuf *s, const char *str, size_t len) 265{ 266 assert_sbuf_integrity(s); 267 assert_sbuf_state(s, 0); |
268 | 268 |
269 if (SBUF_HASOVERFLOWED(s)) 270 return (-1); | 269 if (SBUF_HASOVERFLOWED(s)) 270 return (-1); |
271 | 271 |
272 for (; len; len--) { 273 if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0) 274 break; 275 s->s_buf[s->s_len++] = *str++; 276 } 277 if (len) { 278 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 279 return (-1); --- 18 unchanged lines hidden (view full) --- 298 return (0); 299 if (len > SBUF_FREESPACE(s)) { 300 sbuf_extend(s, len - SBUF_FREESPACE(s)); 301 len = min(len, SBUF_FREESPACE(s)); 302 } 303 if (copyin(uaddr, s->s_buf + s->s_len, len) != 0) 304 return (-1); 305 s->s_len += len; | 272 for (; len; len--) { 273 if (!SBUF_HASROOM(s) && sbuf_extend(s, len) < 0) 274 break; 275 s->s_buf[s->s_len++] = *str++; 276 } 277 if (len) { 278 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 279 return (-1); --- 18 unchanged lines hidden (view full) --- 298 return (0); 299 if (len > SBUF_FREESPACE(s)) { 300 sbuf_extend(s, len - SBUF_FREESPACE(s)); 301 len = min(len, SBUF_FREESPACE(s)); 302 } 303 if (copyin(uaddr, s->s_buf + s->s_len, len) != 0) 304 return (-1); 305 s->s_len += len; |
306 | 306 |
307 return (0); 308} 309#endif 310 311/* 312 * Copy a byte string into an sbuf. 313 */ 314int 315sbuf_bcpy(struct sbuf *s, const char *str, size_t len) 316{ 317 assert_sbuf_integrity(s); 318 assert_sbuf_state(s, 0); | 307 return (0); 308} 309#endif 310 311/* 312 * Copy a byte string into an sbuf. 313 */ 314int 315sbuf_bcpy(struct sbuf *s, const char *str, size_t len) 316{ 317 assert_sbuf_integrity(s); 318 assert_sbuf_state(s, 0); |
319 | 319 |
320 sbuf_clear(s); 321 return (sbuf_bcat(s, str, len)); 322} 323 324/* 325 * Append a string to an sbuf. 326 */ 327int 328sbuf_cat(struct sbuf *s, const char *str) 329{ 330 assert_sbuf_integrity(s); 331 assert_sbuf_state(s, 0); | 320 sbuf_clear(s); 321 return (sbuf_bcat(s, str, len)); 322} 323 324/* 325 * Append a string to an sbuf. 326 */ 327int 328sbuf_cat(struct sbuf *s, const char *str) 329{ 330 assert_sbuf_integrity(s); 331 assert_sbuf_state(s, 0); |
332 | 332 |
333 if (SBUF_HASOVERFLOWED(s)) 334 return (-1); | 333 if (SBUF_HASOVERFLOWED(s)) 334 return (-1); |
335 | 335 |
336 while (*str) { 337 if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0) 338 break; 339 s->s_buf[s->s_len++] = *str++; 340 } 341 if (*str) { 342 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 343 return (-1); --- 4 unchanged lines hidden (view full) --- 348#ifdef _KERNEL 349/* 350 * Append a string from userland to an sbuf. 351 */ 352int 353sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len) 354{ 355 size_t done; | 336 while (*str) { 337 if (!SBUF_HASROOM(s) && sbuf_extend(s, strlen(str)) < 0) 338 break; 339 s->s_buf[s->s_len++] = *str++; 340 } 341 if (*str) { 342 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 343 return (-1); --- 4 unchanged lines hidden (view full) --- 348#ifdef _KERNEL 349/* 350 * Append a string from userland to an sbuf. 351 */ 352int 353sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len) 354{ 355 size_t done; |
356 | 356 |
357 assert_sbuf_integrity(s); 358 assert_sbuf_state(s, 0); 359 360 if (SBUF_HASOVERFLOWED(s)) 361 return (-1); 362 363 if (len == 0) 364 len = SBUF_FREESPACE(s); /* XXX return 0? */ --- 6 unchanged lines hidden (view full) --- 371 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 372 /* fall through */ 373 case 0: 374 s->s_len += done - 1; 375 break; 376 default: 377 return (-1); /* XXX */ 378 } | 357 assert_sbuf_integrity(s); 358 assert_sbuf_state(s, 0); 359 360 if (SBUF_HASOVERFLOWED(s)) 361 return (-1); 362 363 if (len == 0) 364 len = SBUF_FREESPACE(s); /* XXX return 0? */ --- 6 unchanged lines hidden (view full) --- 371 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 372 /* fall through */ 373 case 0: 374 s->s_len += done - 1; 375 break; 376 default: 377 return (-1); /* XXX */ 378 } |
379 | 379 |
380 return (0); 381} 382#endif 383 384/* 385 * Copy a string into an sbuf. 386 */ 387int 388sbuf_cpy(struct sbuf *s, const char *str) 389{ 390 assert_sbuf_integrity(s); 391 assert_sbuf_state(s, 0); | 380 return (0); 381} 382#endif 383 384/* 385 * Copy a string into an sbuf. 386 */ 387int 388sbuf_cpy(struct sbuf *s, const char *str) 389{ 390 assert_sbuf_integrity(s); 391 assert_sbuf_state(s, 0); |
392 | 392 |
393 sbuf_clear(s); 394 return (sbuf_cat(s, str)); 395} 396 397/* 398 * Format the given argument list and append the resulting string to an sbuf. 399 */ 400int --- 58 unchanged lines hidden (view full) --- 459/* 460 * Append a character to an sbuf. 461 */ 462int 463sbuf_putc(struct sbuf *s, int c) 464{ 465 assert_sbuf_integrity(s); 466 assert_sbuf_state(s, 0); | 393 sbuf_clear(s); 394 return (sbuf_cat(s, str)); 395} 396 397/* 398 * Format the given argument list and append the resulting string to an sbuf. 399 */ 400int --- 58 unchanged lines hidden (view full) --- 459/* 460 * Append a character to an sbuf. 461 */ 462int 463sbuf_putc(struct sbuf *s, int c) 464{ 465 assert_sbuf_integrity(s); 466 assert_sbuf_state(s, 0); |
467 | 467 |
468 if (SBUF_HASOVERFLOWED(s)) 469 return (-1); | 468 if (SBUF_HASOVERFLOWED(s)) 469 return (-1); |
470 | 470 |
471 if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) { 472 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 473 return (-1); 474 } 475 if (c != '\0') 476 s->s_buf[s->s_len++] = c; 477 return (0); 478} 479 480/* 481 * Trim whitespace characters from end of an sbuf. 482 */ 483int 484sbuf_trim(struct sbuf *s) 485{ 486 assert_sbuf_integrity(s); 487 assert_sbuf_state(s, 0); | 471 if (!SBUF_HASROOM(s) && sbuf_extend(s, 1) < 0) { 472 SBUF_SETFLAG(s, SBUF_OVERFLOWED); 473 return (-1); 474 } 475 if (c != '\0') 476 s->s_buf[s->s_len++] = c; 477 return (0); 478} 479 480/* 481 * Trim whitespace characters from end of an sbuf. 482 */ 483int 484sbuf_trim(struct sbuf *s) 485{ 486 assert_sbuf_integrity(s); 487 assert_sbuf_state(s, 0); |
488 | 488 |
489 if (SBUF_HASOVERFLOWED(s)) 490 return (-1); | 489 if (SBUF_HASOVERFLOWED(s)) 490 return (-1); |
491 | 491 |
492 while (s->s_len && isspace(s->s_buf[s->s_len-1])) 493 --s->s_len; 494 495 return (0); 496} 497 498/* 499 * Check if an sbuf overflowed --- 7 unchanged lines hidden (view full) --- 507/* 508 * Finish off an sbuf. 509 */ 510void 511sbuf_finish(struct sbuf *s) 512{ 513 assert_sbuf_integrity(s); 514 assert_sbuf_state(s, 0); | 492 while (s->s_len && isspace(s->s_buf[s->s_len-1])) 493 --s->s_len; 494 495 return (0); 496} 497 498/* 499 * Check if an sbuf overflowed --- 7 unchanged lines hidden (view full) --- 507/* 508 * Finish off an sbuf. 509 */ 510void 511sbuf_finish(struct sbuf *s) 512{ 513 assert_sbuf_integrity(s); 514 assert_sbuf_state(s, 0); |
515 | 515 |
516 s->s_buf[s->s_len] = '\0'; 517 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 518 SBUF_SETFLAG(s, SBUF_FINISHED); 519} 520 521/* 522 * Return a pointer to the sbuf data. 523 */ 524char * 525sbuf_data(struct sbuf *s) 526{ 527 assert_sbuf_integrity(s); 528 assert_sbuf_state(s, SBUF_FINISHED); | 516 s->s_buf[s->s_len] = '\0'; 517 SBUF_CLEARFLAG(s, SBUF_OVERFLOWED); 518 SBUF_SETFLAG(s, SBUF_FINISHED); 519} 520 521/* 522 * Return a pointer to the sbuf data. 523 */ 524char * 525sbuf_data(struct sbuf *s) 526{ 527 assert_sbuf_integrity(s); 528 assert_sbuf_state(s, SBUF_FINISHED); |
529 | 529 |
530 return s->s_buf; 531} 532 533/* 534 * Return the length of the sbuf data. 535 */ 536int 537sbuf_len(struct sbuf *s) 538{ 539 assert_sbuf_integrity(s); 540 /* don't care if it's finished or not */ | 530 return s->s_buf; 531} 532 533/* 534 * Return the length of the sbuf data. 535 */ 536int 537sbuf_len(struct sbuf *s) 538{ 539 assert_sbuf_integrity(s); 540 /* don't care if it's finished or not */ |
541 | 541 |
542 if (SBUF_HASOVERFLOWED(s)) 543 return (-1); 544 return s->s_len; 545} 546 547/* 548 * Clear an sbuf, free its buffer if necessary. 549 */ 550void 551sbuf_delete(struct sbuf *s) 552{ 553 int isdyn; 554 555 assert_sbuf_integrity(s); 556 /* don't care if it's finished or not */ | 542 if (SBUF_HASOVERFLOWED(s)) 543 return (-1); 544 return s->s_len; 545} 546 547/* 548 * Clear an sbuf, free its buffer if necessary. 549 */ 550void 551sbuf_delete(struct sbuf *s) 552{ 553 int isdyn; 554 555 assert_sbuf_integrity(s); 556 /* don't care if it's finished or not */ |
557 | 557 |
558 if (SBUF_ISDYNAMIC(s)) 559 SBFREE(s->s_buf); 560 isdyn = SBUF_ISDYNSTRUCT(s); 561 bzero(s, sizeof *s); 562 if (isdyn) 563 SBFREE(s); 564} 565 566/* 567 * Check if an sbuf has been finished. 568 */ 569int 570sbuf_done(struct sbuf *s) 571{ 572 573 return(SBUF_ISFINISHED(s)); 574} | 558 if (SBUF_ISDYNAMIC(s)) 559 SBFREE(s->s_buf); 560 isdyn = SBUF_ISDYNSTRUCT(s); 561 bzero(s, sizeof *s); 562 if (isdyn) 563 SBFREE(s); 564} 565 566/* 567 * Check if an sbuf has been finished. 568 */ 569int 570sbuf_done(struct sbuf *s) 571{ 572 573 return(SBUF_ISFINISHED(s)); 574} |