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