diff --git a/clite/commands.js b/clite/commands.js index 24043c7..9e0f530 100644 --- a/clite/commands.js +++ b/clite/commands.js @@ -1304,7 +1304,7 @@ head - copy the first part of files Usage: head [OPTION] Options: --n the number of lines to print (default 10) +-n N the number of lines to print (default 10) -? Print this help information `); } @@ -1377,6 +1377,120 @@ Options: return main(args); }); +clite.commands.load('tail',function(args,env,io) { + var stdio = io.include('stdio'); + var clite = io.include('clite'); + var lines = 10; + var chars = 0; + + function help() { + stdio.printf(` +tail - copy the last part of a file +Usage: tail [OPTION] + +Options: +-n N the number of lines to print (default 10) +-c N instead of lines, print the last N characters +-? Print this help information + `); + } + + function writeFile(fd) { + if (!fd) { + stdio.fprintf(io.stderr,'could not open file\n'); + io.exit(1); + return; + } + + var d = stdio.readAll(fd); + + if (chars > 0) { + if (d.length < chars) { + stdio.write(io.stdout,d); + }else{ + stdio.write(io.stdout,d.substring(d.length-chars)); + } + }else{ + var ls = d.split('\n'); + if (ls[ls.length-1] == '') + ls.pop(); + + while (ls.length > lines) { + ls.shift(); + } + + ls.forEach(function(l) { + stdio.printf('%s\n',l); + }); + } + + stdio.close(fd); + io.exit(0); + } + + function main(args) { + var lnext = false; + var cnext = false; + var short = null; + var file = null; + for (var i=1; i