summaryrefslogtreecommitdiffstats
path: root/gl/fcntl.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/fcntl.c')
-rw-r--r--gl/fcntl.c47
1 files changed, 27 insertions, 20 deletions
diff --git a/gl/fcntl.c b/gl/fcntl.c
index 7cd3a0f9..ecb18e50 100644
--- a/gl/fcntl.c
+++ b/gl/fcntl.c
@@ -1,6 +1,6 @@
1/* Provide file descriptor control. 1/* Provide file descriptor control.
2 2
3 Copyright (C) 2009-2024 Free Software Foundation, Inc. 3 Copyright (C) 2009-2025 Free Software Foundation, Inc.
4 4
5 This file is free software: you can redistribute it and/or modify 5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as 6 it under the terms of the GNU Lesser General Public License as
@@ -29,8 +29,7 @@
29#include <unistd.h> 29#include <unistd.h>
30 30
31#ifdef __KLIBC__ 31#ifdef __KLIBC__
32# define INCL_DOS 32# include <emx/io.h>
33# include <os2.h>
34#endif 33#endif
35 34
36#if defined _WIN32 && ! defined __CYGWIN__ 35#if defined _WIN32 && ! defined __CYGWIN__
@@ -562,7 +561,8 @@ klibc_fcntl (int fd, int action, /* arg */...)
562 if (result == -1 && (errno == EPERM || errno == ENOTSUP) 561 if (result == -1 && (errno == EPERM || errno == ENOTSUP)
563 && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode)) 562 && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
564 { 563 {
565 ULONG ulMode; 564 PLIBCFH pFH;
565 unsigned fFlags;
566 566
567 switch (action) 567 switch (action)
568 { 568 {
@@ -574,34 +574,41 @@ klibc_fcntl (int fd, int action, /* arg */...)
574 result = dup2 (fd, arg); 574 result = dup2 (fd, arg);
575 break; 575 break;
576 576
577 /* Using underlying APIs is right ? */
578 case F_GETFD: 577 case F_GETFD:
579 if (DosQueryFHState (fd, &ulMode)) 578 pFH = __libc_FH (fd);
580 break; 579 if (!pFH)
580 {
581 errno = EBADF;
582 break;
583 }
581 584
582 result = (ulMode & OPEN_FLAGS_NOINHERIT) ? FD_CLOEXEC : 0; 585 result = (pFH->fFlags & ((FD_CLOEXEC << __LIBC_FH_FDFLAGS_SHIFT )
586 | O_NOINHERIT)) ? FD_CLOEXEC : 0;
583 break; 587 break;
584 588
585 case F_SETFD: 589 case F_SETFD:
586 if (arg & ~FD_CLOEXEC) 590 if (arg & ~FD_CLOEXEC)
587 break; 591 break;
588 592
589 if (DosQueryFHState (fd, &ulMode)) 593 pFH = __libc_FH (fd);
590 break; 594 if (!pFH)
595 {
596 errno = EBADF;
597 break;
598 }
591 599
600 fFlags = pFH->fFlags;
592 if (arg & FD_CLOEXEC) 601 if (arg & FD_CLOEXEC)
593 ulMode |= OPEN_FLAGS_NOINHERIT; 602 fFlags |= (FD_CLOEXEC << __LIBC_FH_FDFLAGS_SHIFT) | O_NOINHERIT;
594 else 603 else
595 ulMode &= ~OPEN_FLAGS_NOINHERIT; 604 fFlags &= ~((FD_CLOEXEC << __LIBC_FH_FDFLAGS_SHIFT) | O_NOINHERIT);
596
597 /* Filter supported flags. */
598 ulMode &= (OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR
599 | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT);
600 605
601 if (DosSetFHState (fd, ulMode)) 606 result = __libc_FHSetFlags (pFH, fd, fFlags);
602 break; 607 if (result < 0)
603 608 {
604 result = 0; 609 errno = -result;
610 result = -1;
611 }
605 break; 612 break;
606 613
607 case F_GETFL: 614 case F_GETFL: