about summary refs log tree commit diff stats
path: root/src/extern/stdio.nim
blob: 729b50f691b33cbdba17381d507af1caa4620782 (plain) (blame)
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
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)