Changeset ff9244a in mainline


Ignore:
Timestamp:
2007-12-26T22:16:21Z (17 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2f02aa17
Parents:
75ae31d
Message:

Move the open(), read() and write() provided by streams.c away so that
these functions won't clash with the real open(), read() and write(), which are
soon to be provided by a libvfs library. Applications can now use open_stdin(),
open_stdout(), read_stdin() and write_stdout(). Later, there might be an option
of providing a custom console file system, which will work similarly to how
streams' open(), read() and write() worked.

Location:
uspace
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tetris/screen.c

    r75ae31d rff9244a  
    119119scr_init(void)
    120120{
    121         con_phone = get_fd_phone(1);
     121        con_phone = get_cons_phone();
    122122        async_msg_1(con_phone, CONSOLE_CURSOR_VISIBILITY, 0);
    123123        resume_normal();
  • uspace/lib/libc/generic/io/io.c

    r75ae31d rff9244a  
    4848       
    4949        for (count = 0; str[count] != 0; count++);
    50         if (write(1, (void *) str, count) == count) {
    51                 if (write(1, &nl, 1) == 1)
     50        if (write_stdout((void *) str, count) == count) {
     51                if (write_stdout(&nl, 1) == 1)
    5252                        return 0;
    5353        }
     
    6363int putnchars(const char *buf, size_t count)
    6464{
    65         if (write(1, (void *) buf, count) == count)
    66                         return 0;
     65        if (write_stdout((void *) buf, count) == count)
     66                return 0;
    6767       
    6868        return EOF;
     
    8080
    8181        for (count = 0; str[count] != 0; count++);
    82         if (write(1, (void *) str, count) == count)
    83                         return 0;
     82        if (write_stdout((void *) str, count) == count)
     83                return 0;
    8484       
    8585        return EOF;
     
    8989{
    9090        unsigned char ch = c;
    91         if (write(1, (void *) &ch, 1) == 1)
    92                         return c;
     91        if (write_stdout((void *) &ch, 1) == 1)
     92                return c;
    9393       
    9494        return EOF;
     
    9898{
    9999        unsigned char c;
    100         if (read(0, (void *) &c, 1) == 1)
    101                         return c;
     100        if (read_stdin((void *) &c, 1) == 1)
     101                return c;
    102102       
    103103        return EOF;
  • uspace/lib/libc/generic/io/stream.c

    r75ae31d rff9244a  
    4848#include <sys/types.h>
    4949
    50 #define FDS 32
     50static int console_phone = -1;
    5151
    52 typedef struct stream_t {
    53         pwritefn_t w;
    54         preadfn_t r;
    55         void * param;
    56         int phone;
    57 } stream_t;
    58 
    59 static int console_phone = -1;
    60 static stream_t streams[FDS];
    61 
    62 static ssize_t write_stderr(void *param, const void *buf, size_t count)
     52ssize_t write_stderr(const void *buf, size_t count)
    6353{
    6454        return count;
    6555}
    6656
    67 static ssize_t read_stdin(void *param, void *buf, size_t count)
     57ssize_t read_stdin(void *buf, size_t count)
    6858{
    6959        ipcarg_t r0, r1;
     
    7161
    7262        while (i < count) {
    73                 if (async_req_0_2(streams[0].phone, CONSOLE_GETCHAR, &r0,
     63                if (async_req_0_2(console_phone, CONSOLE_GETCHAR, &r0,
    7464                    &r1) < 0) {
    7565                        return -1;
     
    8070}
    8171
    82 static ssize_t write_stdout(void *param, const void *buf, size_t count)
     72ssize_t write_stdout(const void *buf, size_t count)
    8373{
    8474        int i;
    8575
    8676        for (i = 0; i < count; i++)
    87                 async_msg_1(streams[1].phone, CONSOLE_PUTCHAR,
     77                async_msg_1(console_phone, CONSOLE_PUTCHAR,
    8878                    ((const char *) buf)[i]);
    8979       
     
    9181}
    9282
    93 static stream_t open_stdin(void)
     83void open_stdin(void)
    9484{
    95         stream_t stream;
    96        
    9785        if (console_phone < 0) {
    9886                while ((console_phone = ipc_connect_me_to(PHONE_NS,
     
    10189                }
    10290        }
    103        
    104         stream.r = read_stdin;
    105         stream.w = NULL;
    106         stream.param = 0;
    107         stream.phone = console_phone;
    108        
    109         return stream;
    11091}
    11192
    112 static stream_t open_stdout(void)
     93void open_stdout(void)
    11394{
    114         stream_t stream;
    115 
    11695        if (console_phone < 0) {
    11796                while ((console_phone = ipc_connect_me_to(PHONE_NS,
     
    12099                }
    121100        }
    122        
    123         stream.r = NULL;
    124         stream.w = write_stdout;
    125         stream.phone = console_phone;
    126         stream.param = 0;
    127        
    128         return stream;
    129101}
    130102
    131 static ssize_t write_null(void *param, const void *buf, size_t count)
     103int get_cons_phone(void)
    132104{
    133         return count;
    134 }
    135 
    136 fd_t open(const char *fname, int flags)
    137 {
    138         int c = 0;
    139 
    140         while (((streams[c].w) || (streams[c].r)) && (c < FDS))
    141                 c++;
    142        
    143         if (c == FDS)
    144                 return EMFILE;
    145        
    146         if (!strcmp(fname, "stdin")) {
    147                 streams[c] = open_stdin();
    148                 return c;
    149         }
    150        
    151         if (!strcmp(fname, "stdout")) {
    152                 streams[c] = open_stdout();
    153                 return c;
    154         }
    155        
    156         if (!strcmp(fname, "stderr")) {
    157                 streams[c].w = write_stderr;
    158                 return c;
    159         }
    160        
    161         if (!strcmp(fname, "null")) {
    162                 streams[c].w = write_null;
    163                 return c;
    164         }
    165        
    166         return -1;
    167 }
    168 
    169 
    170 ssize_t write(int fd, const void *buf, size_t count)
    171 {
    172         if (fd < FDS && streams[fd].w)
    173                 return streams[fd].w(streams[fd].param, buf, count);
    174        
    175         return 0;
    176 }
    177 
    178 ssize_t read(int fd, void *buf, size_t count)
    179 {
    180         if (fd < FDS && streams[fd].r)
    181                 return streams[fd].r(streams[fd].param, buf, count);
    182        
    183         return 0;
    184 }
    185 
    186 int get_fd_phone(int fd)
    187 {
    188         if (fd >= FDS || fd < 0)
    189                 return -1;
    190         return streams[fd].phone;
     105        return console_phone;
    191106}
    192107
  • uspace/lib/libc/generic/io/vprintf.c

    r75ae31d rff9244a  
    4444static int vprintf_write(const char *str, size_t count, void *unused)
    4545{
    46         return write(1, str, count);
     46        return write_stdout(str, count);
    4747}
    4848
  • uspace/lib/libc/generic/libc.c

    r75ae31d rff9244a  
    6969void __io_init(void)
    7070{
    71         open("stdin", 0);
    72         open("stdout", 0);
    73         open("stderr", 0);
     71        open_stdin();
     72        open_stdout();
    7473}
    7574
  • uspace/lib/libc/include/io/stream.h

    r75ae31d rff9244a  
    4040#define EMFILE -17
    4141
    42 typedef int fd_t;
     42extern void open_stdin(void);
     43extern void open_stdout(void);
    4344
    44 typedef ssize_t (*pwritefn_t)(void *, const void *, size_t);
    45 typedef ssize_t (*preadfn_t)(void *, void *, size_t);
     45extern ssize_t read_stdin(void *, size_t);
     46extern ssize_t write_stdout(const void *, size_t);
     47extern ssize_t write_stderr(const void *, size_t);
    4648
    47 fd_t open(const char *fname, int flags);
    48 int get_fd_phone(int fd);
     49extern int get_cons_phone(void);
    4950
    5051#endif
Note: See TracChangeset for help on using the changeset viewer.