Changes in uspace/lib/c/generic/io/io.c [6afc9d7:6fa9a99d] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/io.c
r6afc9d7 r6fa9a99d 231 231 if (stream->buf == NULL) { 232 232 errno = ENOMEM; 233 return EOF;233 return -1; 234 234 } 235 235 … … 299 299 } 300 300 301 302 static int _fclose_nofree(FILE *stream) 301 int fclose(FILE *stream) 303 302 { 304 303 int rc = 0; … … 313 312 314 313 list_remove(&stream->link); 315 316 if (rc != 0) {317 /* errno was set by close() */318 return EOF;319 }320 321 return 0;322 }323 324 int fclose(FILE *stream)325 {326 int rc = _fclose_nofree(stream);327 314 328 315 if ((stream != &stdin_null) … … 331 318 free(stream); 332 319 333 return rc; 334 } 335 336 FILE *freopen(const char *path, const char *mode, FILE *stream) 337 { 338 FILE *nstr; 339 340 if (path == NULL) { 341 /* Changing mode is not supported */ 342 return NULL; 343 } 344 345 (void) _fclose_nofree(stream); 346 nstr = fopen(path, mode); 347 if (nstr == NULL) { 348 free(stream); 349 return NULL; 350 } 351 352 list_remove(&nstr->link); 353 *stream = *nstr; 354 list_append(&stream->link, &files); 355 356 free(nstr); 357 358 return stream; 320 stream = NULL; 321 322 if (rc != 0) { 323 /* errno was set by close() */ 324 return EOF; 325 } 326 327 return 0; 359 328 } 360 329 … … 365 334 * @param nmemb Number of records to read. 366 335 * @param stream Pointer to the stream. 367 *368 * @return Number of elements successfully read. On error this is less than369 * nmemb, stream error indicator is set and errno is set.370 336 */ 371 337 static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream) … … 382 348 ssize_t rd = read(stream->fd, buf + done, left); 383 349 384 if (rd < 0) { 385 /* errno was set by read() */ 350 if (rd < 0) 386 351 stream->error = true; 387 } else if (rd == 0) {352 else if (rd == 0) 388 353 stream->eof = true; 389 }else {354 else { 390 355 left -= rd; 391 356 done += rd; … … 402 367 * @param nmemb Number of records to write. 403 368 * @param stream Pointer to the stream. 404 *405 * @return Number of elements successfully written. On error this is less than406 * nmemb, stream error indicator is set and errno is set.407 369 */ 408 370 static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream) … … 410 372 size_t left; 411 373 size_t done; 412 int rc;413 374 414 375 if (size == 0 || nmemb == 0) … … 420 381 while ((left > 0) && (!stream->error)) { 421 382 ssize_t wr; 422 size_t uwr; 423 424 if (stream->kio) { 425 uwr = 0; 426 rc = kio_write(buf + done, left, &uwr); 427 if (rc != EOK) 428 errno = rc; 429 } else { 383 384 if (stream->kio) 385 wr = kio_write(buf + done, left); 386 else 430 387 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 } 440 441 if (rc != EOK) { 442 /* errno was set above */ 388 389 if (wr <= 0) 443 390 stream->error = true; 444 }else {445 left -= uwr;446 done += uwr;391 else { 392 left -= wr; 393 done += wr; 447 394 } 448 395 } … … 454 401 } 455 402 456 /** Read some data in stream buffer. 457 * 458 * On error, stream error indicator is set and errno is set. 459 */ 403 /** Read some data in stream buffer. */ 460 404 static void _ffillbuf(FILE *stream) 461 405 { … … 466 410 rc = read(stream->fd, stream->buf, stream->buf_size); 467 411 if (rc < 0) { 468 /* errno was set by read() */469 412 stream->error = true; 470 413 return; … … 491 434 492 435 /* If buffer has prefetched read data, we need to seek back. */ 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 } 436 if (bytes_used > 0 && stream->buf_state == _bs_read) 437 lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR); 502 438 503 439 /* If buffer has unwritten data, we need to write them out. */ 504 if (bytes_used > 0 && stream->buf_state == _bs_write) {440 if (bytes_used > 0 && stream->buf_state == _bs_write) 505 441 (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 }510 442 511 443 stream->buf_head = stream->buf; … … 534 466 return 0; 535 467 536 bytes_left = size * nmemb;537 total_read = 0;538 dp = (uint8_t *) dest;539 540 /* Bytes from ungetc() buffer */541 while (stream->ungetc_chars > 0 && bytes_left > 0) {542 *dp++ = stream->ungetc_buf[--stream->ungetc_chars];543 ++total_read;544 --bytes_left;545 }546 547 468 /* If not buffered stream, read in directly. */ 548 469 if (stream->btype == _IONBF) { 549 total_read += _fread(dest, 1, bytes_left, stream);550 return total_read / size;470 now = _fread(dest, size, nmemb, stream); 471 return now; 551 472 } 552 473 … … 561 482 } 562 483 484 bytes_left = size * nmemb; 485 total_read = 0; 486 dp = (uint8_t *) dest; 487 563 488 while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) { 564 489 if (stream->buf_head == stream->buf_tail) 565 490 _ffillbuf(stream); 566 491 567 if (stream->error || stream->eof) { 568 /* On error errno was set by _ffillbuf() */ 492 if (stream->error || stream->eof) 569 493 break; 570 }571 494 572 495 data_avail = stream->buf_head - stream->buf_tail; … … 624 547 _fflushbuf(stream); 625 548 549 626 550 /* Perform lazy allocation of stream buffer. */ 627 551 if (stream->buf == NULL) { … … 660 584 /* Only need to drain buffer. */ 661 585 _fflushbuf(stream); 662 if (!stream->error) 663 need_flush = false; 586 need_flush = false; 664 587 } 665 588 } … … 695 618 int fputs(const char *str, FILE *stream) 696 619 { 697 (void) fwrite(str, str_size(str), 1, stream); 698 if (ferror(stream)) 699 return EOF; 700 return 0; 620 return fwrite(str, str_size(str), 1, stream); 701 621 } 702 622 … … 754 674 } 755 675 756 int ungetc(int c, FILE *stream)757 {758 if (c == EOF)759 return EOF;760 761 if (stream->ungetc_chars >= UNGETC_MAX)762 return EOF;763 764 stream->ungetc_buf[stream->ungetc_chars++] =765 (uint8_t)c;766 767 stream->eof = false;768 return (uint8_t)c;769 }770 771 676 int fseek(FILE *stream, off64_t offset, int whence) 772 677 { 773 678 off64_t rc; 774 679 775 if (stream->error)776 return EOF;777 778 680 _fflushbuf(stream); 779 if (stream->error) {780 /* errno was set by _fflushbuf() */781 return EOF;782 }783 784 stream->ungetc_chars = 0;785 681 786 682 rc = lseek(stream->fd, offset, whence); 787 683 if (rc == (off64_t) (-1)) { 788 /* errno has been set by lseek ()*/789 return EOF;684 /* errno has been set by lseek64. */ 685 return -1; 790 686 } 791 687 … … 796 692 off64_t ftell(FILE *stream) 797 693 { 798 off64_t pos;799 800 if (stream->error)801 return EOF;802 803 694 _fflushbuf(stream); 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; 695 return lseek(stream->fd, 0, SEEK_CUR); 816 696 } 817 697 … … 823 703 int fflush(FILE *stream) 824 704 { 825 if (stream->error)826 return EOF;827 828 705 _fflushbuf(stream); 829 if (stream->error) {830 /* errno was set by _fflushbuf() */831 return EOF;832 }833 706 834 707 if (stream->kio) { 835 708 kio_update(); 836 return 0;709 return EOK; 837 710 } 838 711 … … 843 716 */ 844 717 stream->need_sync = false; 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; 718 return fsync(stream->fd); 719 } 720 721 return ENOENT; 854 722 } 855 723 … … 874 742 if (stream->kio) { 875 743 errno = EBADF; 876 return EOF;744 return -1; 877 745 } 878 746 … … 880 748 } 881 749 882 async_sess_t * vfs_fsession(FILE *stream, iface_t iface)750 async_sess_t *fsession(exch_mgmt_t mgmt, FILE *stream) 883 751 { 884 752 if (stream->fd >= 0) { 885 753 if (stream->sess == NULL) 886 stream->sess = vfs_fd_session(stream->fd, iface);754 stream->sess = fd_session(mgmt, stream->fd); 887 755 888 756 return stream->sess; … … 892 760 } 893 761 894 int vfs_fhandle(FILE *stream, int *handle)762 int fhandle(FILE *stream, int *handle) 895 763 { 896 764 if (stream->fd >= 0) {
Note:
See TracChangeset
for help on using the changeset viewer.