root/fs/read_write.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_readdir
  2. sys_lseek
  3. sys_read
  4. sys_write

   1 /*
   2  *  linux/fs/read_write.c
   3  *
   4  *  (C) 1991  Linus Torvalds
   5  */
   6 
   7 #include <errno.h>
   8 #include <sys/types.h>
   9 #include <sys/stat.h>
  10 #include <sys/dirent.h>
  11 
  12 #include <linux/kernel.h>
  13 #include <linux/sched.h>
  14 #include <linux/minix_fs.h>
  15 #include <asm/segment.h>
  16 
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38         struct file * file;
  39         int tmp, mem_dev;
  40 
  41         if (fd >= NR_OPEN || !(file=current->filp[fd]) || !(file->f_inode))
  42                 return -EBADF;
  43         if (origin > 2)
  44                 return -EINVAL;
  45         if (file->f_inode->i_pipe)
  46                 return -ESPIPE;
  47         if (file->f_op && file->f_op->lseek)
  48                 return file->f_op->lseek(file->f_inode,file,offset,origin);
  49         mem_dev = S_ISCHR(file->f_inode->i_mode);
  50 
  51 /* this is the default handler if no lseek handler is present */
  52         switch (origin) {
  53                 case 0:
  54                         if (offset<0 && !mem_dev) return -EINVAL;
  55                         file->f_pos=offset;
  56                         break;
  57                 case 1:
  58                         if (file->f_pos+offset<0 && !mem_dev) return -EINVAL;
  59                         file->f_pos += offset;
  60                         break;
  61                 case 2:
  62                         if ((tmp=file->f_inode->i_size+offset)<0 && !mem_dev)
  63                                 return -EINVAL;
  64                         file->f_pos = tmp;
  65         }
  66         if (mem_dev && file->f_pos < 0)
  67                 return 0;
  68         return file->f_pos;
  69 }
  70 
  71 int sys_read(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  72 {
  73         struct file * file;
  74         struct inode * inode;
  75 
  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 (!count)
  81                 return 0;
  82         verify_area(buf,count);
  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 */
  86         if (inode->i_pipe)
  87                 return pipe_read(inode,file,buf,count);
  88         if (S_ISCHR(inode->i_mode))
  89                 return char_read(inode,file,buf,count);
  90         if (S_ISBLK(inode->i_mode))
  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);
  94         printk("(Read)inode->i_mode=%06o\n\r",inode->i_mode);
  95         return -EINVAL;
  96 }
  97 
  98 int sys_write(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100         struct file * file;
 101         struct inode * inode;
 102         
 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;
 107         if (!count)
 108                 return 0;
 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 */
 112         if (inode->i_pipe)
 113                 return pipe_write(inode,file,buf,count);
 114         if (S_ISCHR(inode->i_mode))
 115                 return char_write(inode,file,buf,count);
 116         if (S_ISBLK(inode->i_mode))
 117                 return block_write(inode,file,buf,count);
 118         if (S_ISREG(inode->i_mode))
 119                 return minix_file_write(inode,file,buf,count);
 120         printk("(Write)inode->i_mode=%06o\n\r",inode->i_mode);
 121         return -EINVAL;
 122 }

/* [previous][next][first][last][top][bottom][index][help] */