Changes in uspace/lib/c/generic/io/io.c [82582e4:2f72c67a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/io.c
r82582e4 r2f72c67a 44 44 #include <io/klog.h> 45 45 #include <vfs/vfs.h> 46 #include <ipc/devmap.h> 46 #include <vfs/vfs_sess.h> 47 #include <ipc/loc.h> 47 48 #include <adt/list.h> 48 49 #include "../private/io.h" 50 #include "../private/stdio.h" 49 51 50 52 static void _ffillbuf(FILE *stream); … … 56 58 .eof = true, 57 59 .klog = false, 58 . phone = -1,60 .sess = NULL, 59 61 .btype = _IONBF, 60 62 .buf = NULL, … … 70 72 .eof = false, 71 73 .klog = true, 72 . phone = -1,74 .sess = NULL, 73 75 .btype = _IOLBF, 74 76 .buf = NULL, … … 84 86 .eof = false, 85 87 .klog = true, 86 . phone = -1,88 .sess = NULL, 87 89 .btype = _IONBF, 88 90 .buf = NULL, … … 99 101 static LIST_INITIALIZE(files); 100 102 101 void __stdio_init(int filc , fdi_node_t *filv[])103 void __stdio_init(int filc) 102 104 { 103 105 if (filc > 0) { 104 stdin = f open_node(filv[0], "r");106 stdin = fdopen(0, "r"); 105 107 } else { 106 108 stdin = &stdin_null; … … 109 111 110 112 if (filc > 1) { 111 stdout = f open_node(filv[1], "w");113 stdout = fdopen(1, "w"); 112 114 } else { 113 115 stdout = &stdout_klog; … … 116 118 117 119 if (filc > 2) { 118 stderr = f open_node(filv[2], "w");120 stderr = fdopen(2, "w"); 119 121 } else { 120 122 stderr = &stderr_klog; … … 125 127 void __stdio_done(void) 126 128 { 127 link_t *link = files.next; 128 129 while (link != &files) { 130 FILE *file = list_get_instance(link, FILE, link); 129 while (!list_empty(&files)) { 130 FILE *file = list_get_instance(list_first(&files), FILE, link); 131 131 fclose(file); 132 link = files.next;133 132 } 134 133 } … … 255 254 stream->eof = false; 256 255 stream->klog = false; 257 stream-> phone = -1;256 stream->sess = NULL; 258 257 stream->need_sync = false; 259 258 _setvbuf(stream); … … 277 276 stream->eof = false; 278 277 stream->klog = false; 279 stream-> phone = -1;278 stream->sess = NULL; 280 279 stream->need_sync = false; 281 280 _setvbuf(stream); … … 286 285 } 287 286 288 FILE *fopen_node(fdi_node_t *node, const char *mode)289 {290 int flags;291 if (!parse_mode(mode, &flags))292 return NULL;293 294 /* Open file. */295 FILE *stream = malloc(sizeof(FILE));296 if (stream == NULL) {297 errno = ENOMEM;298 return NULL;299 }300 301 stream->fd = open_node(node, flags);302 if (stream->fd < 0) {303 /* errno was set by open_node() */304 free(stream);305 return NULL;306 }307 308 stream->error = false;309 stream->eof = false;310 stream->klog = false;311 stream->phone = -1;312 stream->need_sync = false;313 _setvbuf(stream);314 315 list_append(&stream->link, &files);316 317 return stream;318 }319 320 287 int fclose(FILE *stream) 321 288 { … … 324 291 fflush(stream); 325 292 326 if (stream-> phone >= 0)327 async_hangup(stream-> phone);293 if (stream->sess != NULL) 294 async_hangup(stream->sess); 328 295 329 296 if (stream->fd >= 0) … … 451 418 452 419 bytes_used = stream->buf_head - stream->buf_tail; 453 if (bytes_used == 0)454 return;455 420 456 421 /* If buffer has prefetched read data, we need to seek back. */ 457 if ( stream->buf_state == _bs_read)422 if (bytes_used > 0 && stream->buf_state == _bs_read) 458 423 lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR); 459 424 460 425 /* If buffer has unwritten data, we need to write them out. */ 461 if ( stream->buf_state == _bs_write)426 if (bytes_used > 0 && stream->buf_state == _bs_write) 462 427 (void) _fwrite(stream->buf_tail, 1, bytes_used, stream); 463 428 … … 595 560 } 596 561 597 buf+= now;562 data += now; 598 563 stream->buf_head += now; 599 564 buf_free -= now; 600 565 bytes_left -= now; 601 566 total_written += now; 567 stream->buf_state = _bs_write; 602 568 603 569 if (buf_free == 0) { … … 607 573 } 608 574 } 609 610 if (total_written > 0)611 stream->buf_state = _bs_write;612 575 613 576 if (need_flush) … … 715 678 off64_t ftell(FILE *stream) 716 679 { 680 _fflushbuf(stream); 717 681 return lseek(stream->fd, 0, SEEK_CUR); 718 682 } … … 732 696 } 733 697 734 if ( stream->fd >= 0 && stream->need_sync) {698 if ((stream->fd >= 0) && (stream->need_sync)) { 735 699 /** 736 700 * Better than syncing always, but probably still not the … … 770 734 } 771 735 772 int fphone(FILE *stream)736 async_sess_t *fsession(exch_mgmt_t mgmt, FILE *stream) 773 737 { 774 738 if (stream->fd >= 0) { 775 if (stream->phone < 0) 776 stream->phone = fd_phone(stream->fd); 777 778 return stream->phone; 779 } 780 781 return -1; 782 } 783 784 int fnode(FILE *stream, fdi_node_t *node) 785 { 786 if (stream->fd >= 0) 787 return fd_node(stream->fd, node); 739 if (stream->sess == NULL) 740 stream->sess = fd_session(mgmt, stream->fd); 741 742 return stream->sess; 743 } 744 745 return NULL; 746 } 747 748 int fhandle(FILE *stream, int *handle) 749 { 750 if (stream->fd >= 0) { 751 *handle = stream->fd; 752 return EOK; 753 } 788 754 789 755 return ENOENT;
Note:
See TracChangeset
for help on using the changeset viewer.