about summary refs log tree commit diff stats
path: root/src/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/io')
-rw-r--r--src/io/stdio.nim25
-rw-r--r--src/io/tempfile.nim18
2 files changed, 43 insertions, 0 deletions
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
href='/akkartik/mu/blame/subx/037compute_segment_address.cc?h=hlt&id=a49bc41365bf6b4f0c006c5fbdcb4b519634c42c'>^
73aa4d14 ^

5c368edc ^
1bbbf14f ^



c442a5ad ^
1bbbf14f ^


83c67014 ^


4e8f5fa4 ^
87d5bdb9 ^
a6517ed8 ^

4a99a6e0 ^
1bbbf14f ^

a6517ed8 ^










a6061b9f ^

a6517ed8 ^

8998908e ^
a6061b9f ^


3c46d5a2 ^

a6061b9f ^




8998908e ^













1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86