root/fs/fcntl.c

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

DEFINITIONS

This source file includes following definitions.
  1. dupfd
  2. sys_dup2
  3. sys_dup
  4. sys_fcntl

   1 /*
   2  *  linux/fs/fcntl.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/kernel.h>
  11 #include <linux/errno.h>
  12 #include <linux/stat.h>
  13 #include <linux/fcntl.h>
  14 #include <linux/string.h>
  15 
  16 extern int sys_close(int fd);
  17 
  18 static int dupfd(unsigned int fd, unsigned int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
  19 {
  20         if (fd >= NR_OPEN || !current->filp[fd])
  21                 return -EBADF;
  22         if (arg >= NR_OPEN)
  23                 return -EINVAL;
  24         while (arg < NR_OPEN)
  25                 if (current->filp[arg])
  26                         arg++;
  27                 else
  28                         break;
  29         if (arg >= NR_OPEN)
  30                 return -EMFILE;
  31         current->close_on_exec &= ~(1<<arg);
  32         (current->filp[arg] = current->filp[fd])->f_count++;
  33         return arg;
  34 }
  35 
  36 int sys_dup2(unsigned int oldfd, unsigned int newfd)
     /* [previous][next][first][last][top][bottom][index][help] */
  37 {
  38         if (oldfd >= NR_OPEN || !current->filp[oldfd])
  39                 return -EBADF;
  40         if (newfd == oldfd)
  41                 return newfd;
  42         sys_close(newfd);
  43         return dupfd(oldfd,newfd);
  44 }
  45 
  46 int sys_dup(unsigned int fildes)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48         return dupfd(fildes,0);
  49 }
  50 
  51 int sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
  52 {       
  53         struct file * filp;
  54 
  55         if (fd >= NR_OPEN || !(filp = current->filp[fd]))
  56                 return -EBADF;
  57         switch (cmd) {
  58                 case F_DUPFD:
  59                         return dupfd(fd,arg);
  60                 case F_GETFD:
  61                         return (current->close_on_exec>>fd)&1;
  62                 case F_SETFD:
  63                         if (arg&1)
  64                                 current->close_on_exec |= (1<<fd);
  65                         else
  66                                 current->close_on_exec &= ~(1<<fd);
  67                         return 0;
  68                 case F_GETFL:
  69                         return filp->f_flags;
  70                 case F_SETFL:
  71                         filp->f_flags &= ~(O_APPEND | O_NONBLOCK);
  72                         filp->f_flags |= arg & (O_APPEND | O_NONBLOCK);
  73                         return 0;
  74                 case F_GETLK:   case F_SETLK:   case F_SETLKW:
  75                         return -ENOSYS;
  76                 default:
  77                         return -EINVAL;
  78         }
  79 }

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