/*      wildname.c                                    03.05.95       */
/*
/  --------------------------------------------------------------
/  Copyright (C) 1993: Michael Braun
/                      Kaetinger Muehlenweg 103 A
/                      D-28816 Stuhr
/  --------------------------------------------------------------
/
/    ausgelagerte functions von mbedit.c
/
/     read files wild wildcard names (e.g. "*.c")
/
*/


/************************/
/*  include files       */
/************************/

#include "config.h"
#include "standard.h"
#include "global.h"
#include "directry.h"
#include "wildname.h"


#if WILD_NAMES


/* some constants */

#if (ACT_OP_SYSTEM == MS_DOS)
#define DOS_HIGH_SPEED 1        /* <== select here */
#else
#define DOS_HIGH_SPEED 0        /* must always be 0 !! */
#endif

#define TEST_DISPLAY   0
#define TIMING_TEST    0

#define sel_entry      de[file_ind].name


/* some local variables */

static int window_length;    /* = f (strlen (...text)) */
/* static char file_entry [BUF_256]; */
static int top, left, bot, right;

/* -FF-  */

#if (QSORT_FUNC_ANSI)
static int comp_dirs  (const void *de1, const void *de2)
#else
static int comp_dirs  (de1, de2)
const void *de1;
const void *de2;
#endif
{
   return (((struct DIR_ENTRY *) de2)->dir_flag - 
           ((struct DIR_ENTRY *) de1)->dir_flag);
}



#if (QSORT_FUNC_ANSI)
static int comp_names (const void *de1, const void *de2)
#else
static int comp_names (de1, de2)
const void *de1;
const void *de2;
#endif
{
   return (strcmp (((struct DIR_ENTRY *) de1)->name,
                   ((struct DIR_ENTRY *) de2)->name));
}

/* -FF-  */

struct DIR_ENTRY *get_directory_buffer (char *path  , struct DIR_ENTRY *de,
                                        int *dir_num, int *total_num)
{
DIR *act_dir;
struct dirent *act_ent;
int high_speed, dir_flag;

struct stat buff;
int save_len;

/* step 1.) */

/* check, if directory exists */
   if ((act_dir = opendir (path)) == NULL)
      return NULL;

/* count no of files */
#if TEST_DISPLAY
   fprintf (stderr, "Counting Directory ...\n\r");
#endif
   *total_num = 0;
   while ((act_ent = readdir (act_dir)) != NULL)
   {
      (*total_num)++;  /* count every entry, directories + files. */
   }                   /* so we are on the safe side. */
   closedir (act_dir);

#if TEST_DISPLAY
   fprintf (stderr, "*total_num = %d\n\r", *total_num);
#endif

#if 0
/* free old buffer */
   if (de)
      free (de);
#endif

/* get dynamic memory */
   de = (struct DIR_ENTRY *) malloc (*total_num * sizeof (struct DIR_ENTRY));
   if (de == NULL)  /* NEU ! 29.09.94 */
      return NULL;

/* step 2.) */

/* read the filenames into allocated buffer */
   if ((act_dir = opendir (path)) == NULL)
      return NULL;

#if TEST_DISPLAY
   fprintf (stderr, "Reading Directory ...\n\r");
#endif
   *total_num = 0;
   *dir_num = 0;
   while ((act_ent = readdir (act_dir)) != NULL)
   {
   /* look for valid pathname and type (dir or file) */

   /* decide, which method is used (stat = slow) */
      high_speed = 1;
      if ((!DOS_HIGH_SPEED) ||
          (strcmp(act_ent->d_name, "..") == 0))
         high_speed = 0;

      dir_flag = -1;   /* invalid */

#if (ACT_OP_SYSTEM == MS_DOS)
      if (high_speed)
      {
         dir_flag = ((act_ent->d_ino & _A_SUBDIR) != 0);
      }
      else
#endif
      {
      /* construct full pathname */
         save_len = strlen (path);
         strncat (path, FILE_SEPARATOR, BUF_256);
         strncat (path, act_ent->d_name, BUF_256);

         if (!stat (path, &buff))
            dir_flag = ((buff.st_mode & S_IFMT) == S_IFDIR);

      /* restore old pathname */
         path[save_len] = '\0';
      }

   /* valid file entry ? */   /* dir_flag = -1 : invalid entry */
                              /*          =  0 : file          */
                              /*          =  1 : directory     */
      if (dir_flag >= 0)
      {
         (*dir_num) += dir_flag;

         de [*total_num].dir_flag = dir_flag;
         strncpy (de [*total_num].name, act_ent->d_name, MAX_FILENAME); 
         (*total_num)++;
      }
   }
   closedir (act_dir);

#if TEST_DISPLAY
   fprintf (stderr, "*dir_num = %d\n\r", *dir_num);
#endif


/* step 3.) */
#if TEST_DISPLAY
   fprintf (stderr, "Sorting Directory ...\n\r");
#endif

/* sort all entries */
/* sort dirs on top */
   qsort (de, *total_num,
          sizeof(struct DIR_ENTRY), comp_dirs);

/* sort entries (separate for dirs + files) */
   qsort (de, *dir_num,
          sizeof(struct DIR_ENTRY), comp_names);

   qsort (&de[*dir_num], (*total_num - *dir_num),
          sizeof(struct DIR_ENTRY), comp_names);


#if TEST_DISPLAY
   fprintf (stderr, "Ready !\n\r");
#endif

   return de;

}  /* get_directory_buffer */

/* -FF-  */

#endif

/* Modification History */
/* 03.05.95 - file erzeugt */
