From 4275b9abb6d764491ef08620cf4d850e825d73d1 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Sun, 29 Dec 2024 09:09:55 +0100 Subject: [PATCH 1/3] reuse `posix` declarations in syncio The ones in syncio are pretty far off (`mode_t` is uint16 sometimes for example - Stat is all over the place etc) --- lib/std/syncio.nim | 45 +++++++-------------------------------------- 1 file changed, 7 insertions(+), 38 deletions(-) diff --git a/lib/std/syncio.nim b/lib/std/syncio.nim index 5c97712a4386b..34596bc4d1f32 100644 --- a/lib/std/syncio.nim +++ b/lib/std/syncio.nim @@ -291,13 +291,7 @@ when SupportIoctlInheritCtl: proc c_ioctl(fd: cint, request: cint): cint {. importc: "ioctl", header: "", varargs.} elif defined(posix) and not defined(lwip) and not defined(nimscript): - var - F_GETFD {.importc, header: "".}: cint - F_SETFD {.importc, header: "".}: cint - FD_CLOEXEC {.importc, header: "".}: cint - - proc c_fcntl(fd: cint, cmd: cint): cint {. - importc: "fcntl", header: "", varargs.} + from posix import F_GETFD, F_SETFD, FD_CLOEXEC, fcntl elif defined(windows): type WinDWORD = culong @@ -382,11 +376,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 @@ -677,35 +671,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: "".} = cint - - # fillers ensure correct size & offsets - Stat {.importc: "struct stat", - header: "", 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: "".} = cint - - Stat {.importc: "struct stat", - header: "", final, pure.} = object ## struct stat - st_mode: Mode ## Mode of file - - proc modeIsDir(m: Mode): bool {.importc: "S_ISDIR", header: "".} - ## Test for a directory. - - proc c_fstat(a1: cint, a2: var Stat): cint {. - importc: "fstat", header: "".} + 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, @@ -724,7 +693,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 From b9b547050175a8bcd0a234bb404be636e9d3f023 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Sun, 29 Dec 2024 09:21:21 +0100 Subject: [PATCH 2/3] avoid cyclic imports --- lib/posix/posix.nim | 7 +++---- lib/std/syncio.nim | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim index 15ce82eb32176..f6e42ac6997a8 100644 --- a/lib/posix/posix.nim +++ b/lib/posix/posix.nim @@ -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? @@ -576,9 +575,9 @@ proc nice*(a1: cint): cint {.importc, header: "".} proc pathconf*(a1: cstring, a2: cint): int {.importc, header: "".} proc pause*(): cint {.importc, header: "".} -proc pclose*(a: File): cint {.importc, header: "".} +proc pclose*(a: CFilePtr): cint {.importc, header: "".} proc pipe*(a: array[0..1, cint]): cint {.importc, header: "".} -proc popen*(a1, a2: cstring): File {.importc, header: "".} +proc popen*(a1, a2: cstring): CFilePtr {.importc, header: "".} proc pread*(a1: cint, a2: pointer, a3: int, a4: Off): int {. importc, header: "".} proc pwrite*(a1: cint, a2: pointer, a3: int, a4: Off): int {. diff --git a/lib/std/syncio.nim b/lib/std/syncio.nim index 34596bc4d1f32..12d6bc0d6c4b9 100644 --- a/lib/std/syncio.nim +++ b/lib/std/syncio.nim @@ -16,10 +16,10 @@ when defined(windows): import std/widestrs # ----------------- IO Part ------------------------------------------------ +from system/ansi_c import CFilePtr + type - CFile {.importc: "FILE", header: "", - 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. From d0a3755abea8322af18ff32514c0bc34f1b5a60a Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Sun, 29 Dec 2024 09:27:38 +0100 Subject: [PATCH 3/3] FileHandle --- lib/posix/posix.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim index f6e42ac6997a8..4f9a311954b5c 100644 --- a/lib/posix/posix.nim +++ b/lib/posix/posix.nim @@ -585,7 +585,7 @@ proc pwrite*(a1: cint, a2: pointer, a3: int, a4: Off): int {. proc read*(a1: cint, a2: pointer, a3: int): int {.importc, header: "".} when not defined(nintendoswitch): proc readlink*(a1, a2: cstring, a3: int): int {.importc, header: "".} -proc ioctl*(f: FileHandle, device: uint): int {.importc: "ioctl", +proc ioctl*(f: cint, device: uint): int {.importc: "ioctl", header: "", varargs, tags: [WriteIOEffect].} ## A system call for device-specific input/output operations and other ## operations which cannot be expressed by regular system calls