home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / fs / ioctl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-01  |  2.2 KB  |  100 lines

  1. /*
  2.  *  linux/fs/ioctl.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. #include <asm/segment.h>
  8.  
  9. #include <linux/sched.h>
  10. #include <linux/errno.h>
  11. #include <linux/string.h>
  12. #include <linux/stat.h>
  13. #include <linux/termios.h>
  14. #include <linux/fcntl.h> /* for f_flags values */
  15.  
  16. static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
  17. {
  18.     int error;
  19.     int block;
  20.  
  21.     switch (cmd) {
  22.         case FIBMAP:
  23.             if (filp->f_inode->i_op == NULL)
  24.                 return -EBADF;
  25.                 if (filp->f_inode->i_op->bmap == NULL)
  26.                 return -EINVAL;
  27.             error = verify_area(VERIFY_WRITE,(void *) arg,4);
  28.             if (error)
  29.                 return error;
  30.             block = get_fs_long((long *) arg);
  31.             block = filp->f_inode->i_op->bmap(filp->f_inode,block);
  32.             put_fs_long(block,(long *) arg);
  33.             return 0;
  34.         case FIGETBSZ:
  35.             if (filp->f_inode->i_sb == NULL)
  36.                 return -EBADF;
  37.             error = verify_area(VERIFY_WRITE,(void *) arg,4);
  38.             if (error)
  39.                 return error;
  40.             put_fs_long(filp->f_inode->i_sb->s_blocksize,
  41.                 (long *) arg);
  42.             return 0;
  43.         case FIONREAD:
  44.             error = verify_area(VERIFY_WRITE,(void *) arg,4);
  45.             if (error)
  46.                 return error;
  47.             put_fs_long(filp->f_inode->i_size - filp->f_pos,
  48.                 (long *) arg);
  49.             return 0;
  50.     }
  51.     if (filp->f_op && filp->f_op->ioctl)
  52.         return filp->f_op->ioctl(filp->f_inode, filp, cmd,arg);
  53.     return -EINVAL;
  54. }
  55.  
  56.  
  57. asmlinkage int sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
  58. {    
  59.     struct file * filp;
  60.     int on;
  61.  
  62.     if (fd >= NR_OPEN || !(filp = current->filp[fd]))
  63.         return -EBADF;
  64.     switch (cmd) {
  65.         case FIOCLEX:
  66.             FD_SET(fd, ¤t->close_on_exec);
  67.             return 0;
  68.  
  69.         case FIONCLEX:
  70.             FD_CLR(fd, ¤t->close_on_exec);
  71.             return 0;
  72.  
  73.         case FIONBIO:
  74.             on = get_fs_long((unsigned long *) arg);
  75.             if (on)
  76.                 filp->f_flags |= O_NONBLOCK;
  77.             else
  78.                 filp->f_flags &= ~O_NONBLOCK;
  79.             return 0;
  80.  
  81.         case FIOASYNC: /* O_SYNC is not yet implemented,
  82.                   but it's here for completeness. */
  83.             on = get_fs_long ((unsigned long *) arg);
  84.             if (on)
  85.                 filp->f_flags |= O_SYNC;
  86.             else
  87.                 filp->f_flags &= ~O_SYNC;
  88.             return 0;
  89.  
  90.         default:
  91.             if (filp->f_inode && S_ISREG(filp->f_inode->i_mode))
  92.                 return file_ioctl(filp,cmd,arg);
  93.  
  94.             if (filp->f_op && filp->f_op->ioctl)
  95.                 return filp->f_op->ioctl(filp->f_inode, filp, cmd,arg);
  96.  
  97.             return -EINVAL;
  98.     }
  99. }
  100.