#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

FILE *f1,*f2,*f,*fal;
int b;

char* getline (FILE *fal)
{
  char* str;
  int i=0;
  int ifeof;
  int ch;
  
  str=(char*)malloc(sizeof(char)*2);
  while (((ch=fgetc(fal))!=EOF) && (ch!='\n')) 
  {
    str[i]=ch;
    i++;
    str=(char*)realloc(str, sizeof(char)*(i+5));
  };
  b=(ch!=EOF);
  str[i]='\n';
  str[i+1]='\0';
  return str;
} 


int main (int argc, char** argv){
int i;
char *s1, *s2, *str;
//----------- conflux -----------
f = fopen ("temp", "w");

for (i=1; i<argc; i++) 
  {
    fal=fopen (argv[i], "r");
    b=1;
    while (b)  {
    str = getline (fal);
    fputs (str,f);
    free (str);
               }
    fclose(fal);
  }
fclose(f);
//-------------------------------
//------------ sort -------------
f = fopen ("temp", "r");
f1 = fopen ("temp1.c", "w");
f2 = fopen ("temp2.c", "w");
s1=getline (f);
fputs (s1,f1);
s2=getline (f);
while (b)
{ 
reverse:
 while (strcmp (s1,s2)<0) 
  {
   fputs (s2, f1);
   free(s1);
   s1=s2;
   free(s2);
   s2=getline(f);
  }
 s1=s2;
 fputs (s1, f2);
 s2=getline (f);
 while (strcmp (s1,s2)<0) 
  {
   fputs (s2, f2);
   free(s1);
   s1=s2;
   free(s2);
   s2=getline(f);
  }
  goto reverse;
}
fclose(f1);
fclose(f2);
fclose(f);
return 1;
}


