VirtualBox

source: kBuild/trunk/src/grep/lib/dup.c

Last change on this file was 3529, checked in by bird, 3 years ago

Imported grep 3.7 from grep-3.7.tar.gz (sha256: c22b0cf2d4f6bbe599c902387e8058990e1eee99aef333a203829e5fd3dbb342), applying minimal auto-props.

  • Property svn:eol-style set to native
File size: 1.9 KB
Line 
1/* Duplicate an open file descriptor.
2
3 Copyright (C) 2011-2021 Free Software Foundation, Inc.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18#include <config.h>
19
20/* Specification. */
21#include <unistd.h>
22
23#include <errno.h>
24
25#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
26# include "msvc-inval.h"
27#endif
28
29#undef dup
30
31#if defined _WIN32 && !defined __CYGWIN__
32# if HAVE_MSVC_INVALID_PARAMETER_HANDLER
33static int
34dup_nothrow (int fd)
35{
36 int result;
37
38 TRY_MSVC_INVAL
39 {
40 result = _dup (fd);
41 }
42 CATCH_MSVC_INVAL
43 {
44 result = -1;
45 errno = EBADF;
46 }
47 DONE_MSVC_INVAL;
48
49 return result;
50}
51# else
52# define dup_nothrow _dup
53# endif
54#elif defined __KLIBC__
55# include <fcntl.h>
56# include <sys/stat.h>
57
58# include <InnoTekLIBC/backend.h>
59
60static int
61dup_nothrow (int fd)
62{
63 int dupfd;
64 struct stat sbuf;
65
66 dupfd = dup (fd);
67 if (dupfd == -1 && errno == ENOTSUP \
68 && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
69 {
70 char path[_MAX_PATH];
71
72 /* Get a path from fd */
73 if (!__libc_Back_ioFHToPath (fd, path, sizeof (path)))
74 dupfd = open (path, O_RDONLY);
75 }
76
77 return dupfd;
78}
79#else
80# define dup_nothrow dup
81#endif
82
83int
84rpl_dup (int fd)
85{
86 int result = dup_nothrow (fd);
87#if REPLACE_FCHDIR
88 if (result >= 0)
89 result = _gl_register_dup (fd, result);
90#endif
91 return result;
92}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette