/* * linux/fs/read_write.c * * (C) 1991 Linus Torvalds */ #include #include #include #include #include #include #include extern int rw_char(int rw,int dev, char * buf, int count, off_t * pos); extern int read_pipe(struct m_inode * inode, char * buf, int count); extern int write_pipe(struct m_inode * inode, char * buf, int count); extern int block_read(int dev, off_t * pos, char * buf, int count); extern int block_write(int dev, off_t * pos, char * buf, int count); extern int file_read(struct m_inode * inode, struct file * filp, char * buf, int count); extern int file_write(struct m_inode * inode, struct file * filp, char * buf, int count); int sys_lseek(unsigned int fd,off_t offset, int origin) { struct file * file; int tmp; if (fd >= NR_OPEN || !(file=current->filp[fd]) || !(file->f_inode) || !IS_SEEKABLE(MAJOR(file->f_inode->i_dev))) return -EBADF; if (file->f_inode->i_pipe) return -ESPIPE; switch (origin) { case 0: if (offset<0) return -EINVAL; file->f_pos=offset; break; case 1: if (file->f_pos+offset<0) return -EINVAL; file->f_pos += offset; break; case 2: if ((tmp=file->f_inode->i_size+offset) < 0) return -EINVAL; file->f_pos = tmp; break; default: return -EINVAL; } return file->f_pos; } int sys_read(unsigned int fd,char * buf,int count) { struct file * file; struct m_inode * inode; if (fd>=NR_OPEN || count<0 || !(file=current->filp[fd])) return -EINVAL; if (!count) return 0; verify_area(buf,count); inode = file->f_inode; if (inode->i_pipe) return (file->f_mode&1)?read_pipe(inode,buf,count):-EIO; if (S_ISCHR(inode->i_mode)) return rw_char(READ,inode->i_zone[0],buf,count,&file->f_pos); if (S_ISBLK(inode->i_mode)) return block_read(inode->i_zone[0],&file->f_pos,buf,count); if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode)) { if (count+file->f_pos > inode->i_size) count = inode->i_size - file->f_pos; if (count<=0) return 0; return file_read(inode,file,buf,count); } printk("(Read)inode->i_mode=%06o\n\r",inode->i_mode); return -EINVAL; } int k_read(unsigned int fd,char * buf,int count) { struct file * file; struct m_inode * inode; if (fd>=NR_OPEN || count<0 || !(file=current->filp[fd])) return -EINVAL; if (!count) return 0; verify_area(buf,count); inode = file->f_inode; if (inode->i_pipe) return (file->f_mode&1)?read_pipe(inode,buf,count):-EIO; if (S_ISCHR(inode->i_mode)) return rw_char(READ,inode->i_zone[0],buf,count,&file->f_pos); if (S_ISBLK(inode->i_mode)) return block_read(inode->i_zone[0],&file->f_pos,buf,count); if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode)) { if (count+file->f_pos > inode->i_size) count = inode->i_size - file->f_pos; if (count<=0) return 0; return k_file_read(inode,file,buf,count); } printk("(Read)inode->i_mode=%06o\n\r",inode->i_mode); return -EINVAL; } int sys_write(unsigned int fd,char * buf,int count) { struct file * file; struct m_inode * inode; if (fd>=NR_OPEN || count <0 || !(file=current->filp[fd])) return -EINVAL; if (!count) return 0; inode=file->f_inode; if (inode->i_pipe) return (file->f_mode&2)?write_pipe(inode,buf,count):-EIO; if (S_ISCHR(inode->i_mode)) return rw_char(WRITE,inode->i_zone[0],buf,count,&file->f_pos); if (S_ISBLK(inode->i_mode)) return block_write(inode->i_zone[0],&file->f_pos,buf,count); if (S_ISREG(inode->i_mode)) return file_write(inode,file,buf,count); printk("(Write)inode->i_mode=%06o\n\r",inode->i_mode); return -EINVAL; } int sys_getdents(unsigned int fd, struct linux_dirent *dirp, unsigned long count) { char name[16]; int i=0,j=0; int rst=count; while(k_read(fd,name,16)>0 && name[0] && count-32>0) { put_fs_long(name, &(dirp->d_ino)); i++; put_fs_long(i,&(dirp->d_off)); put_fs_word(32,&(dirp->d_reclen)); for(j=0;j<14;j++) { put_fs_byte(name[j+2],dirp->d_name+j); } count-=32; dirp=(struct linux_dirent*)((char *)dirp+32); } return rst-count; }