#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *read_file(char *fname)
{
FILE *fp;
size_t filesize;
char *buff;
fp = fopen(fname, "rb");
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
buff = (char *)calloc(1, filesize + 1);
buff[filesize] = '\0';
fseek(fp, 0, SEEK_SET);
fread(buff, filesize, 1, fp);
fclose(fp);
return buff;
}
int main(int argc, char **argv)
{
char *buff;
char **index = 0;
char **p;
char *token;
int count = 0;
buff = read_file(argv[1]);
token = strtok(buff, "\n");
while (token) {
count++;
index = (char **)realloc(index, (count + 1) * sizeof(*index));
if (count == 1) {
index[0] = 0;
}
index[count] = 0;
for (p = index; *p && strcmp(token, *p) > 0; p++) ;
memmove(p + 1, p, (index + count - p) * sizeof(*p));
*p = token;
token = strtok(NULL, "\n");
}
index[count] = 0;
p = index;
while (*p) {
printf("%s\n", *p++);
}
free(index);
free(buff);
return 0;
}