diff options
author | kaashoek <kaashoek> | 2006-08-09 16:04:04 +0000 |
---|---|---|
committer | kaashoek <kaashoek> | 2006-08-09 16:04:04 +0000 |
commit | 6fa5ffb56ffdbe5a37bfc04d063fbff2bf929c27 (patch) | |
tree | 7a06e4e145812109fcec97aec32c135632481edf | |
parent | 6c0e444fcdf7ba21442513acbc69c7fca9def06b (diff) | |
download | xv6-labs-6fa5ffb56ffdbe5a37bfc04d063fbff2bf929c27.tar.gz xv6-labs-6fa5ffb56ffdbe5a37bfc04d063fbff2bf929c27.tar.bz2 xv6-labs-6fa5ffb56ffdbe5a37bfc04d063fbff2bf929c27.zip |
devsw
checkpoint: write(fd,"hello\n",6) where fd is a console dev almost works
-rw-r--r-- | console.c | 22 | ||||
-rw-r--r-- | defs.h | 1 | ||||
-rw-r--r-- | dev.h | 10 | ||||
-rw-r--r-- | fd.c | 4 | ||||
-rw-r--r-- | fs.c | 11 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | param.h | 1 | ||||
-rw-r--r-- | syscall.c | 9 | ||||
-rw-r--r-- | userfs.c | 11 |
9 files changed, 68 insertions, 2 deletions
@@ -2,6 +2,7 @@ #include "x86.h" #include "defs.h" #include "spinlock.h" +#include "dev.h" struct spinlock console_lock = { "console" }; int panicked = 0; @@ -155,3 +156,24 @@ panic(char *s) for(;;) ; } + +int +console_write (int minor, void *buf, int n) +{ + int i; + uchar *b = buf; + + cprintf ("print character to console\n"); + + for (i = 0; i < n; i++) { + cons_putc((int) b[i]); + } + + return n; +} + +void +console_init () +{ + devsw[CONSOLE].d_write = console_write; +} @@ -109,4 +109,5 @@ void idecref(struct inode *ip); void iput(struct inode *ip); struct inode * namei(char *path); int readi(struct inode *ip, void *xdst, uint off, uint n); +int writei(struct inode *ip, void *addr, uint n); struct inode *mknod(struct inode *, char *, short, short, short); @@ -0,0 +1,10 @@ +struct devsw { + int (*d_open)(char *, int); + int (*d_read)(int, void *, int); + int (*d_write)(int, void *, int); + int (*d_close)(int); +}; + +extern struct devsw devsw[]; + +#define CONSOLE 1 @@ -6,8 +6,10 @@ #include "defs.h" #include "fd.h" #include "spinlock.h" +#include "dev.h" struct spinlock fd_table_lock; +struct devsw devsw[NDEV]; struct fd fds[NFD]; @@ -56,6 +58,8 @@ fd_write(struct fd *fd, char *addr, int n) return -1; if(fd->type == FD_PIPE){ return pipe_write(fd->pipe, addr, n); + } else if (fd->type == FD_FILE) { + return writei (fd->ip, addr, n); } else { panic("fd_write"); return -1; @@ -8,6 +8,7 @@ #include "buf.h" #include "fs.h" #include "fsvar.h" +#include "dev.h" // these are inodes currently in use // an entry is free if count == 0 @@ -252,6 +253,16 @@ readi(struct inode *ip, void *xdst, uint off, uint n) return target - n; } +int +writei(struct inode *ip, void *addr, uint n) +{ + if (ip->type == T_DEV) { + return devsw[ip->major].d_write (ip->minor, addr, n); + } else { + panic ("writei: unknown type\n"); + } +} + struct inode * namei(char *path) { @@ -72,6 +72,7 @@ main0(void) setupsegs(p); // init disk device + console_init(); ide_init(); mp_startthem(); @@ -7,3 +7,4 @@ #define NREQUEST 100 // outstanding disk requests #define NBUF 10 #define NINODE 100 +#define NDEV 10 @@ -271,8 +271,13 @@ sys_open(void) iunlock(ip); fd->type = FD_FILE; - fd->readable = 1; - fd->writeable = 0; + if (arg1) { + fd->readable = 1; + fd->writeable = 1; + } else { + fd->readable = 1; + fd->writeable = 0; + } fd->ip = ip; fd->off = 0; cp->fds[ufd] = fd; @@ -20,6 +20,17 @@ main(void) puts ("mknod failed\n"); else puts ("made a node\n"); + fd = open("console", 1); + if(fd >= 0){ + puts("open console ok\n"); + close(fd); + } else { + puts("open console failed!\n"); + } + if (write (fd, "hello\n", 6) != 6) { + puts ("write to console failed\n"); + } + close (fd); fd = open("echo", 0); if(fd >= 0){ |