subr_sbuf.c (7150b86bfe2474cebd10c863c1c57f089684f8bc) | subr_sbuf.c (adecd05bf0293be427de08c863bf62887c075ef0) |
---|---|
1/*- 2 * Copyright (c) 2000-2008 Poul-Henning Kamp 3 * Copyright (c) 2000-2008 Dag-Erling Coïdan Smørgrav 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: --- 138 unchanged lines hidden (view full) --- 147 148/* 149 * Extend an sbuf. 150 */ 151static int 152sbuf_extend(struct sbuf *s, int addlen) 153{ 154 char *newbuf; | 1/*- 2 * Copyright (c) 2000-2008 Poul-Henning Kamp 3 * Copyright (c) 2000-2008 Dag-Erling Coïdan Smørgrav 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: --- 138 unchanged lines hidden (view full) --- 147 148/* 149 * Extend an sbuf. 150 */ 151static int 152sbuf_extend(struct sbuf *s, int addlen) 153{ 154 char *newbuf; |
155 size_t newsize; | 155 int newsize; |
156 157 if (!SBUF_CANEXTEND(s)) 158 return (-1); 159 newsize = sbuf_extendsize(s->s_size + addlen); | 156 157 if (!SBUF_CANEXTEND(s)) 158 return (-1); 159 newsize = sbuf_extendsize(s->s_size + addlen); |
160 if (s->s_buf == s->s_static_buf && newsize <= sizeof(s->s_static_buf)) { 161 s->s_size = sizeof(s->s_static_buf); 162 return (0); 163 } 164 | |
165 newbuf = SBMALLOC(newsize); 166 if (newbuf == NULL) 167 return (-1); 168 memcpy(newbuf, s->s_buf, s->s_size); 169 if (SBUF_ISDYNAMIC(s)) 170 SBFREE(s->s_buf); 171 else 172 SBUF_SETFLAG(s, SBUF_DYNAMIC); 173 s->s_buf = newbuf; 174 s->s_size = newsize; 175 return (0); 176} 177 178/* 179 * Initialize the internals of an sbuf. 180 * If buf is non-NULL, it points to a static or already-allocated string 181 * big enough to hold at least length characters. 182 */ 183static struct sbuf * | 160 newbuf = SBMALLOC(newsize); 161 if (newbuf == NULL) 162 return (-1); 163 memcpy(newbuf, s->s_buf, s->s_size); 164 if (SBUF_ISDYNAMIC(s)) 165 SBFREE(s->s_buf); 166 else 167 SBUF_SETFLAG(s, SBUF_DYNAMIC); 168 s->s_buf = newbuf; 169 s->s_size = newsize; 170 return (0); 171} 172 173/* 174 * Initialize the internals of an sbuf. 175 * If buf is non-NULL, it points to a static or already-allocated string 176 * big enough to hold at least length characters. 177 */ 178static struct sbuf * |
184sbuf_newbuf(struct sbuf *s, char *buf, size_t length, int flags) | 179sbuf_newbuf(struct sbuf *s, char *buf, int length, int flags) |
185{ 186 187 memset(s, 0, sizeof(*s)); 188 s->s_flags = flags; 189 s->s_size = length; 190 s->s_buf = buf; 191 192 if ((s->s_flags & SBUF_AUTOEXTEND) == 0) { 193 KASSERT(s->s_size >= 0, 194 ("attempt to create a too small sbuf")); 195 } 196 197 if (s->s_buf != NULL) 198 return (s); 199 200 if ((flags & SBUF_AUTOEXTEND) != 0) 201 s->s_size = sbuf_extendsize(s->s_size); 202 | 180{ 181 182 memset(s, 0, sizeof(*s)); 183 s->s_flags = flags; 184 s->s_size = length; 185 s->s_buf = buf; 186 187 if ((s->s_flags & SBUF_AUTOEXTEND) == 0) { 188 KASSERT(s->s_size >= 0, 189 ("attempt to create a too small sbuf")); 190 } 191 192 if (s->s_buf != NULL) 193 return (s); 194 195 if ((flags & SBUF_AUTOEXTEND) != 0) 196 s->s_size = sbuf_extendsize(s->s_size); 197 |
203 if (s->s_size <= sizeof(s->s_static_buf)) { 204 s->s_buf = s->s_static_buf; 205 return (s); 206 } 207 | |
208 s->s_buf = SBMALLOC(s->s_size); 209 if (s->s_buf == NULL) 210 return (NULL); 211 SBUF_SETFLAG(s, SBUF_DYNAMIC); 212 return (s); 213} 214 215/* --- 72 unchanged lines hidden (view full) --- 288 s->s_sect_len = 0; 289} 290 291/* 292 * Set the sbuf's end position to an arbitrary value. 293 * Effectively truncates the sbuf at the new position. 294 */ 295int | 198 s->s_buf = SBMALLOC(s->s_size); 199 if (s->s_buf == NULL) 200 return (NULL); 201 SBUF_SETFLAG(s, SBUF_DYNAMIC); 202 return (s); 203} 204 205/* --- 72 unchanged lines hidden (view full) --- 278 s->s_sect_len = 0; 279} 280 281/* 282 * Set the sbuf's end position to an arbitrary value. 283 * Effectively truncates the sbuf at the new position. 284 */ 285int |
296sbuf_setpos(struct sbuf *s, size_t pos) | 286sbuf_setpos(struct sbuf *s, ssize_t pos) |
297{ 298 299 assert_sbuf_integrity(s); 300 assert_sbuf_state(s, 0); 301 | 287{ 288 289 assert_sbuf_integrity(s); 290 assert_sbuf_state(s, 0); 291 |
292 KASSERT(pos >= 0, 293 ("attempt to seek to a negative position (%jd)", (intmax_t)pos)); |
|
302 KASSERT(pos < s->s_size, 303 ("attempt to seek past end of sbuf (%jd >= %jd)", 304 (intmax_t)pos, (intmax_t)s->s_size)); 305 KASSERT(!SBUF_ISSECTION(s), 306 ("attempt to seek when in a section")); 307 | 294 KASSERT(pos < s->s_size, 295 ("attempt to seek past end of sbuf (%jd >= %jd)", 296 (intmax_t)pos, (intmax_t)s->s_size)); 297 KASSERT(!SBUF_ISSECTION(s), 298 ("attempt to seek when in a section")); 299 |
308 if (pos > s->s_len) | 300 if (pos < 0 || pos > s->s_len) |
309 return (-1); 310 s->s_len = pos; 311 return (0); 312} 313 314/* 315 * Set up a drain function and argument on an sbuf to flush data to 316 * when the sbuf buffer overflows. --- 247 unchanged lines hidden (view full) --- 564 return (-1); 565 return (0); 566} 567#else /* !_KERNEL */ 568int 569sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap) 570{ 571 va_list ap_copy; | 301 return (-1); 302 s->s_len = pos; 303 return (0); 304} 305 306/* 307 * Set up a drain function and argument on an sbuf to flush data to 308 * when the sbuf buffer overflows. --- 247 unchanged lines hidden (view full) --- 556 return (-1); 557 return (0); 558} 559#else /* !_KERNEL */ 560int 561sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap) 562{ 563 va_list ap_copy; |
572 size_t len; 573 int error; | 564 int error, len; |
574 575 assert_sbuf_integrity(s); 576 assert_sbuf_state(s, 0); 577 578 KASSERT(fmt != NULL, 579 ("%s called with a NULL format string", __func__)); 580 581 if (s->s_error != 0) --- 259 unchanged lines hidden --- | 565 566 assert_sbuf_integrity(s); 567 assert_sbuf_state(s, 0); 568 569 KASSERT(fmt != NULL, 570 ("%s called with a NULL format string", __func__)); 571 572 if (s->s_error != 0) --- 259 unchanged lines hidden --- |