Skip to content

Instantly share code, notes, and snippets.

@cipharius
Last active January 22, 2018 02:09
Show Gist options
  • Save cipharius/aa9387d11c42d4cdd439191ecbb30499 to your computer and use it in GitHub Desktop.
Save cipharius/aa9387d11c42d4cdd439191ecbb30499 to your computer and use it in GitHub Desktop.
Yes implementation in Nim
import os
# Handle Ctrl-C signal
setControlCHook(proc() {.noconv.} = quit 0)
# Use POSIX write
proc write(fd: cint, buffer: pointer, count: cint) {.header: "<unistd.h>", importc: "write".}
const pageSize = 4096
var yes: string
var buffer = ""
var bufferLen: cint
# Use argument or y
if os.paramCount() > 0:
yes = os.paramStr(1) & '\10'
else:
yes = "y\n"
# Fill up the buffer
for i in 2..yes.len:
if i*pageSize mod yes.len == 0:
bufferLen = cint(i*pageSize)
break
while buffer.len < bufferLen:
buffer.add(yes)
# Main yes loop
let buffPtr = cast[pointer](addr buffer[0])
while true:
write(1, buffPtr, bufferLen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment