xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/filedescriptor.c (revision 5173eb0a33e5d83890ba976253e703be4c92557c)
18dffb485Schristos /* File descriptor related functions.
28dffb485Schristos 
3*5173eb0aSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
48dffb485Schristos 
58dffb485Schristos    This file is part of the libiberty library.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 2 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program; if not, write to the Free Software
198dffb485Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor,
208dffb485Schristos    Boston, MA 02110-1301, USA.  */
218dffb485Schristos 
228dffb485Schristos #include "config.h"
238dffb485Schristos #include "ansidecl.h"
248dffb485Schristos #include "libiberty.h"
258dffb485Schristos 
268dffb485Schristos #ifdef HAVE_FCNTL_H
278dffb485Schristos #include <fcntl.h>
288dffb485Schristos #endif
298dffb485Schristos 
308dffb485Schristos #if defined (_WIN32)
318dffb485Schristos #define WIN32_LEAN_AND_MEAN
328dffb485Schristos #include <windows.h> /* for GetFullPathName */
338dffb485Schristos #endif
348dffb485Schristos /* Return true when FD file descriptor exists.  */
358dffb485Schristos 
368dffb485Schristos int
378dffb485Schristos is_valid_fd (int fd)
388dffb485Schristos {
398dffb485Schristos #if defined(_WIN32)
408dffb485Schristos   HANDLE h = (HANDLE) _get_osfhandle (fd);
418dffb485Schristos   return h != (HANDLE) -1;
428dffb485Schristos #elif defined(F_GETFD)
438dffb485Schristos   return fcntl (fd, F_GETFD) >= 0;
448dffb485Schristos #else
458dffb485Schristos   return dup2 (fd, fd) < 0;
468dffb485Schristos #endif
478dffb485Schristos }
48