#define COMP_MAX 256
#define L_MAX 8192 

#define OK 0
#define erro_na_alocacao_de_memoria 1 

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

char *nome_do_prog;
int status = OK;

void 
quicksort (char **tab, int l, int r)
{
  int j;

  if (l < r)
    {
      {
	char *pivot;
	char *ptr_aux;
	int i;

	pivot = tab[l];
	i = l - 1;
	j = r + 1;
	while (1)
	  {
	    while (strcmp (tab[--j], pivot) > 0);
	    while (strcmp (tab[++i], pivot) < 0);
	    if (i >= j)
	      break;
	    ptr_aux = tab[i], tab[i] = tab[j], tab[j] = ptr_aux;
	  }
      }
      quicksort (tab, l, j);
      quicksort (tab, j + 1, r);
    }
}

main (argc, argv)
     int argc;
     char **argv;
{

  char linha[COMP_MAX];
  char *s;
  int i;
  int n_linhas;
  int tmp;

  char *texto[L_MAX];

  nome_do_prog = argv[0];

  for (i = 0; i < L_MAX; i++)
    texto[i] = NULL;

  i = 0;
  while (gets (linha))
    {
      tmp = strlen (linha);
      s = (char *) malloc (tmp + 2);

      if (s == NULL)
	{
	  status = erro_na_alocacao_de_memoria;
	  fprintf (stderr, "%s: Faltou memoria!\n", nome_do_prog);
	  exit (status);
	}
      strcpy (s, linha);
      s[tmp] = '\n';

      s[++tmp] = '\0';
      texto[i++] = s;
    }
  n_linhas = i;

  quicksort (texto, 0, n_linhas - 1);

  for (i = 0; texto[i]; i++)
    printf ("%s", texto[i]);

  exit (status);
}



