9 void error(const char *err
)
11 fprintf(stderr
, "ERROR: %s\n", err
);
15 int getline(char s
[], int limit
)
18 for(i
= 0; i
< limit
- 1 && (c
= getchar()) != EOF
&& c
!= '\n'; i
++)
22 if(c
== '\n'){ s
[i
++] = c
; }
27 char *dupstr(const char *in
)
29 char *str
= malloc(strlen(in
)+1);
37 int main(int argc
, char *argv
[])
39 int i
, j
, lines
= DEFLINES
;
41 char buffer
[MAXLENGTH
];
44 lines
= atoi(argv
[1]);
45 lines
= -lines
; // input as negative - fixed that here
46 if(lines
< 1) { error("Incorrect Input"); }
48 buff
= malloc(sizeof *buff
* lines
);
49 if(!buff
) { error("Could not allocate enough memory"); }
50 for(i
= 0; i
< lines
; i
++)
57 getline(buffer
, sizeof buffer
); // get line and store in buffer
60 if(buff
[current_line
])
61 free(buff
[current_line
]);
63 buff
[current_line
] = dupstr(buffer
);
64 if(!buff
[current_line
])
66 error("Out of Memory.");
68 current_line
= (current_line
+ 1) % lines
;
70 } while (!feof(stdin
));
71 for( i
= 0; i
< lines
; i
++)
73 j
= (current_line
+ i
) % lines
;
76 printf("%s", buff
[j
]);