* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-0.99 / fs / read_write.c
blob3ff9058e9d04414c804f193adb0a571b20cc6fb4
1 /*
2 * linux/fs/read_write.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/types.h>
8 #include <linux/errno.h>
9 #include <linux/stat.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <asm/segment.h>
16 * Count is not yet used: but we'll probably support reading several entries
17 * at once in the future. Use count=1 in the library for future expansions.
19 asmlinkage int sys_readdir(unsigned int fd, struct dirent * dirent, unsigned int count)
21 int error;
22 struct file * file;
23 struct inode * inode;
25 if (fd >= NR_OPEN || !(file = current->filp[fd]) ||
26 !(inode = file->f_inode))
27 return -EBADF;
28 error = -ENOTDIR;
29 if (file->f_op && file->f_op->readdir) {
30 error = verify_area(VERIFY_WRITE, dirent, sizeof (*dirent));
31 if (!error)
32 error = file->f_op->readdir(inode,file,dirent,count);
34 return error;
37 asmlinkage int sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
39 struct file * file;
40 int tmp = -1;
42 if (fd >= NR_OPEN || !(file=current->filp[fd]) || !(file->f_inode))
43 return -EBADF;
44 if (origin > 2)
45 return -EINVAL;
46 if (file->f_op && file->f_op->lseek)
47 return file->f_op->lseek(file->f_inode,file,offset,origin);
49 /* this is the default handler if no lseek handler is present */
50 switch (origin) {
51 case 0:
52 tmp = offset;
53 break;
54 case 1:
55 tmp = file->f_pos + offset;
56 break;
57 case 2:
58 if (!file->f_inode)
59 return -EINVAL;
60 tmp = file->f_inode->i_size + offset;
61 break;
63 if (tmp < 0)
64 return -EINVAL;
65 file->f_pos = tmp;
66 file->f_reada = 0;
67 return file->f_pos;
70 asmlinkage int sys_read(unsigned int fd,char * buf,unsigned int count)
72 int error;
73 struct file * file;
74 struct inode * inode;
76 if (fd>=NR_OPEN || !(file=current->filp[fd]) || !(inode=file->f_inode))
77 return -EBADF;
78 if (!(file->f_mode & 1))
79 return -EBADF;
80 if (!file->f_op || !file->f_op->read)
81 return -EINVAL;
82 if (!count)
83 return 0;
84 error = verify_area(VERIFY_WRITE,buf,count);
85 if (error)
86 return error;
87 return file->f_op->read(inode,file,buf,count);
90 asmlinkage int sys_write(unsigned int fd,char * buf,unsigned int count)
92 int error;
93 struct file * file;
94 struct inode * inode;
96 if (fd>=NR_OPEN || !(file=current->filp[fd]) || !(inode=file->f_inode))
97 return -EBADF;
98 if (!(file->f_mode & 2))
99 return -EBADF;
100 if (!file->f_op || !file->f_op->write)
101 return -EINVAL;
102 if (!count)
103 return 0;
104 error = verify_area(VERIFY_READ,buf,count);
105 if (error)
106 return error;
107 return file->f_op->write(inode,file,buf,count);