diff --git a/cat.nim b/cat.nim index 0aa132f..bafd2f6 100644 --- a/cat.nim +++ b/cat.nim @@ -1,15 +1,26 @@ import cligen, os, streams import common +proc catFile(file:string) = + if not existsFile(file): + echo "cat: " & file & ": No such file or directory" + return + var s = newFileStream(file, fmRead) + while not s.atEnd: + write(stdout, s.readChar) + +proc catStdin() = + var line = "" + while stdin.readLine(line): + echo line + proc main(files:seq[string]) = + if files.len == 0: + catStdin() for file in files: if file == "-": - var line = "" - while stdin.readLine(line): - echo line + catStdin() else: - var s = newFileStream(file, fmRead) - while not s.atEnd: - write(stdout, s.readChar) + catFile(file) dispatch(main, version=("version", nimbaseVersion)) diff --git a/head.nim b/head.nim index c73da44..24a72dc 100644 --- a/head.nim +++ b/head.nim @@ -2,6 +2,8 @@ import cligen, os, streams import common proc main(files:seq[string], lines=10, bytes=0) = +# if files.len == 0: +# files.add "-" for file in files: var s = newFileStream(file, fmRead) if bytes > 0: diff --git a/nimbase.nimble b/nimbase.nimble index 35b0c97..59d03fe 100644 --- a/nimbase.nimble +++ b/nimbase.nimble @@ -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", "seq", "sleep", "sub", "tee", "true", "yes"] +bin = @["add", "cat", "cp", "div", "echo", "factor", "false", "head", "mkdir", "mul", "rm", "seq", "sleep", "sub", "tee", "true", "yes"] binDir = "bin" diff --git a/rm.nim b/rm.nim new file mode 100644 index 0000000..22f25cd --- /dev/null +++ b/rm.nim @@ -0,0 +1,16 @@ +import cligen, os +import common + +proc main(files:seq[string], force=false, recursive=false) = + for file in files: + if existsFile(file): + removeFile(file) + elif existsDir(file): + if recursive: + removeDir(file) + else: + echo "rm: cannot remove '" & file & "': Is a directory" + else: + echo "rm: cannot remove '" & file & "': No such file or directory" + +dispatch(main, version=("version", nimbaseVersion))