#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
int main()
{
char *lines[N] = {};
unsigned i = 0;
for (;;) {
unsigned len = 1024;
char *line = malloc(len);
if (NULL == line) {
perror("malloc");
exit(EXIT_FAILURE);
}
for (unsigned j = 0;; j++) {
if (j >= len) {
len += 1024;
line = realloc(line, len);
if (NULL == line) {
perror("realloc");
exit(EXIT_FAILURE);
}
}
int c = fgetc(stdin);
if (c == EOF) {
if (ferror(stdin)) {
perror("fgetc");
exit(EXIT_FAILURE);
} else {
goto end_of_file;
}
}
line[j] = c;
if (line[j] == '\n') {
break;
}
}
free(lines[i % N]);
lines[i % N] = line;
if (lines[i % N] == NULL) {
perror("strdup");
exit(EXIT_FAILURE);
}
i++;
}
end_of_file:
for (unsigned j = 0; j < N; j++) {
if (lines[(i + j) % N] == NULL) break;
fputs(lines[(i + j) % N], stdout);
free(lines[(i + j) % N]);
}
if (EOF == fflush(stdout)) {
perror("fflush");
exit(EXIT_FAILURE);
}
return EXIT_FAILURE;
}