os161-1.99
 All Data Structures
file_syscalls.c
00001 #include <types.h>
00002 #include <kern/errno.h>
00003 #include <kern/unistd.h>
00004 #include <lib.h>
00005 #include <uio.h>
00006 #include <syscall.h>
00007 #include <vnode.h>
00008 #include <vfs.h>
00009 #include <current.h>
00010 #include <proc.h>
00011 
00012 /* handler for write() system call                  */
00013 /*
00014  * n.b.
00015  * This implementation handles only writes to standard output 
00016  * and standard error, both of which go to the console.
00017  * Also, it does not provide any synchronization, so writes
00018  * are not atomic.
00019  *
00020  * You will need to improve this implementation
00021  */
00022 
00023 int
00024 sys_write(int fdesc,userptr_t ubuf,unsigned int nbytes,int *retval)
00025 {
00026   struct iovec iov;
00027   struct uio u;
00028   int res;
00029 
00030   DEBUG(DB_SYSCALL,"Syscall: write(%d,%x,%d)\n",fdesc,(unsigned int)ubuf,nbytes);
00031   
00032   /* only stdout and stderr writes are currently implemented */
00033   if (!((fdesc==STDOUT_FILENO)||(fdesc==STDERR_FILENO))) {
00034     return EUNIMP;
00035   }
00036   KASSERT(curproc != NULL);
00037   KASSERT(curproc->console != NULL);
00038   KASSERT(curproc->p_addrspace != NULL);
00039 
00040   /* set up a uio structure to refer to the user program's buffer (ubuf) */
00041   iov.iov_ubase = ubuf;
00042   iov.iov_len = nbytes;
00043   u.uio_iov = &iov;
00044   u.uio_iovcnt = 1;
00045   u.uio_offset = 0;  /* not needed for the console */
00046   u.uio_resid = nbytes;
00047   u.uio_segflg = UIO_USERSPACE;
00048   u.uio_rw = UIO_WRITE;
00049   u.uio_space = curproc->p_addrspace;
00050 
00051   res = VOP_WRITE(curproc->console,&u);
00052   if (res) {
00053     return res;
00054   }
00055 
00056   /* pass back the number of bytes actually written */
00057   *retval = nbytes - u.uio_resid;
00058   KASSERT(*retval >= 0);
00059   return 0;
00060 }
 All Data Structures