1*fae548d3Szrj /* File descriptor related functions.
2*fae548d3Szrj 
3*fae548d3Szrj    Copyright (C) 2019-2020 Free Software Foundation, Inc.
4*fae548d3Szrj 
5*fae548d3Szrj    This file is part of the libiberty library.
6*fae548d3Szrj 
7*fae548d3Szrj    This program is free software; you can redistribute it and/or modify
8*fae548d3Szrj    it under the terms of the GNU General Public License as published by
9*fae548d3Szrj    the Free Software Foundation; either version 2 of the License, or
10*fae548d3Szrj    (at your option) any later version.
11*fae548d3Szrj 
12*fae548d3Szrj    This program is distributed in the hope that it will be useful,
13*fae548d3Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*fae548d3Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*fae548d3Szrj    GNU General Public License for more details.
16*fae548d3Szrj 
17*fae548d3Szrj    You should have received a copy of the GNU General Public License
18*fae548d3Szrj    along with this program; if not, write to the Free Software
19*fae548d3Szrj    Foundation, Inc., 51 Franklin Street - Fifth Floor,
20*fae548d3Szrj    Boston, MA 02110-1301, USA.  */
21*fae548d3Szrj 
22*fae548d3Szrj #include "config.h"
23*fae548d3Szrj #include "ansidecl.h"
24*fae548d3Szrj #include "libiberty.h"
25*fae548d3Szrj 
26*fae548d3Szrj #ifdef HAVE_FCNTL_H
27*fae548d3Szrj #include <fcntl.h>
28*fae548d3Szrj #endif
29*fae548d3Szrj 
30*fae548d3Szrj #if defined (_WIN32)
31*fae548d3Szrj #define WIN32_LEAN_AND_MEAN
32*fae548d3Szrj #include <windows.h> /* for GetFullPathName */
33*fae548d3Szrj #endif
34*fae548d3Szrj /* Return true when FD file descriptor exists.  */
35*fae548d3Szrj 
36*fae548d3Szrj int
is_valid_fd(int fd)37*fae548d3Szrj is_valid_fd (int fd)
38*fae548d3Szrj {
39*fae548d3Szrj #if defined(_WIN32)
40*fae548d3Szrj   HANDLE h = (HANDLE) _get_osfhandle (fd);
41*fae548d3Szrj   return h != (HANDLE) -1;
42*fae548d3Szrj #elif defined(F_GETFD)
43*fae548d3Szrj   return fcntl (fd, F_GETFD) >= 0;
44*fae548d3Szrj #else
45*fae548d3Szrj   return dup2 (fd, fd) < 0;
46*fae548d3Szrj #endif
47*fae548d3Szrj }
48