1 /****************************************************************************
2 * fs/vfs/fs_dup2.c
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership. The
7 * ASF licenses this file to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance with the
9 * License. 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, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations
17 * 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
dup2(int fd1,int fd2)52 int 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