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