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