root/kern/syscall/file_syscalls.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_write

   1 #include <types.h>
   2 #include <kern/errno.h>
   3 #include <kern/unistd.h>
   4 #include <lib.h>
   5 #include <uio.h>
   6 #include <syscall.h>
   7 #include <vnode.h>
   8 #include <vfs.h>
   9 #include <current.h>
  10 #include <proc.h>
  11 
  12 /* handler for write() system call                  */
  13 /*
  14  * n.b.
  15  * This implementation handles only writes to standard output 
  16  * and standard error, both of which go to the console.
  17  * Also, it does not provide any synchronization, so writes
  18  * are not atomic.
  19  *
  20  * You will need to improve this implementation
  21  */
  22 
  23 int
  24 sys_write(int fdesc,userptr_t ubuf,unsigned int nbytes,int *retval)
  25 {
  26   struct iovec iov;
  27   struct uio u;
  28   int res;
  29 
  30   DEBUG(DB_SYSCALL,"Syscall: write(%d,%x,%d)\n",fdesc,(unsigned int)ubuf,nbytes);
  31   
  32   /* only stdout and stderr writes are currently implemented */
  33   if (!((fdesc==STDOUT_FILENO)||(fdesc==STDERR_FILENO))) {
  34     return EUNIMP;
  35   }
  36   KASSERT(curproc != NULL);
  37   KASSERT(curproc->console != NULL);
  38   KASSERT(curproc->p_addrspace != NULL);
  39 
  40   /* set up a uio structure to refer to the user program's buffer (ubuf) */
  41   iov.iov_ubase = ubuf;
  42   iov.iov_len = nbytes;
  43   u.uio_iov = &iov;
  44   u.uio_iovcnt = 1;
  45   u.uio_offset = 0;  /* not needed for the console */
  46   u.uio_resid = nbytes;
  47   u.uio_segflg = UIO_USERSPACE;
  48   u.uio_rw = UIO_WRITE;
  49   u.uio_space = curproc->p_addrspace;
  50 
  51   res = VOP_WRITE(curproc->console,&u);
  52   if (res) {
  53     return res;
  54   }
  55 
  56   /* pass back the number of bytes actually written */
  57   *retval = nbytes - u.uio_resid;
  58   KASSERT(*retval >= 0);
  59   return 0;
  60 }

/* [<][>][^][v][top][bottom][index][help] */