Changes in uspace/lib/c/generic/io/io.c [e86a617a:7699c21] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/io.c
re86a617a r7699c21 42 42 #include <malloc.h> 43 43 #include <async.h> 44 #include <io/k io.h>44 #include <io/klog.h> 45 45 #include <vfs/vfs.h> 46 46 #include <vfs/vfs_sess.h> … … 57 57 .error = true, 58 58 .eof = true, 59 .k io= false,59 .klog = false, 60 60 .sess = NULL, 61 61 .btype = _IONBF, … … 67 67 }; 68 68 69 static FILE stdout_k io= {69 static FILE stdout_klog = { 70 70 .fd = -1, 71 71 .error = false, 72 72 .eof = false, 73 .k io= true,73 .klog = true, 74 74 .sess = NULL, 75 75 .btype = _IOLBF, … … 81 81 }; 82 82 83 static FILE stderr_k io= {83 static FILE stderr_klog = { 84 84 .fd = -1, 85 85 .error = false, 86 86 .eof = false, 87 .k io= true,87 .klog = true, 88 88 .sess = NULL, 89 89 .btype = _IONBF, … … 113 113 stdout = fdopen(1, "w"); 114 114 } else { 115 stdout = &stdout_k io;115 stdout = &stdout_klog; 116 116 list_append(&stdout->link, &files); 117 117 } … … 120 120 stderr = fdopen(2, "w"); 121 121 } else { 122 stderr = &stderr_k io;122 stderr = &stderr_klog; 123 123 list_append(&stderr->link, &files); 124 124 } … … 231 231 if (stream->buf == NULL) { 232 232 errno = ENOMEM; 233 return EOF;233 return -1; 234 234 } 235 235 … … 267 267 stream->error = false; 268 268 stream->eof = false; 269 stream->k io= false;269 stream->klog = false; 270 270 stream->sess = NULL; 271 271 stream->need_sync = false; 272 272 _setvbuf(stream); 273 stream->ungetc_chars = 0;274 273 275 274 list_append(&stream->link, &files); … … 290 289 stream->error = false; 291 290 stream->eof = false; 292 stream->k io= false;291 stream->klog = false; 293 292 stream->sess = NULL; 294 293 stream->need_sync = false; 295 294 _setvbuf(stream); 296 stream->ungetc_chars = 0;297 295 298 296 list_append(&stream->link, &files); … … 301 299 } 302 300 303 304 static int _fclose_nofree(FILE *stream) 301 int fclose(FILE *stream) 305 302 { 306 303 int rc = 0; … … 315 312 316 313 list_remove(&stream->link); 314 315 if ((stream != &stdin_null) 316 && (stream != &stdout_klog) 317 && (stream != &stderr_klog)) 318 free(stream); 319 320 stream = NULL; 317 321 318 322 if (rc != 0) { … … 322 326 323 327 return 0; 324 }325 326 int fclose(FILE *stream)327 {328 int rc = _fclose_nofree(stream);329 330 if ((stream != &stdin_null)331 && (stream != &stdout_kio)332 && (stream != &stderr_kio))333 free(stream);334 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;361 328 } 362 329 … … 367 334 * @param nmemb Number of records to read. 368 335 * @param stream Pointer to the stream. 369 *370 * @return Number of elements successfully read. On error this is less than371 * nmemb, stream error indicator is set and errno is set.372 336 */ 373 337 static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream) … … 384 348 ssize_t rd = read(stream->fd, buf + done, left); 385 349 386 if (rd < 0) { 387 /* errno was set by read() */ 350 if (rd < 0) 388 351 stream->error = true; 389 } else if (rd == 0) {352 else if (rd == 0) 390 353 stream->eof = true; 391 }else {354 else { 392 355 left -= rd; 393 356 done += rd; … … 404 367 * @param nmemb Number of records to write. 405 368 * @param stream Pointer to the stream. 406 *407 * @return Number of elements successfully written. On error this is less than408 * nmemb, stream error indicator is set and errno is set.409 369 */ 410 370 static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream) … … 412 372 size_t left; 413 373 size_t done; 414 int rc;415 374 416 375 if (size == 0 || nmemb == 0) … … 422 381 while ((left > 0) && (!stream->error)) { 423 382 ssize_t wr; 424 size_t uwr; 425 426 if (stream->kio) { 427 uwr = 0; 428 rc = kio_write(buf + done, left, &uwr); 429 if (rc != EOK) 430 errno = rc; 431 } else { 383 384 if (stream->klog) 385 wr = klog_write(buf + done, left); 386 else 432 387 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 } 442 443 if (rc != EOK) { 444 /* errno was set above */ 388 389 if (wr <= 0) 445 390 stream->error = true; 446 }else {447 left -= uwr;448 done += uwr;391 else { 392 left -= wr; 393 done += wr; 449 394 } 450 395 } … … 456 401 } 457 402 458 /** Read some data in stream buffer. 459 * 460 * On error, stream error indicator is set and errno is set. 461 */ 403 /** Read some data in stream buffer. */ 462 404 static void _ffillbuf(FILE *stream) 463 405 { … … 468 410 rc = read(stream->fd, stream->buf, stream->buf_size); 469 411 if (rc < 0) { 470 /* errno was set by read() */471 412 stream->error = true; 472 413 return; … … 493 434 494 435 /* If buffer has prefetched read data, we need to seek back. */ 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 } 436 if (bytes_used > 0 && stream->buf_state == _bs_read) 437 lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR); 504 438 505 439 /* If buffer has unwritten data, we need to write them out. */ 506 if (bytes_used > 0 && stream->buf_state == _bs_write) {440 if (bytes_used > 0 && stream->buf_state == _bs_write) 507 441 (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 }512 442 513 443 stream->buf_head = stream->buf; … … 536 466 return 0; 537 467 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 549 468 /* If not buffered stream, read in directly. */ 550 469 if (stream->btype == _IONBF) { 551 total_read += _fread(dest, 1, bytes_left, stream);552 return total_read / size;470 now = _fread(dest, size, nmemb, stream); 471 return now; 553 472 } 554 473 … … 563 482 } 564 483 484 bytes_left = size * nmemb; 485 total_read = 0; 486 dp = (uint8_t *) dest; 487 565 488 while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) { 566 489 if (stream->buf_head == stream->buf_tail) 567 490 _ffillbuf(stream); 568 491 569 if (stream->error || stream->eof) { 570 /* On error errno was set by _ffillbuf() */ 492 if (stream->error || stream->eof) 571 493 break; 572 }573 494 574 495 data_avail = stream->buf_head - stream->buf_tail; … … 626 547 _fflushbuf(stream); 627 548 549 628 550 /* Perform lazy allocation of stream buffer. */ 629 551 if (stream->buf == NULL) { … … 662 584 /* Only need to drain buffer. */ 663 585 _fflushbuf(stream); 664 if (!stream->error) 665 need_flush = false; 586 need_flush = false; 666 587 } 667 588 } … … 697 618 int fputs(const char *str, FILE *stream) 698 619 { 699 (void) fwrite(str, str_size(str), 1, stream); 700 if (ferror(stream)) 701 return EOF; 702 return 0; 620 return fwrite(str, str_size(str), 1, stream); 703 621 } 704 622 … … 756 674 } 757 675 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 773 676 int fseek(FILE *stream, off64_t offset, int whence) 774 677 { 775 678 off64_t rc; 776 679 777 if (stream->error)778 return EOF;779 780 680 _fflushbuf(stream); 781 if (stream->error) {782 /* errno was set by _fflushbuf() */783 return EOF;784 }785 786 stream->ungetc_chars = 0;787 681 788 682 rc = lseek(stream->fd, offset, whence); 789 683 if (rc == (off64_t) (-1)) { 790 /* errno has been set by lseek ()*/791 return EOF;684 /* errno has been set by lseek64. */ 685 return -1; 792 686 } 793 687 … … 798 692 off64_t ftell(FILE *stream) 799 693 { 800 off64_t pos;801 802 if (stream->error)803 return EOF;804 805 694 _fflushbuf(stream); 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; 695 return lseek(stream->fd, 0, SEEK_CUR); 818 696 } 819 697 … … 825 703 int fflush(FILE *stream) 826 704 { 827 if (stream->error)828 return EOF;829 830 705 _fflushbuf(stream); 831 if (stream->error) { 832 /* errno was set by _fflushbuf() */ 833 return EOF; 834 } 835 836 if (stream->kio) { 837 kio_update(); 838 return 0; 706 707 if (stream->klog) { 708 klog_update(); 709 return EOK; 839 710 } 840 711 … … 845 716 */ 846 717 stream->need_sync = false; 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; 718 return fsync(stream->fd); 719 } 720 721 return ENOENT; 856 722 } 857 723 … … 874 740 int fileno(FILE *stream) 875 741 { 876 if (stream->k io) {742 if (stream->klog) { 877 743 errno = EBADF; 878 return EOF;744 return -1; 879 745 } 880 746 … … 882 748 } 883 749 884 async_sess_t * vfs_fsession(FILE *stream, iface_t iface)750 async_sess_t *fsession(exch_mgmt_t mgmt, FILE *stream) 885 751 { 886 752 if (stream->fd >= 0) { 887 753 if (stream->sess == NULL) 888 stream->sess = vfs_fd_session(stream->fd, iface);754 stream->sess = fd_session(mgmt, stream->fd); 889 755 890 756 return stream->sess; … … 894 760 } 895 761 896 int vfs_fhandle(FILE *stream, int *handle)762 int fhandle(FILE *stream, int *handle) 897 763 { 898 764 if (stream->fd >= 0) {
Note:
See TracChangeset
for help on using the changeset viewer.