summaryrefslogtreecommitdiff
path: root/tail.c
blob: abd57c8849a6872a291ea320cc00d503cce5d261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <string.h>

int main(int argc, const char *argv[])
{
  FILE *file = fopen(argv[1], "r");
  if (file) {
    int current_position = ftell(file);
    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) {
        break;
      }
    }

    while( (character = getc(file)) != EOF) {
      putchar(character);
    }

    fclose(file);
    return 0;
  }
}