#include "defines.h"
/*     Bed a Binary EDitor for Linux and for Rxvt running under Linux.       */
/*     Copyright (C) 1998  Jaap Korthals Altes <jkaltes@hetnet.nl>      */
/*                                                                           */
/*     Bed is free software; you can redistribute it and/or modify           */
/*     it under the terms of the GNU General Public License as published by  */
/*     the Free Software Foundation; either version 2 of the License, or     */
/*     (at your option) any later version.                                   */
/*                                                                           */
/*     Bed is distributed in the hope that it will be useful,                */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
/*     GNU General Public License for more details.                          */
/*                                                                           */
/*     You should have received a copy of the GNU General Public License     */
/*     along with bed; if not, write to the Free Software                    */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             */
/*                                                                           */
/* Sun Dec  6 18:34:27 1998                                                  */


#include <features.h>
#include <stdio.h>
#include <unistd.h>
#include <glob.h>

#include <sys/stat.h>
#include "glob.h"

#ifdef __CYGWIN__
#include <stdlib.h>
#include <windows.h>
#endif
int getfiles(char *pattern,char ***files,int *back)			{
	  struct stat st;
	int end,beg;
	  char **was;
	  char *tmp;
#ifdef __CYGWIN__
#define STARTFILES 100

	static char **allfiles=NULL;
	static int allfileslen=0,filenr=0;
 WIN32_FIND_DATA  fdata;

 HANDLE han;
if(allfileslen) {
	int i;
	for(i=0;i<filenr;i++)
		free(allfiles[i]);
	filenr=0;
	}
	 if((han=FindFirstFile(pattern,&fdata))==INVALID_HANDLE_VALUE) {
		*back=0;
		return 0;
		}
do {
	int len=strlen(fdata.cFileName)+1;
	if(filenr>=allfileslen) {
		allfileslen=allfileslen*2+50;
		allfiles=(char **)realloc(allfiles,sizeof(char*)*allfileslen);
		}
	allfiles[filenr]=(char *)malloc(len);
	strcpy(allfiles[filenr],fdata.cFileName);
	filenr++;
	 } while(FindNextFile(han,&fdata));
was=allfiles;
#else
	int filenr;
          static glob_t globbuf;
	static int globed=-1;
if(globed>=0) {
	globfree(&globbuf);
	}
else
	globed=1;
#ifdef GLOB_PERIOD
          glob(pattern,GLOB_PERIOD|GLOB_NOSORT, NULL, &globbuf);
	filenr=globbuf.gl_pathc;
#else

        glob(pattern,GLOB_NOSORT, NULL, &globbuf);
	switch(pattern[0]) {
		case '?': {
			int len=strlen(pattern);		
			char newp[len+1];
			memcpy(newp+1,pattern+1,len);
			newp[0]='.';
			glob(newp,GLOB_APPEND|GLOB_NOSORT, NULL, &globbuf);break;
			break;
			}
		case '*': {
			int len=strlen(pattern);		
			char newp[len+2];
			memcpy(newp+1,pattern,len+1);
			newp[0]='.';
			glob(newp,GLOB_APPEND|GLOB_NOSORT, NULL, &globbuf);break;
			break;
			};
		default:;	
		}	;
	filenr=globbuf.gl_pathc;
#endif
	was=globbuf.gl_pathv;
#endif
	beg=-1;
	end=filenr;

	  while(1) {
	  	do {
			beg++;
	  		if(beg>=end) {
	  			beg--;
	  			goto uit;
	  			}
			stat(was[beg],&st);
			} while(!S_ISDIR(st.st_mode));
		do {
			end--;
	  		if(beg>=end) {
	  			beg--;
	  			goto uit;
	  			}
			stat(was[end],&st);
			} while(S_ISDIR(st.st_mode));
		tmp=was[end];
		was[end]=was[beg];
		was[beg]=tmp;
  		}
  	uit:
  	*files=was;
  	*back=end;
	return filenr;
	}
#ifdef TEST	
void main(int argc, char** argv) {
	char **files;
	int len,end,i;
	len=getfiles(argv[1],&files,&end);
	puts("Files: ");
	for(i=0;i<end;i++) 
		puts(files[i]);
	puts("Directories: ");
	for(i=end;i<len;i++) 
		puts(files[i]);
	len=getfiles(argv[1],&files,&end);
	puts("Files: ");
	for(i=0;i<end;i++) 
		puts(files[i]);
	puts("Directories: ");
	for(i=end;i<len;i++) 
		puts(files[i]);


	}
#endif
