Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/io/io.c

    r47b7006 r76d6169  
    4444#include <io/klog.h>
    4545#include <vfs/vfs.h>
     46#include <vfs/vfs_sess.h>
    4647#include <ipc/devmap.h>
    4748#include <adt/list.h>
    4849#include "../private/io.h"
     50#include "../private/stdio.h"
    4951
    5052static void _ffillbuf(FILE *stream);
     
    5658        .eof = true,
    5759        .klog = false,
    58         .phone = -1,
     60        .sess = NULL,
    5961        .btype = _IONBF,
    6062        .buf = NULL,
     
    7072        .eof = false,
    7173        .klog = true,
    72         .phone = -1,
     74        .sess = NULL,
    7375        .btype = _IOLBF,
    7476        .buf = NULL,
     
    8486        .eof = false,
    8587        .klog = true,
    86         .phone = -1,
     88        .sess = NULL,
    8789        .btype = _IONBF,
    8890        .buf = NULL,
     
    99101static LIST_INITIALIZE(files);
    100102
    101 void __stdio_init(int filc, fdi_node_t *filv[])
     103void __stdio_init(int filc)
    102104{
    103105        if (filc > 0) {
    104                 stdin = fopen_node(filv[0], "r");
     106                stdin = fdopen(0, "r");
    105107        } else {
    106108                stdin = &stdin_null;
     
    109111       
    110112        if (filc > 1) {
    111                 stdout = fopen_node(filv[1], "w");
     113                stdout = fdopen(1, "w");
    112114        } else {
    113115                stdout = &stdout_klog;
     
    116118       
    117119        if (filc > 2) {
    118                 stderr = fopen_node(filv[2], "w");
     120                stderr = fdopen(2, "w");
    119121        } else {
    120122                stderr = &stderr_klog;
     
    125127void __stdio_done(void)
    126128{
    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);
    131131                fclose(file);
    132                 link = files.next;
    133132        }
    134133}
     
    173172                }
    174173                *flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
     174                break;
    175175        default:
    176176                errno = EINVAL;
     
    254254        stream->eof = false;
    255255        stream->klog = false;
    256         stream->phone = -1;
     256        stream->sess = NULL;
    257257        stream->need_sync = false;
    258258        _setvbuf(stream);
     
    276276        stream->eof = false;
    277277        stream->klog = false;
    278         stream->phone = -1;
     278        stream->sess = NULL;
    279279        stream->need_sync = false;
    280280        _setvbuf(stream);
     
    285285}
    286286
    287 FILE *fopen_node(fdi_node_t *node, const char *mode)
    288 {
    289         int flags;
    290         if (!parse_mode(mode, &flags))
    291                 return NULL;
    292        
    293         /* Open file. */
    294         FILE *stream = malloc(sizeof(FILE));
    295         if (stream == NULL) {
    296                 errno = ENOMEM;
    297                 return NULL;
    298         }
    299        
    300         stream->fd = open_node(node, flags);
    301         if (stream->fd < 0) {
    302                 /* errno was set by open_node() */
    303                 free(stream);
    304                 return NULL;
    305         }
    306        
    307         stream->error = false;
    308         stream->eof = false;
    309         stream->klog = false;
    310         stream->phone = -1;
    311         stream->need_sync = false;
    312         _setvbuf(stream);
    313        
    314         list_append(&stream->link, &files);
    315        
    316         return stream;
    317 }
    318 
    319287int fclose(FILE *stream)
    320288{
     
    323291        fflush(stream);
    324292       
    325         if (stream->phone >= 0)
    326                 async_hangup(stream->phone);
     293        if (stream->sess != NULL)
     294                async_hangup(stream->sess);
    327295       
    328296        if (stream->fd >= 0)
     
    594562                }
    595563               
    596                 buf += now;
     564                data += now;
    597565                stream->buf_head += now;
    598566                buf_free -= now;
    599567                bytes_left -= now;
    600568                total_written += now;
     569                stream->buf_state = _bs_write;
    601570               
    602571                if (buf_free == 0) {
     
    606575                }
    607576        }
    608        
    609         if (total_written > 0)
    610                 stream->buf_state = _bs_write;
    611577
    612578        if (need_flush)
     
    714680off64_t ftell(FILE *stream)
    715681{
     682        _fflushbuf(stream);
    716683        return lseek(stream->fd, 0, SEEK_CUR);
    717684}
     
    731698        }
    732699       
    733         if (stream->fd >= 0 && stream->need_sync) {
     700        if ((stream->fd >= 0) && (stream->need_sync)) {
    734701                /**
    735702                 * Better than syncing always, but probably still not the
     
    769736}
    770737
    771 int fphone(FILE *stream)
     738async_sess_t *fsession(exch_mgmt_t mgmt, FILE *stream)
    772739{
    773740        if (stream->fd >= 0) {
    774                 if (stream->phone < 0)
    775                         stream->phone = fd_phone(stream->fd);
    776                
    777                 return stream->phone;
    778         }
    779        
    780         return -1;
    781 }
    782 
    783 int fnode(FILE *stream, fdi_node_t *node)
    784 {
    785         if (stream->fd >= 0)
    786                 return fd_node(stream->fd, node);
     741                if (stream->sess == NULL)
     742                        stream->sess = fd_session(mgmt, stream->fd);
     743               
     744                return stream->sess;
     745        }
     746       
     747        return NULL;
     748}
     749
     750int fhandle(FILE *stream, int *handle)
     751{
     752        if (stream->fd >= 0) {
     753                *handle = stream->fd;
     754                return EOK;
     755        }
    787756       
    788757        return ENOENT;
Note: See TracChangeset for help on using the changeset viewer.