Add pwd utility and performance to cat

This commit is contained in:
joachimschmidt557 2019-03-28 12:15:55 +01:00
parent 049289f452
commit 2f6f90dce4
4 changed files with 23 additions and 5 deletions

16
cat.nim
View file

@ -3,11 +3,19 @@ import common
proc catFile(file:string) =
if not existsFile(file):
echo "cat: " & file & ": No such file or directory"
err "cat: " & file & ": No such file or directory"
system.quit 1
var s = newFileStream(file, fmRead)
while not s.atEnd:
write(stdout, s.readChar)
const bufSize = 1024
var
f = open(file, fmRead)
buffer {.noinit.}: array[bufSize, char]
while true:
let readBytes = readBuffer(f, addr(buffer[0]), bufSize)
if readBytes == 0:
break
discard writeBuffer(stdout, addr(buffer[0]), readBytes)
if readBytes < bufSize:
break
proc catStdin() =
var line = ""

View file

@ -2,3 +2,6 @@
const
nimbaseVersion* = "0.0.1"
proc err*(x:string) =
writeLine(stderr, x)

View file

@ -8,6 +8,6 @@ requires "nim >= 0.19.0"
requires "cligen >= 0.9.19"
requires "stint >= 0.0.1"
bin = @["add", "cat", "cp", "div", "echo", "factor", "false", "head", "mkdir", "mul", "rm", "seq", "sleep", "sub", "tee", "touch", "true", "yes"]
bin = @["add", "cat", "cp", "div", "echo", "factor", "false", "head", "mkdir", "mul", "pwd", "rm", "seq", "sleep", "sub", "tee", "touch", "true", "yes"]
binDir = "bin"

7
pwd.nim Normal file
View file

@ -0,0 +1,7 @@
import os, cligen
import common
proc main() =
echo getCurrentDir()
dispatch(main, version=("version", nimbaseVersion))