int file_dup(FAR struct file *filep, int minfd) { int fd2; /* Verify that fd is a valid, open file descriptor */ if (!DUP_ISOPEN(filep)) { return -EBADF; } /* Increment the reference count on the contained inode */ inode_addref(filep->f_inode); /* Then allocate a new file descriptor for the inode */ fd2 = files_allocate(filep->f_inode, filep->f_oflags, filep->f_pos, minfd); if (fd2 < 0) { inode_release(filep->f_inode); return -EMFILE; } return fd2; }
int dup2(int fildes1, int fildes2) #endif { FAR struct filelist *list; /* Get the thread-specific file list */ list = sched_getfiles(); if (!list) { set_errno(EMFILE); return ERROR; } /* Verify that fildes is a valid, open file descriptor */ if (!DUP_ISOPEN(fildes1, list)) { set_errno(EBADF); return ERROR; } /* Handle a special case */ if (fildes1 == fildes2) { return fildes1; } /* Verify fildes2 */ if ((unsigned int)fildes2 >= CONFIG_NFILE_DESCRIPTORS) { set_errno(EBADF); return ERROR; } return files_dup(&list->fl_files[fildes1], &list->fl_files[fildes2]); }