From d26766c4c4015990703e84e8136f96d222edbc97 Mon Sep 17 00:00:00 2001 From: bptato Date: Thu, 14 Mar 2024 20:57:45 +0100 Subject: Move around some modules * extern -> gone, runproc absorbed by pager, others moved into io/ * display -> local/ (where else would we display?) * xhr -> html/ * move out WindowAttributes from term, so we don't depend on local from server --- src/io/stdio.nim | 25 +++++++++++++++++++++++++ src/io/tempfile.nim | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/io/stdio.nim create mode 100644 src/io/tempfile.nim (limited to 'src/io') diff --git a/src/io/stdio.nim b/src/io/stdio.nim new file mode 100644 index 00000000..729b50f6 --- /dev/null +++ b/src/io/stdio.nim @@ -0,0 +1,25 @@ +import std/posix + +proc closeHandle(fd, flags: cint) = + let devnull = open("/dev/null", flags) + doAssert devnull != -1 + if devnull != fd: + discard dup2(devnull, fd) + discard close(devnull) + +proc closeStdin*() = + closeHandle(0, O_RDONLY) + +proc closeStdout*() = + closeHandle(1, O_WRONLY) + +proc closeStderr*() = + closeHandle(2, O_WRONLY) + +proc safeClose*(fd: cint) = + if fd == 0: + closeStdin() + elif fd == 1 or fd == 2: + closeHandle(fd, O_WRONLY) + else: + discard close(fd) diff --git a/src/io/tempfile.nim b/src/io/tempfile.nim new file mode 100644 index 00000000..5968270b --- /dev/null +++ b/src/io/tempfile.nim @@ -0,0 +1,18 @@ +import std/os + +var tmpf_seq: int +proc getTempFile*(tmpdir: string, ext = ""): string = + if not dirExists(tmpdir): + createDir(tmpdir) + var tmpf = tmpdir / "chatmp" & $getCurrentProcessId() & "-" & $tmpf_seq + if ext != "": + tmpf &= "." + tmpf &= ext + while fileExists(tmpf): + inc tmpf_seq + tmpf = tmpdir / "chatmp" & $tmpf_seq + if ext != "": + tmpf &= "." + tmpf &= ext + inc tmpf_seq + return tmpf -- cgit 1.4.1-2-gfad0