filedescriptor.c 1.3 KB

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