/***************************************************************************/
/* Programa criado por Wenderson Teixeira                                  */
/* Todos os Direitos Reservados (c) 1999                                   */
/* casa@programadores.com.br                                               */
/* http://casa.dos.programadores.com.br/                                   */
/* ICQ:1597462                                                             */
/***************************************************************************/

#include <stdio.h>
#include <io.h>
#include <dir.h>
#include <alloc.h>
#include "linklist.h" 

typedef struct ffblk  TFileAttr;

/* Funcoes definidas para utilizao com a biblioteca de listas generica     */

/***************************************************************************/
TFileAttr *CreateItem(TFileAttr *item) 
{ 
  TFileAttr *pTmp = malloc(sizeof(TFileAttr));
  memcpy(pTmp, item, sizeof(TFileAttr));
  return pTmp;
} 
/***************************************************************************/
bool IsEqual(void *i1, void *i2)
{
  return strcmp(((TFileAttr *)i1)->ff_name, ((TFileAttr *)i2)->ff_name) == 0;
}
/***************************************************************************/
int Compare(void *i1, void *i2)
{
  return strcmp(((TFileAttr *)i1)->ff_name, ((TFileAttr *)i2)->ff_name);
} 
/***************************************************************************/
void Delete(void *item) 
{ 
  free(item);
} 
/***************************************************************************/
TList *FindItem(TList *root, TFileAttr *item) 
{ 
  return Find(root, (void *)item, IsEqual);
} 
/***************************************************************************/
void SortList(TList *root, int order) 
{ 
  BubbleSort(root, order, Compare); 
} 
/***************************************************************************/
TList *RemoveItem(TList *root, char *item) 
{ 
  return Remove(root, item, IsEqual, Delete); 
} 
/***************************************************************************/
void DisplayList(TList *root) 
{ 
  TList *iter = root; 
  while(iter) 
  { 
    TFileAttr *file = (TFileAttr *)iter->Data;
    struct ftime *ft = (struct ftime *)&file->ff_ftime;
    
    printf("%-13s  %11ld     %02d/%02d/%02d     %2d:%02d:%02d\n", 
            file->ff_name, file->ff_fsize, 
            ft->ft_day, ft->ft_month, ft->ft_year + 80,
            ft->ft_hour, ft->ft_min, ft->ft_tsec << 1);
    iter = iter->Next;
  } 
} 
/***************************************************************************/
TList *ListDir(const char *path)
{
  TList *root = NULL;
  TFileAttr files;
  int result;
  
  result = findfirst(path, &files, 0);
  while(!result)
  {
    TFileAttr *pTmp = CreateItem(&files);
    root = Add(root, pTmp);
    
    result = findnext(&files);
  }
  
  return root;
}
/***************************************************************************/
void main(int ArgC, char *ArgV[])
{
  TList *root = ListDir(ArgC > 1 ? ArgV[1] : "*.*");
  
  SortList(root, UP);
  DisplayList(root);
    
  while(root) 
    root = RemoveItem(root, root->Data); 
}
/***************************************************************************/
1