summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2015-12-29 10:48:52 -0700
committermo khan <mo@mokhan.ca>2015-12-29 10:48:52 -0700
commit5d506979c1fd74d96c0af2e84274f9171b80c875 (patch)
tree6c64d02dd96ed4630f7bf88fdd10e9c3f4c937b7
parent61d51ca13eea2b16fc4fde2fb1926e953c350168 (diff)
implement tail -n
-rw-r--r--tail.c29
1 files changed, 24 insertions, 5 deletions
diff --git a/tail.c b/tail.c
index abd57c8..b0f4986 100644
--- a/tail.c
+++ b/tail.c
@@ -1,21 +1,39 @@
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
-int main(int argc, const char *argv[])
+int main(int argc, char *argv[])
{
- FILE *file = fopen(argv[1], "r");
+ int choice;
+ int lines_to_print = 10;
+ char *ep;
+
+ while ( (choice = getopt(argc, argv, "n:")) != -1 ) {
+ switch(choice) {
+ case 'n':
+ lines_to_print = strtol(optarg, &ep, 10);
+ break;
+ case '?':
+ default:
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ FILE *file = fopen(argv[0], "r");
if (file) {
- int current_position = ftell(file);
+ int current_position;
int character;
int lines_found = 0;
- int lines_to_print = 10;
fseek(file, 0L, SEEK_END);
current_position = ftell(file);
while(--current_position >= 0) {
fseek(file, current_position, SEEK_SET);
- if (getc(file) == '\n' && ++lines_found == lines_to_print) {
+ if (getc(file) == '\n' && (++lines_found) - 1 == lines_to_print) {
break;
}
}
@@ -27,4 +45,5 @@ int main(int argc, const char *argv[])
fclose(file);
return 0;
}
+ return 1;
}