Changeset bfd247f in mainline
- Timestamp:
- 2009-06-29T09:20:10Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7fcb74c
- Parents:
- 415c7e0d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/libc/generic/io/io.c
r415c7e0d rbfd247f 181 181 } 182 182 183 static void _setvbuf(FILE *stream) 184 { 185 /* FIXME: Use more complex rules for setting buffering options. */ 186 187 switch (stream->fd) { 188 case 1: 189 setvbuf(stream, NULL, _IOLBF, BUFSIZ); 190 break; 191 case 0: 192 case 2: 193 setvbuf(stream, NULL, _IONBF, 0); 194 break; 195 default: 196 setvbuf(stream, NULL, _IOFBF, BUFSIZ); 197 } 198 } 199 183 200 /** Allocate stream buffer. */ 184 201 static int _fallocbuf(FILE *stream) 185 202 { 186 203 assert(stream->buf == NULL); 187 204 188 205 stream->buf = malloc(stream->buf_size); 189 206 if (stream->buf == NULL) { … … 191 208 return -1; 192 209 } 193 210 194 211 stream->buf_head = stream->buf; 195 212 return 0; … … 226 243 stream->klog = false; 227 244 stream->phone = -1; 228 229 /* FIXME: Should select buffering type based on what was opened. */ 230 setvbuf(stream, NULL, _IOFBF, BUFSIZ); 245 _setvbuf(stream); 231 246 232 247 list_append(&stream->link, &files); … … 249 264 stream->klog = false; 250 265 stream->phone = -1; 251 252 /* FIXME: Should select buffering type based on what was opened. */ 253 setvbuf(stream, NULL, _IOLBF, BUFSIZ); 266 _setvbuf(stream); 254 267 255 268 list_append(&stream->link, &files); … … 282 295 stream->klog = false; 283 296 stream->phone = -1; 284 285 /* FIXME: Should select buffering type based on what was opened. */ 286 setvbuf(stream, NULL, _IOLBF, BUFSIZ); 297 _setvbuf(stream); 287 298 288 299 list_append(&stream->link, &files); … … 332 343 size_t left = size * nmemb; 333 344 size_t done = 0; 334 345 335 346 /* Make sure no data is pending write. */ 336 347 _fflushbuf(stream); 337 348 338 349 while ((left > 0) && (!stream->error) && (!stream->eof)) { 339 350 ssize_t rd = read(stream->fd, buf + done, left); … … 380 391 { 381 392 size_t bytes_used; 382 383 if ( !stream->buf || stream->btype == _IONBF || stream->error)393 394 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error)) 384 395 return; 385 396 386 397 bytes_used = stream->buf_head - stream->buf; 387 398 if (bytes_used == 0) 388 399 return; 389 400 390 401 (void) _fwrite(stream->buf, 1, bytes_used, stream); 391 402 stream->buf_head = stream->buf; … … 410 421 uint8_t b; 411 422 bool need_flush; 412 423 413 424 /* If not buffered stream, write out directly. */ 414 if (stream->btype == _IONBF) 415 return _fwrite(buf, size, nmemb, stream); 416 425 if (stream->btype == _IONBF) { 426 now = _fwrite(buf, size, nmemb, stream); 427 fflush(stream); 428 return now; 429 } 430 417 431 /* Perform lazy allocation of stream buffer. */ 418 432 if (stream->buf == NULL) { … … 420 434 return 0; /* Errno set by _fallocbuf(). */ 421 435 } 422 436 423 437 data = (uint8_t *) buf; 424 438 bytes_left = size * nmemb; 425 439 total_written = 0; 426 440 need_flush = false; 427 428 while (!stream->error && bytes_left > 0) { 429 441 442 while ((!stream->error) && (bytes_left > 0)) { 430 443 buf_free = stream->buf_size - (stream->buf_head - stream->buf); 431 444 if (bytes_left > buf_free) … … 433 446 else 434 447 now = bytes_left; 435 448 436 449 for (i = 0; i < now; i++) { 437 450 b = data[i]; 438 451 stream->buf_head[i] = b; 439 440 if ( b == '\n' && stream->btype == _IOLBF)452 453 if ((b == '\n') && (stream->btype == _IOLBF)) 441 454 need_flush = true; 442 455 } 443 456 444 457 buf += now; 445 458 stream->buf_head += now; … … 447 460 bytes_left -= now; 448 461 total_written += now; 449 462 450 463 if (buf_free == 0) { 451 464 /* Only need to drain buffer. */ … … 454 467 } 455 468 } 456 469 457 470 if (need_flush) 458 471 fflush(stream); 459 472 460 473 return (total_written / size); 461 474 } … … 496 509 { 497 510 char c; 498 511 499 512 /* This could be made faster by only flushing when needed. */ 500 513 if (stdout) … … 502 515 if (stderr) 503 516 fflush(stderr); 504 517 505 518 if (fread(&c, sizeof(char), 1, stream) < sizeof(char)) 506 519 return EOF; … … 535 548 { 536 549 _fflushbuf(stream); 537 550 538 551 if (stream->klog) { 539 552 klog_update();
Note:
See TracChangeset
for help on using the changeset viewer.