1/**************************************************************************** 2 * fs/vfs/fs_dup2.c 3 * 4 * Copyright (c) 2023 Huawei Device Co., Ltd. All rights reserved. 5 * Based on NuttX originally from nuttx source (nuttx/fs/ and nuttx/drivers/) 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 ****************************************************************************/ 20 21/**************************************************************************** 22 * Included Files 23 ****************************************************************************/ 24 25#include "vfs_config.h" 26 27#include "errno.h" 28#include "unistd.h" 29#include "sched.h" 30#include "vnode.h" 31 32/* This logic in this applies only when both socket and file descriptors are 33 * in that case, this function descriminates which type of dup2 is being 34 * performed. 35 */ 36 37#if CONFIG_NFILE_DESCRIPTORS > 0 && defined(LOSCFG_NET_LWIP_SACK) 38 39/**************************************************************************** 40 * Public Functions 41 ****************************************************************************/ 42 43/**************************************************************************** 44 * Name: dup2 45 * 46 * Description: 47 * Clone a file descriptor or socket descriptor to a specific descriptor 48 * number 49 * 50 ****************************************************************************/ 51 52int dup2(int fd1, int fd2) 53{ 54 /* Check the range of the descriptor to see if we got a file or a socket 55 * descriptor. 56 */ 57 58 if ((unsigned int)fd1 >= CONFIG_NFILE_DESCRIPTORS) 59 { 60 /* Not a valid file descriptor. Did we get a valid socket descriptor? */ 61 62 if ((unsigned int)fd1 < (unsigned int)(CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) 63 { 64 /* Yes.. dup the socket descriptor. The errno value is not set. */ 65 66 set_errno(EBADF); /* When net_dupsd2 is still closed,errno should set */ 67 return VFS_ERROR; /* LWIP not support */ 68 } 69 else 70 { 71 /* No.. then it is a bad descriptor number */ 72 73 set_errno(EBADF); 74 return VFS_ERROR; 75 } 76 } 77 else 78 { 79 /* Its a valid file descriptor.. dup the file descriptor. fd_dupfd() 80 * sets the errno value in the event of any failures. 81 */ 82 83 return fs_dupfd2(fd1, fd2); 84 } 85} 86 87#endif /* CONFIG_NFILE_DESCRIPTORS > 0 ... */ 88