diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim index 15ce82eb32176..4f9a311954b5c 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 {. @@ -586,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 diff --git a/lib/std/syncio.nim b/lib/std/syncio.nim index 911bff276e173..1a0e27c0bc08b 100644 --- a/lib/std/syncio.nim +++ b/lib/std/syncio.nim @@ -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: "", - 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. @@ -293,13 +293,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 @@ -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 @@ -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: "".} = 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, @@ -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