root/fs/read_write.c

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

DEFINITIONS

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

   1 /*
   2  *  linux/fs/read_write.c
   3  *
   4  *  (C) 1991  Linus Torvalds
   5  */
   6 
   7 #include <sys/stat.h>
   8 #include <errno.h>
   9 #include <sys/types.h>
  10 
  11 #include <linux/kernel.h>
  12 #include <linux/sched.h>
  13 #include <asm/segment.h>
  14 
  15 int sys_lseek(unsigned int fd,off_t offset, unsigned int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
  16 {
  17         struct file * file;
  18         int tmp;
  19 
  20         if (fd >= NR_OPEN || !(file=current->filp[fd]) || !(file->f_inode))
  21                 return -EBADF;
  22         if (origin > 2)
  23                 return -EINVAL;
  24         if (file->f_inode->i_pipe)
  25                 return -ESPIPE;
  26         if (file->f_op && file->f_op->lseek)
  27                 return file->f_op->lseek(file->f_inode,file,offset,origin);
  28 /* this is the default handler if no lseek handler is present */
  29         switch (origin) {
  30                 case 0:
  31                         if (offset<0) return -EINVAL;
  32                         file->f_pos=offset;
  33                         break;
  34                 case 1:
  35                         if (file->f_pos+offset<0) return -EINVAL;
  36                         file->f_pos += offset;
  37                         break;
  38                 case 2:
  39                         if ((tmp=file->f_inode->i_size+offset) < 0)
  40                                 return -EINVAL;
  41                         file->f_pos = tmp;
  42         }
  43         return file->f_pos;
  44 }
  45 
  46 int sys_read(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48         struct file * file;
  49         struct inode * inode;
  50 
  51         if (fd>=NR_OPEN || !(file=current->filp[fd]) || !(inode=file->f_inode))
  52                 return -EBADF;
  53         if (!(file->f_mode & 1))
  54                 return -EBADF;
  55         if (!count)
  56                 return 0;
  57         verify_area(buf,count);
  58         if (file->f_op && file->f_op->read)
  59                 return file->f_op->read(inode,file,buf,count);
  60 /* these are the default read-functions */
  61         if (inode->i_pipe)
  62                 return pipe_read(inode,file,buf,count);
  63         if (S_ISCHR(inode->i_mode))
  64                 return char_read(inode,file,buf,count);
  65         if (S_ISBLK(inode->i_mode))
  66                 return block_read(inode,file,buf,count);
  67         if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode))
  68                 return minix_file_read(inode,file,buf,count);
  69         printk("(Read)inode->i_mode=%06o\n\r",inode->i_mode);
  70         return -EINVAL;
  71 }
  72 
  73 int sys_write(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  74 {
  75         struct file * file;
  76         struct inode * inode;
  77         
  78         if (fd>=NR_OPEN || !(file=current->filp[fd]) || !(inode=file->f_inode))
  79                 return -EBADF;
  80         if (!(file->f_mode&2))
  81                 return -EBADF;
  82         if (!count)
  83                 return 0;
  84         if (file->f_op && file->f_op->write)
  85                 return file->f_op->write(inode,file,buf,count);
  86 /* these are the default read-functions */
  87         if (inode->i_pipe)
  88                 return pipe_write(inode,file,buf,count);
  89         if (S_ISCHR(inode->i_mode))
  90                 return char_write(inode,file,buf,count);
  91         if (S_ISBLK(inode->i_mode))
  92                 return block_write(inode,file,buf,count);
  93         if (S_ISREG(inode->i_mode))
  94                 return minix_file_write(inode,file,buf,count);
  95         printk("(Write)inode->i_mode=%06o\n\r",inode->i_mode);
  96         return -EINVAL;
  97 }

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