summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2015-12-29 10:25:17 -0700
committermo khan <mo@mokhan.ca>2015-12-29 10:25:17 -0700
commit228c8513cf9830746e87b280f6f7890a351be4a3 (patch)
tree745db66534210984616b83595f5221c9c0d07edd
parent54685bfefd4000db0802192cefd4b987c9aa37f9 (diff)
start to implement tail.
-rw-r--r--head.c2
-rw-r--r--tail.c24
2 files changed, 25 insertions, 1 deletions
diff --git a/head.c b/head.c
index 887b66f..f3f29a7 100644
--- a/head.c
+++ b/head.c
@@ -23,7 +23,7 @@ int main(int argc, char *argv[])
FILE *file = fopen(argv[0], "r");
if (file) {
- int character;
+ int character;
while( (character = getc(file)) != EOF ) {
putchar(character);
diff --git a/tail.c b/tail.c
new file mode 100644
index 0000000..e92992d
--- /dev/null
+++ b/tail.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+
+int main(int argc, const char *argv[])
+{
+ FILE *file = fopen(argv[1], "r");
+ if (file) {
+ int current_position = ftell(file);
+ int character;
+
+ fseek(file, 0L, SEEK_END);
+
+ current_position = ftell(file);
+ printf("file size: %d\n", current_position);
+
+ while(current_position >= 0) {
+ fseek(file, --current_position, SEEK_SET);
+ character = getc(file);
+ putchar(character);
+ }
+
+ fclose(file);
+ return 0;
+ }
+}