xref: /netbsd-src/external/gpl3/binutils.old/dist/libiberty/filedescriptor.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1867d70fcSchristos /* File descriptor related functions.
2867d70fcSchristos 
3*c42dbd0eSchristos    Copyright (C) 2019-2022 Free Software Foundation, Inc.
4867d70fcSchristos 
5867d70fcSchristos    This file is part of the libiberty library.
6867d70fcSchristos 
7867d70fcSchristos    This program is free software; you can redistribute it and/or modify
8867d70fcSchristos    it under the terms of the GNU General Public License as published by
9867d70fcSchristos    the Free Software Foundation; either version 2 of the License, or
10867d70fcSchristos    (at your option) any later version.
11867d70fcSchristos 
12867d70fcSchristos    This program is distributed in the hope that it will be useful,
13867d70fcSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14867d70fcSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15867d70fcSchristos    GNU General Public License for more details.
16867d70fcSchristos 
17867d70fcSchristos    You should have received a copy of the GNU General Public License
18867d70fcSchristos    along with this program; if not, write to the Free Software
19867d70fcSchristos    Foundation, Inc., 51 Franklin Street - Fifth Floor,
20867d70fcSchristos    Boston, MA 02110-1301, USA.  */
21867d70fcSchristos 
22867d70fcSchristos #include "config.h"
23867d70fcSchristos #include "ansidecl.h"
24867d70fcSchristos #include "libiberty.h"
25867d70fcSchristos 
26867d70fcSchristos #ifdef HAVE_FCNTL_H
27867d70fcSchristos #include <fcntl.h>
28867d70fcSchristos #endif
29867d70fcSchristos 
30867d70fcSchristos #if defined (_WIN32)
31867d70fcSchristos #define WIN32_LEAN_AND_MEAN
32867d70fcSchristos #include <windows.h> /* for GetFullPathName */
33867d70fcSchristos #endif
34867d70fcSchristos /* Return true when FD file descriptor exists.  */
35867d70fcSchristos 
36867d70fcSchristos int
is_valid_fd(int fd)37867d70fcSchristos is_valid_fd (int fd)
38867d70fcSchristos {
39867d70fcSchristos #if defined(_WIN32)
40867d70fcSchristos   HANDLE h = (HANDLE) _get_osfhandle (fd);
41867d70fcSchristos   return h != (HANDLE) -1;
42867d70fcSchristos #elif defined(F_GETFD)
43867d70fcSchristos   return fcntl (fd, F_GETFD) >= 0;
44867d70fcSchristos #else
45867d70fcSchristos   return dup2 (fd, fd) < 0;
46867d70fcSchristos #endif
47867d70fcSchristos }
48