Subversion Repositories shark

Rev

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

Rev Author Line No. Line
2 pj 1
 
2
#include <fs/types.h>
3
#include <fs/errno.h>
4
#include <fs/task.h>
5
#include <fs/maccess.h>
6
#include <fs/fcntl.h>
7
 
8
#include "file.h"
9
#include "inode.h"
10
#include "dentry.h"
11
#include "fileop.h"
12
#include "rwlock.h"
13
#include "fs.h"
14
 
15
#define EBOH EIO
16
 
17
/* temporary: for console i/o */
18
#include <ll/i386/cons.h>
19
#include <drivers/keyb.h>
20
static int __getch(int flag)
21
{
22
  KEY_EVT key;
23
  for (;;) {
24
    if (!keyb_getcode(&key,flag)) return -1;
25
    if (isScanCode(key)) continue;
26
    if (isLeftCtrl(key)||isRightCtrl(key)) {
27
      if (key.ascii>='a'&&key.ascii<='z') return key.ascii-'a'+1;
28
      if (key.ascii>='A'&&key.ascii<='Z') return key.ascii-'A'+1;
29
    }
30
    if (key.ascii==0x0d) return 0x0a; // 'Enter' is '\n'
31
    return key.ascii;
32
  }
33
}
34
 
35
__ssize_t k_read(int fd, __ptr_t buf, __ssize_t nbytes)
36
{
37
  __pid_t pid;
38
  __ssize_t sz;
39
 
40
  call_to_fs();
41
 
42
  /* TO FIX! */  
43
  if (fd>=0&&fd<=2) {    
44
    __ssize_t x;
45
    int ret,ch;
46
    char *c;    
47
 
48
    //printk(KERN_INFO "reading from console");
49
 
50
    /* WARNIG buf is a pointer into user-space
51
     * we are using it without cast
52
     */
53
 
54
    /* WARNIG there is a blocking call...
55
     * the system can go down if it is blocked!
56
     */
57
 
58
    if (fd!=0) return_from_fs(-EBADF);
59
 
60
    c=(char*)buf;
61
    x=0;    
62
    while (x<nbytes) {
63
      ch=__getch(NON_BLOCK);
64
      if (ch!=-1) *c++=ch;
65
      else {
66
        if (x>0) break;
67
        release_sys_call();
68
        ch=__getch(BLOCK);
69
        ret=reacquire_sys_call();
70
        if (ret) return -ENOSYS;
71
        *c++=ch;
72
      }
73
      x++;
74
      cputc(ch);
75
    }
76
 
77
    return_from_fs(x);
78
  }
79
 
80
  /* don't try to use bad file descriptor */
81
  pid=__get_pid();
82
  if (check_fd(pid,fd)) {
83
    printk(KERN_INFO "k_read: using bad file descriptor");
84
    return_from_fs(-EBADF);
85
  }
86
 
87
  /* don't try to read a directory */
88
  if (file_ptr(pid,fd)->f_flag_isdir) return_from_fs(-EBADF);
89
 
90
  /* don't try to read into not user memory */
91
  if (!__verify_write(buf,nbytes)) return_from_fs(-EVERIFY);
92
 
93
  /* if in write only mode */
94
  if (*fildesfflags_ptr(pid,fd)&O_WRONLY)
95
    return_from_fs(-EBOH);
96
 
97
  /* cancellation point */
98
  //fs_test_cancellation(-EINTR);
99
 
100
  /* acquire read lock on inode's file */
101
  __rwlock_rdlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
102
 
103
  /* O_APPEND flags */
104
  if (*fildesfflags_ptr(pid,fd)&O_APPEND) {
105
    sz=0;
106
    goto SKIP;
107
  }
108
 
109
  /* WARNING buf is a pointer into user-space! */
110
  sz=file_ptr(pid,fd)->f_op->read(file_ptr(pid,fd),buf,nbytes);
111
 
112
  /* release the lock */
113
SKIP:
114
  __rwlock_rdunlock(&file_ptr(pid,fd)->f_dentry->d_inode->i_lock);
115
 
116
  return_from_fs(sz);
117
}