This is a maintenance fork
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
2.6 KiB

1 year ago
1 year ago
1 year ago
  1. /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
  2. vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
  3. Copyright 2020 Justine Alexandra Roberts Tunney
  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; version 2 of the License.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  14. 02110-1301 USA
  15. */
  16. #include "libc/calls/internal.h"
  17. #include "libc/nt/files.h"
  18. #include "libc/nt/runtime.h"
  19. #include "libc/sysv/errfuns.h"
  20. textwindows int close$nt(int fd) {
  21. bool32 ok;
  22. if (__isfdopen(fd)) {
  23. if (g_fds.p[fd].kind == kFdFile) {
  24. /*
  25. * Like Linux, closing a file on Windows doesn't guarantee it's
  26. * immediately synced to disk. But unlike Linux, this could cause
  27. * subsequent operations, e.g. unlink() to break w/ access error.
  28. */
  29. FlushFileBuffers(g_fds.p[fd].handle);
  30. }
  31. ok = CloseHandle(g_fds.p[fd].handle);
  32. if (g_fds.p[fd].kind == kFdConsole) {
  33. ok &= CloseHandle(g_fds.p[fd].extra);
  34. }
  35. __removefd(fd);
  36. return ok ? 0 : __winerr();
  37. } else {
  38. return ebadf();
  39. }
  40. }