Changeset 6afc9d7 in mainline for uspace/lib/c/generic/io/io.c
- Timestamp:
- 2015-10-06T19:01:36Z (9 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 0328987
- Parents:
- f1f7584
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/io.c
rf1f7584 r6afc9d7 231 231 if (stream->buf == NULL) { 232 232 errno = ENOMEM; 233 return -1;233 return EOF; 234 234 } 235 235 … … 365 365 * @param nmemb Number of records to read. 366 366 * @param stream Pointer to the stream. 367 * 368 * @return Number of elements successfully read. On error this is less than 369 * nmemb, stream error indicator is set and errno is set. 367 370 */ 368 371 static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream) … … 379 382 ssize_t rd = read(stream->fd, buf + done, left); 380 383 381 if (rd < 0) 384 if (rd < 0) { 385 /* errno was set by read() */ 382 386 stream->error = true; 383 else if (rd == 0)387 } else if (rd == 0) { 384 388 stream->eof = true; 385 else {389 } else { 386 390 left -= rd; 387 391 done += rd; … … 398 402 * @param nmemb Number of records to write. 399 403 * @param stream Pointer to the stream. 404 * 405 * @return Number of elements successfully written. On error this is less than 406 * nmemb, stream error indicator is set and errno is set. 400 407 */ 401 408 static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream) … … 403 410 size_t left; 404 411 size_t done; 412 int rc; 405 413 406 414 if (size == 0 || nmemb == 0) … … 412 420 while ((left > 0) && (!stream->error)) { 413 421 ssize_t wr; 422 size_t uwr; 414 423 415 if (stream->kio) 416 wr = kio_write(buf + done, left); 417 else 424 if (stream->kio) { 425 uwr = 0; 426 rc = kio_write(buf + done, left, &uwr); 427 if (rc != EOK) 428 errno = rc; 429 } else { 418 430 wr = write(stream->fd, buf + done, left); 431 if (wr >= 0) { 432 uwr = (size_t)wr; 433 rc = EOK; 434 } else { 435 /* errno was set by write */ 436 uwr = 0; 437 rc = errno; 438 } 439 } 419 440 420 if (wr <= 0) 441 if (rc != EOK) { 442 /* errno was set above */ 421 443 stream->error = true; 422 else {423 left -= wr;424 done += wr;444 } else { 445 left -= uwr; 446 done += uwr; 425 447 } 426 448 } … … 432 454 } 433 455 434 /** Read some data in stream buffer. */ 456 /** Read some data in stream buffer. 457 * 458 * On error, stream error indicator is set and errno is set. 459 */ 435 460 static void _ffillbuf(FILE *stream) 436 461 { … … 441 466 rc = read(stream->fd, stream->buf, stream->buf_size); 442 467 if (rc < 0) { 468 /* errno was set by read() */ 443 469 stream->error = true; 444 470 return; … … 465 491 466 492 /* If buffer has prefetched read data, we need to seek back. */ 467 if (bytes_used > 0 && stream->buf_state == _bs_read) 468 lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR); 493 if (bytes_used > 0 && stream->buf_state == _bs_read) { 494 off64_t rc; 495 rc = lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR); 496 if (rc == (off64_t)-1) { 497 /* errno was set by lseek */ 498 stream->error = 1; 499 return; 500 } 501 } 469 502 470 503 /* If buffer has unwritten data, we need to write them out. */ 471 if (bytes_used > 0 && stream->buf_state == _bs_write) 504 if (bytes_used > 0 && stream->buf_state == _bs_write) { 472 505 (void) _fwrite(stream->buf_tail, 1, bytes_used, stream); 506 /* On error stream error indicator and errno are set by _fwrite */ 507 if (stream->error) 508 return; 509 } 473 510 474 511 stream->buf_head = stream->buf; … … 528 565 _ffillbuf(stream); 529 566 530 if (stream->error || stream->eof) 567 if (stream->error || stream->eof) { 568 /* On error errno was set by _ffillbuf() */ 531 569 break; 570 } 532 571 533 572 data_avail = stream->buf_head - stream->buf_tail; … … 584 623 if (stream->buf_state == _bs_read) 585 624 _fflushbuf(stream); 586 587 625 588 626 /* Perform lazy allocation of stream buffer. */ … … 622 660 /* Only need to drain buffer. */ 623 661 _fflushbuf(stream); 624 need_flush = false; 662 if (!stream->error) 663 need_flush = false; 625 664 } 626 665 } … … 734 773 off64_t rc; 735 774 775 if (stream->error) 776 return EOF; 777 736 778 _fflushbuf(stream); 779 if (stream->error) { 780 /* errno was set by _fflushbuf() */ 781 return EOF; 782 } 783 737 784 stream->ungetc_chars = 0; 738 785 739 786 rc = lseek(stream->fd, offset, whence); 740 787 if (rc == (off64_t) (-1)) { 741 /* errno has been set by lseek 64.*/742 return -1;788 /* errno has been set by lseek() */ 789 return EOF; 743 790 } 744 791 … … 749 796 off64_t ftell(FILE *stream) 750 797 { 798 off64_t pos; 799 800 if (stream->error) 801 return EOF; 802 751 803 _fflushbuf(stream); 752 return lseek(stream->fd, 0, SEEK_CUR) - stream->ungetc_chars; 804 if (stream->error) { 805 /* errno was set by _fflushbuf() */ 806 return EOF; 807 } 808 809 pos = lseek(stream->fd, 0, SEEK_CUR); 810 if (pos == (off64_t) -1) { 811 /* errno was set by lseek */ 812 return (off64_t) -1; 813 } 814 815 return pos - stream->ungetc_chars; 753 816 } 754 817 … … 760 823 int fflush(FILE *stream) 761 824 { 825 if (stream->error) 826 return EOF; 827 762 828 _fflushbuf(stream); 829 if (stream->error) { 830 /* errno was set by _fflushbuf() */ 831 return EOF; 832 } 763 833 764 834 if (stream->kio) { 765 835 kio_update(); 766 return EOK;836 return 0; 767 837 } 768 838 … … 773 843 */ 774 844 stream->need_sync = false; 775 return fsync(stream->fd); 776 } 777 778 return ENOENT; 845 if (fsync(stream->fd) != 0) { 846 /* errno was set by fsync() */ 847 return EOF; 848 } 849 850 return 0; 851 } 852 853 return 0; 779 854 } 780 855 … … 799 874 if (stream->kio) { 800 875 errno = EBADF; 801 return -1;876 return EOF; 802 877 } 803 878 … … 805 880 } 806 881 807 async_sess_t * fsession(FILE *stream, iface_t iface)882 async_sess_t *vfs_fsession(FILE *stream, iface_t iface) 808 883 { 809 884 if (stream->fd >= 0) { 810 885 if (stream->sess == NULL) 811 stream->sess = fd_session(stream->fd, iface);886 stream->sess = vfs_fd_session(stream->fd, iface); 812 887 813 888 return stream->sess; … … 817 892 } 818 893 819 int fhandle(FILE *stream, int *handle)894 int vfs_fhandle(FILE *stream, int *handle) 820 895 { 821 896 if (stream->fd >= 0) {
Note:
See TracChangeset
for help on using the changeset viewer.