Skip to content

Commit 13af5da

Browse files
committed
More shell lab
1 parent bc97a3c commit 13af5da

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

examples/shell.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ var commands = {
2222
'tail': function (args) {
2323
fs.stat(args[0], function (err, stats) {
2424

25+
if (stats.size === 0) {
26+
return;
27+
}
28+
2529
var options = {
26-
flags: 'r',
27-
encoding: 'utf8',
28-
mode: 0666,
29-
bufferSize: stats.blksize,
30-
start: 0,
31-
end: stats.size
30+
flags: 'r',
31+
encoding: 'utf8',
32+
mode: 0666,
33+
bufferSize: stats.blksize,
34+
start: 0,
35+
end: stats.size
3236
};
3337

3438
var offset = 0;
35-
var numLines = 10;
39+
// Keep track of one extra newline
40+
// So we can start reading in the contents starting
41+
// at the next character
42+
var numLines = 1 + 1;
3643
var newLines = new Array(numLines);
3744
var index = 0;
3845

@@ -50,13 +57,19 @@ var commands = {
5057
});
5158

5259
fileStream.on('end', function () {
53-
var end = newLines.splice(0, index);
54-
newLines = newLines.concat(end);
55-
options.start = newLines[0] + 1;
60+
if (typeof newLines[index] === 'number') {
61+
var position = newLines[index] + 1;
62+
}
63+
else {
64+
var position = 0;
65+
}
66+
67+
var bytesToRead = stats.size - position;
5668

57-
fs.createReadStream(args[0], options)
58-
.on('data', function (d) {
59-
console.log(d.toString());
69+
fs.open(args[0], 'r', function (err, fd) {
70+
var buffer = new Buffer(bytesToRead);
71+
fs.readSync(fd, buffer, 0, bytesToRead, position);
72+
console.log(buffer.toString())
6073
});
6174
});
6275
});

0 commit comments

Comments
 (0)