xref: /netbsd-src/external/gpl3/binutils/dist/libiberty/filedescriptor.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
16f4ced0bSchristos /* File descriptor related functions.
26f4ced0bSchristos 
3*cb63e24eSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
46f4ced0bSchristos 
56f4ced0bSchristos    This file is part of the libiberty library.
66f4ced0bSchristos 
76f4ced0bSchristos    This program is free software; you can redistribute it and/or modify
86f4ced0bSchristos    it under the terms of the GNU General Public License as published by
96f4ced0bSchristos    the Free Software Foundation; either version 2 of the License, or
106f4ced0bSchristos    (at your option) any later version.
116f4ced0bSchristos 
126f4ced0bSchristos    This program is distributed in the hope that it will be useful,
136f4ced0bSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
146f4ced0bSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
156f4ced0bSchristos    GNU General Public License for more details.
166f4ced0bSchristos 
176f4ced0bSchristos    You should have received a copy of the GNU General Public License
186f4ced0bSchristos    along with this program; if not, write to the Free Software
196f4ced0bSchristos    Foundation, Inc., 51 Franklin Street - Fifth Floor,
206f4ced0bSchristos    Boston, MA 02110-1301, USA.  */
216f4ced0bSchristos 
226f4ced0bSchristos #include "config.h"
236f4ced0bSchristos #include "ansidecl.h"
246f4ced0bSchristos #include "libiberty.h"
256f4ced0bSchristos 
266f4ced0bSchristos #ifdef HAVE_FCNTL_H
276f4ced0bSchristos #include <fcntl.h>
286f4ced0bSchristos #endif
296f4ced0bSchristos 
306f4ced0bSchristos #if defined (_WIN32)
316f4ced0bSchristos #define WIN32_LEAN_AND_MEAN
326f4ced0bSchristos #include <windows.h> /* for GetFullPathName */
336f4ced0bSchristos #endif
346f4ced0bSchristos /* Return true when FD file descriptor exists.  */
356f4ced0bSchristos 
366f4ced0bSchristos int
is_valid_fd(int fd)376f4ced0bSchristos is_valid_fd (int fd)
386f4ced0bSchristos {
396f4ced0bSchristos #if defined(_WIN32)
406f4ced0bSchristos   HANDLE h = (HANDLE) _get_osfhandle (fd);
416f4ced0bSchristos   return h != (HANDLE) -1;
426f4ced0bSchristos #elif defined(F_GETFD)
436f4ced0bSchristos   return fcntl (fd, F_GETFD) >= 0;
446f4ced0bSchristos #else
456f4ced0bSchristos   return dup2 (fd, fd) < 0;
466f4ced0bSchristos #endif
476f4ced0bSchristos }
48