Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse posix declarations in syncio #24588

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/posix/posix.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
when defined(nimHasStyleChecks):
{.push styleChecks: off.}

when defined(nimPreviewSlimSystem):
import std/syncio
from system/ansi_c import CFilePtr

# TODO these constants don't seem to be fetched from a header file for unknown
# platforms - where do they come from and why are they here?
Expand Down Expand Up @@ -576,17 +575,17 @@ proc nice*(a1: cint): cint {.importc, header: "<unistd.h>".}
proc pathconf*(a1: cstring, a2: cint): int {.importc, header: "<unistd.h>".}

proc pause*(): cint {.importc, header: "<unistd.h>".}
proc pclose*(a: File): cint {.importc, header: "<stdio.h>".}
proc pclose*(a: CFilePtr): cint {.importc, header: "<stdio.h>".}
proc pipe*(a: array[0..1, cint]): cint {.importc, header: "<unistd.h>".}
proc popen*(a1, a2: cstring): File {.importc, header: "<stdio.h>".}
proc popen*(a1, a2: cstring): CFilePtr {.importc, header: "<stdio.h>".}
proc pread*(a1: cint, a2: pointer, a3: int, a4: Off): int {.
importc, header: "<unistd.h>".}
proc pwrite*(a1: cint, a2: pointer, a3: int, a4: Off): int {.
importc, header: "<unistd.h>".}
proc read*(a1: cint, a2: pointer, a3: int): int {.importc, header: "<unistd.h>".}
when not defined(nintendoswitch):
proc readlink*(a1, a2: cstring, a3: int): int {.importc, header: "<unistd.h>".}
proc ioctl*(f: FileHandle, device: uint): int {.importc: "ioctl",
proc ioctl*(f: cint, device: uint): int {.importc: "ioctl",
header: "<sys/ioctl.h>", varargs, tags: [WriteIOEffect].}
## A system call for device-specific input/output operations and other
## operations which cannot be expressed by regular system calls
Expand Down
51 changes: 10 additions & 41 deletions lib/std/syncio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ when defined(windows):
from system/ansi_c import c_memchr

# ----------------- IO Part ------------------------------------------------
from system/ansi_c import CFilePtr

type
CFile {.importc: "FILE", header: "<stdio.h>",
incompleteStruct.} = object
File* = ptr CFile ## The type representing a file handle.
File* = CFilePtr ## The type representing a file handle.

FileMode* = enum ## The file mode when opening a file.
fmRead, ## Open the file for read access only.
Expand Down Expand Up @@ -293,13 +293,7 @@ when SupportIoctlInheritCtl:
proc c_ioctl(fd: cint, request: cint): cint {.
importc: "ioctl", header: "<sys/ioctl.h>", varargs.}
elif defined(posix) and not defined(lwip) and not defined(nimscript):
var
F_GETFD {.importc, header: "<fcntl.h>".}: cint
F_SETFD {.importc, header: "<fcntl.h>".}: cint
FD_CLOEXEC {.importc, header: "<fcntl.h>".}: cint

proc c_fcntl(fd: cint, cmd: cint): cint {.
importc: "fcntl", header: "<fcntl.h>", varargs.}
from posix import F_GETFD, F_SETFD, FD_CLOEXEC, fcntl
elif defined(windows):
type
WinDWORD = culong
Expand Down Expand Up @@ -384,11 +378,11 @@ when defined(nimdoc) or (defined(posix) and not defined(nimscript)) or defined(w
elif defined(freertos) or defined(zephyr):
result = true
elif defined(posix):
var flags = c_fcntl(f, F_GETFD)
var flags = fcntl(f, F_GETFD)
if flags == -1:
return false
flags = if inheritable: flags and not FD_CLOEXEC else: flags or FD_CLOEXEC
result = c_fcntl(f, F_SETFD, flags) != -1
result = fcntl(f, F_SETFD, flags) != -1
else:
result = setHandleInformation(cast[IoHandle](f), HANDLE_FLAG_INHERIT,
inheritable.WinDWORD) != 0
Expand Down Expand Up @@ -676,35 +670,10 @@ const
# should not be translated.

when defined(posix) and not defined(nimscript):
when defined(linux) and defined(amd64):
type
Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint

# fillers ensure correct size & offsets
Stat {.importc: "struct stat",
header: "<sys/stat.h>", final, pure.} = object ## struct stat
filler_1: array[24, char]
st_mode: Mode ## Mode of file
filler_2: array[144 - 24 - 4, char]

proc modeIsDir(m: Mode): bool =
## Test for a directory.
(m and 0o170000) == 0o40000

else:
type
Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint

Stat {.importc: "struct stat",
header: "<sys/stat.h>", final, pure.} = object ## struct stat
st_mode: Mode ## Mode of file

proc modeIsDir(m: Mode): bool {.importc: "S_ISDIR", header: "<sys/stat.h>".}
## Test for a directory.

proc c_fstat(a1: cint, a2: var Stat): cint {.
importc: "fstat", header: "<sys/stat.h>".}
from posix import Mode, Stat, S_ISDIR, fstat

proc modeIsDir(m: Mode): bool =
S_ISDIR(m)

proc open*(f: var File, filename: string,
mode: FileMode = fmRead,
Expand All @@ -723,7 +692,7 @@ proc open*(f: var File, filename: string,
# POSIX. We do not want to handle directories as regular files that can
# be opened.
var res {.noinit.}: Stat
if c_fstat(getFileHandle(f2), res) >= 0'i32 and modeIsDir(res.st_mode):
if fstat(getFileHandle(f2), res) >= 0'i32 and modeIsDir(res.st_mode):
closeIgnoreError(f2)
return false
when not defined(nimInheritHandles) and declared(setInheritable) and
Expand Down
Loading