]> Devi Nivas Git - cs3210-lab1.git/commitdiff
devsw
authorkaashoek <kaashoek>
Wed, 9 Aug 2006 16:04:04 +0000 (16:04 +0000)
committerkaashoek <kaashoek>
Wed, 9 Aug 2006 16:04:04 +0000 (16:04 +0000)
checkpoint: write(fd,"hello\n",6) where fd is a console dev almost works

console.c
defs.h
dev.h [new file with mode: 0644]
fd.c
fs.c
main.c
param.h
syscall.c
userfs.c

index 9a7d7254607364bf32bbc3b2a5a09a64e2204cc6..7e357fbcaa36f84bda66817264d2c75a011a0ff5 100644 (file)
--- a/console.c
+++ b/console.c
@@ -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;
+}
diff --git a/defs.h b/defs.h
index 30b803d05b5f850c9048e3ceb6ee68d7a7138f62..728b2a47259851497787c9d615c815760216b75d 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -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);
diff --git a/dev.h b/dev.h
new file mode 100644 (file)
index 0000000..48c6b9a
--- /dev/null
+++ b/dev.h
@@ -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
diff --git a/fd.c b/fd.c
index 8332454fab5bdde0a8609eca45906e16684da99c..47b0f204e58397fcc56f15e571a3931030a6e79d 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -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;
diff --git a/fs.c b/fs.c
index cf530a99b02de814906f0a5335f9bf567d64eef6..d921221a325f2bb968be60a31221b44141b8915f 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -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)
 {
diff --git a/main.c b/main.c
index c6e27a534d14a52dbe6ce3f304c1b449e388aa29..f0ca80e78766d7246c3a423f0c58d5748a8679e8 100644 (file)
--- a/main.c
+++ b/main.c
@@ -72,6 +72,7 @@ main0(void)
   setupsegs(p);
 
   // init disk device
+  console_init();
   ide_init(); 
 
   mp_startthem();
diff --git a/param.h b/param.h
index 9b5c5e82f60af4067beb1ba983be54ac0bb3d337..c8d15b73ba7360ad399a4098061df6ca9a88ef1d 100644 (file)
--- a/param.h
+++ b/param.h
@@ -7,3 +7,4 @@
 #define NREQUEST 100 // outstanding disk requests
 #define NBUF 10
 #define NINODE 100
+#define NDEV 10
index ce6e22d0b22da25dfcf5c6919145343830063a37..f0f2cc24f5ae2cc130c141377eac3fa0a07c3a68 100644 (file)
--- a/syscall.c
+++ b/syscall.c
@@ -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;
index b11f3ebb7ec266f5c1b9977b3f0acb4811af4351..dcdffbbeb8de8a7a87a27e0cee59e6d046cb87b5 100644 (file)
--- a/userfs.c
+++ b/userfs.c
@@ -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){