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