Fork me on GitHub

root/kernel/syscalls/dup2.c

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

DEFINITIONS

This source file includes following definitions.
  1. sys_dup2

   1 /*
   2  * fiwix/kernel/syscalls/dup2.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/syscalls.h>
   9 #include <fiwix/process.h>
  10 #include <fiwix/errno.h>
  11 
  12 #ifdef __DEBUG__
  13 #include <fiwix/stdio.h>
  14 #endif /*__DEBUG__ */
  15 
  16 int sys_dup2(int old_ufd, int new_ufd)
  17 {
  18 #ifdef __DEBUG__
  19         printk("(pid %d) sys_dup2(%d, %d)", current->pid, old_ufd, new_ufd);
  20 #endif /*__DEBUG__ */
  21 
  22         CHECK_UFD(old_ufd);
  23         if(new_ufd < 0 || new_ufd > OPEN_MAX) {
  24                 return -EINVAL;
  25         }
  26         if(old_ufd == new_ufd) {
  27                 return new_ufd;
  28         }
  29         if(current->fd[new_ufd]) {
  30                 sys_close(new_ufd);
  31         }
  32         if((new_ufd = get_new_user_fd(new_ufd)) < 0) {
  33                 return new_ufd;
  34         }
  35         current->fd[new_ufd] = current->fd[old_ufd];
  36         fd_table[current->fd[new_ufd]].count++;
  37 #ifdef __DEBUG__
  38         printk(" --> returning %d\n", new_ufd);
  39 #endif /*__DEBUG__ */
  40         return new_ufd;
  41 }

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