Changeset a68f737 in mainline for uspace/lib/libc/generic/io/io.c


Ignore:
Timestamp:
2009-06-08T12:34:38Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d9c8c81
Parents:
f8ef660
Message:

keep a list of open files to support proper cleanup

File:
1 edited

Legend:

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

    rf8ef660 ra68f737  
    4343#include <vfs/vfs.h>
    4444#include <ipc/devmap.h>
    45 
    46 FILE stdin_null = {
     45#include <libadt/list.h>
     46
     47static FILE stdin_null = {
    4748        .fd = -1,
    4849        .error = true,
     
    5253};
    5354
    54 FILE stdout_klog = {
     55static FILE stdout_klog = {
    5556        .fd = -1,
    5657        .error = false,
     
    6061};
    6162
    62 FILE *stdin = &stdin_null;
    63 FILE *stdout = &stdout_klog;
    64 FILE *stderr = &stdout_klog;
     63static FILE stderr_klog = {
     64        .fd = -1,
     65        .error = false,
     66        .eof = false,
     67        .klog = true,
     68        .phone = -1
     69};
     70
     71FILE *stdin = NULL;
     72FILE *stdout = NULL;
     73FILE *stderr = NULL;
     74
     75static LIST_INITIALIZE(files);
     76
     77void stdio_init(int filc, fdi_node_t *filv[])
     78{
     79        if (filc > 0) {
     80                stdin = fopen_node(filv[0], "r");
     81        } else {
     82                stdin = &stdin_null;
     83                list_append(&stdin->link, &files);
     84        }
     85       
     86        if (filc > 1) {
     87                stdout = fopen_node(filv[1], "w");
     88        } else {
     89                stdout = &stdout_klog;
     90                list_append(&stdout->link, &files);
     91        }
     92       
     93        if (filc > 2) {
     94                stderr = fopen_node(filv[2], "w");
     95        } else {
     96                stderr = &stderr_klog;
     97                list_append(&stderr->link, &files);
     98        }
     99}
     100
     101void stdio_done(void)
     102{
     103        link_t *link = files.next;
     104       
     105        while (link != &files) {
     106                FILE *file = list_get_instance(link, FILE, link);
     107                fclose(file);
     108                link = files.next;
     109        }
     110}
    65111
    66112static bool parse_mode(const char *mode, int *flags)
     
    142188        stream->phone = -1;
    143189       
     190        list_append(&stream->link, &files);
     191       
    144192        return stream;
    145193}
     
    170218        stream->phone = -1;
    171219       
     220        list_append(&stream->link, &files);
     221       
    172222        return stream;
    173223}
     
    185235                rc = close(stream->fd);
    186236       
    187         if ((stream != &stdin_null) && (stream != &stdout_klog))
     237        list_remove(&stream->link);
     238       
     239        if ((stream != &stdin_null)
     240            && (stream != &stdout_klog)
     241            && (stream != &stderr_klog))
    188242                free(stream);
    189243       
     
    354408}
    355409
    356 void fnode(FILE *stream, fdi_node_t *node)
    357 {
    358         if (stream->fd >= 0) {
    359                 fd_node(stream->fd, node);
    360         } else {
    361                 node->fs_handle = 0;
    362                 node->dev_handle = 0;
    363                 node->index = 0;
    364         }
     410int fnode(FILE *stream, fdi_node_t *node)
     411{
     412        if (stream->fd >= 0)
     413                return fd_node(stream->fd, node);
     414       
     415        return ENOENT;
    365416}
    366417
Note: See TracChangeset for help on using the changeset viewer.