Subversion Repositories shark

Rev

Rev 2 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed


#include <fs/types.h>
#include <fs/errno.h>
#include <fs/task.h>
#include <fs/maccess.h>
#include <fs/fcntl.h>

#include "file.h"
#include "inode.h"
#include "dentry.h"
#include "fileop.h"
#include "rwlock.h"
#include "fs.h"

#define EBOH EIO

/* temporary: for console i/o */
#include <ll/i386/cons.h>
#include <drivers/keyb.h>
static int __getch(int flag)
{
  KEY_EVT key;
  for (;;) {
    if (!keyb_getcode(&key,flag)) return -1;
    if (isScanCode(key)) continue;
    if (isLeftCtrl(key)||isRightCtrl(key)) {
      if (key.ascii>='a'&&key.ascii<='z') return key.ascii-'a'+1;
      if (key.ascii>='A'&&key.ascii<='Z') return key.ascii-'A'+1;
    }
    if (key.ascii==0x0d) return 0x0a; // 'Enter' is '\n'
    return key.ascii;
  }
}

__ssize_t k_read(int fd, __ptr_t buf, __ssize_t nbytes)
{
  __pid_t pid;
  __ssize_t sz;

  call_to_fs();
 
  /* TO FIX! */  
  if (fd>=0&&fd<=2) {    
    __ssize_t x;
    int ret,ch;
    char *c;    
 
    //printk(KERN_INFO "reading from console");

    /* WARNIG buf is a pointer into user-space
     * we are using it without cast
     */


    /* WARNIG there is a blocking call...
     * the system can go down if it is blocked!
     */

   
    if (fd!=0) return_from_fs(-EBADF);

    c=(char*)buf;
    x=0;    
    while (x<nbytes) {
      ch=__getch(NON_BLOCK);
      if (ch!=-1) *c++=ch;
      else {
        if (x>0) break;
        release_sys_call();
        ch=__getch(BLOCK);
        ret=reacquire_sys_call();
        if (ret) return -ENOSYS;
        *c++=ch;
      }
      x++;
      cputc(ch);
    }
   
    return_from_fs(x);
  }
 
  /* don't try to use bad file descriptor */
  pid=__get_pid();
  if (check_fd(pid,fd)) {
    printk(KERN_INFO "k_read: using bad file descriptor");
    return_from_fs(-EBADF);
  }

  /* don't try to read a directory */
  if (file_ptr(pid,fd)->f_flag_isdir) return_from_fs(-EBADF);

  /* don't try to read into not user memory */
  if (!__verify_write(buf,nbytes)) return_from_fs(-EVERIFY);

  /* if in write only mode */
  if (*fildesfflags_ptr(pid,fd)&O_WRONLY)
    return_from_fs(-EBOH);

  /* cancellation point */
  //fs_test_cancellation(-EINTR);
 
  /* acquire read lock on inode's file */
  __rwlock_rdlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);

  /* O_APPEND flags */
  if (*fildesfflags_ptr(pid,fd)&O_APPEND) {
    sz=0;
    goto SKIP;
  }

  /* WARNING buf is a pointer into user-space! */
  sz=file_ptr(pid,fd)->f_op->read(file_ptr(pid,fd),buf,nbytes);
 
  /* release the lock */
SKIP:
  __rwlock_rdunlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
 
  return_from_fs(sz);
}