|
|
1.1.1.2 root 1: /*
2: * linux/fs/read_write.c
3: *
4: * (C) 1991 Linus Torvalds
5: */
6:
1.1 root 7: #include <errno.h>
8: #include <sys/types.h>
1.1.1.5 ! root 9: #include <sys/stat.h>
! 10: #include <sys/dirent.h>
1.1 root 11:
12: #include <linux/kernel.h>
13: #include <linux/sched.h>
1.1.1.5 ! root 14: #include <linux/minix_fs.h>
1.1 root 15: #include <asm/segment.h>
16:
1.1.1.5 ! root 17: /*
! 18: * Count is not yet used: but we'll probably support reading several entries
! 19: * at once in the future. Use count=1 in the library for future expansions.
! 20: */
! 21: int sys_readdir(unsigned int fd, struct dirent * dirent, unsigned int count)
! 22: {
! 23: struct file * file;
! 24: struct inode * inode;
! 25:
! 26: if (fd >= NR_OPEN || !(file = current->filp[fd]) ||
! 27: !(inode = file->f_inode))
! 28: return -EBADF;
! 29: if (file->f_op && file->f_op->readdir) {
! 30: verify_area(dirent, sizeof (*dirent));
! 31: return file->f_op->readdir(inode,file,dirent);
! 32: }
! 33: return -EBADF;
! 34: }
! 35:
! 36: int sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
1.1 root 37: {
38: struct file * file;
1.1.1.5 ! root 39: int tmp, mem_dev;
1.1 root 40:
1.1.1.4 root 41: if (fd >= NR_OPEN || !(file=current->filp[fd]) || !(file->f_inode))
1.1 root 42: return -EBADF;
1.1.1.4 root 43: if (origin > 2)
44: return -EINVAL;
1.1 root 45: if (file->f_inode->i_pipe)
46: return -ESPIPE;
1.1.1.4 root 47: if (file->f_op && file->f_op->lseek)
48: return file->f_op->lseek(file->f_inode,file,offset,origin);
1.1.1.5 ! root 49: mem_dev = S_ISCHR(file->f_inode->i_mode);
! 50:
1.1.1.4 root 51: /* this is the default handler if no lseek handler is present */
1.1 root 52: switch (origin) {
53: case 0:
1.1.1.5 ! root 54: if (offset<0 && !mem_dev) return -EINVAL;
1.1 root 55: file->f_pos=offset;
56: break;
57: case 1:
1.1.1.5 ! root 58: if (file->f_pos+offset<0 && !mem_dev) return -EINVAL;
1.1 root 59: file->f_pos += offset;
60: break;
61: case 2:
1.1.1.5 ! root 62: if ((tmp=file->f_inode->i_size+offset)<0 && !mem_dev)
1.1 root 63: return -EINVAL;
64: file->f_pos = tmp;
65: }
1.1.1.5 ! root 66: if (mem_dev && file->f_pos < 0)
! 67: return 0;
1.1 root 68: return file->f_pos;
69: }
70:
1.1.1.4 root 71: int sys_read(unsigned int fd,char * buf,unsigned int count)
1.1 root 72: {
73: struct file * file;
1.1.1.4 root 74: struct inode * inode;
1.1 root 75:
1.1.1.4 root 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;
1.1 root 80: if (!count)
81: return 0;
82: verify_area(buf,count);
1.1.1.4 root 83: if (file->f_op && file->f_op->read)
84: return file->f_op->read(inode,file,buf,count);
85: /* these are the default read-functions */
1.1 root 86: if (inode->i_pipe)
1.1.1.4 root 87: return pipe_read(inode,file,buf,count);
1.1 root 88: if (S_ISCHR(inode->i_mode))
1.1.1.4 root 89: return char_read(inode,file,buf,count);
1.1 root 90: if (S_ISBLK(inode->i_mode))
1.1.1.4 root 91: return block_read(inode,file,buf,count);
92: if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode))
93: return minix_file_read(inode,file,buf,count);
1.1 root 94: printk("(Read)inode->i_mode=%06o\n\r",inode->i_mode);
95: return -EINVAL;
96: }
97:
1.1.1.4 root 98: int sys_write(unsigned int fd,char * buf,unsigned int count)
1.1 root 99: {
100: struct file * file;
1.1.1.4 root 101: struct inode * inode;
1.1 root 102:
1.1.1.4 root 103: if (fd>=NR_OPEN || !(file=current->filp[fd]) || !(inode=file->f_inode))
104: return -EBADF;
105: if (!(file->f_mode&2))
106: return -EBADF;
1.1 root 107: if (!count)
108: return 0;
1.1.1.4 root 109: if (file->f_op && file->f_op->write)
110: return file->f_op->write(inode,file,buf,count);
111: /* these are the default read-functions */
1.1 root 112: if (inode->i_pipe)
1.1.1.4 root 113: return pipe_write(inode,file,buf,count);
1.1 root 114: if (S_ISCHR(inode->i_mode))
1.1.1.4 root 115: return char_write(inode,file,buf,count);
1.1 root 116: if (S_ISBLK(inode->i_mode))
1.1.1.4 root 117: return block_write(inode,file,buf,count);
1.1 root 118: if (S_ISREG(inode->i_mode))
1.1.1.4 root 119: return minix_file_write(inode,file,buf,count);
1.1 root 120: printk("(Write)inode->i_mode=%06o\n\r",inode->i_mode);
121: return -EINVAL;
122: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.