#if defined(USE_WINFILE)||defined(__linux__)
#include <errno.h>
#include "editor.h"
#include "main.h"
#include "getfile.h"
#ifdef USE_WINFILE
#include <windows.h>
#endif
#ifdef BIOSDISK
#include "biosdisk.h"
#endif
FILEHANDLE openreading(const char *filename) ;
int Editor::selectdisk(void) {
#ifdef USE_WINFILE
extern OSVERSIONINFO WindowsVersion;
	int hardnr=0,drivenr=0;
	DWORD driv=0;
	int flops=0;
	HANDLE filehandle;
	const int maxhard=20;
	int partnums[maxhard],totpart=0;
	memset(partnums,'\0',sizeof(partnums));
#ifdef BIOSDISK
if(WindowsVersion.dwOSVersionInfoSize&&WindowsVersion.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS) 
{
	for(hardnr=0;;hardnr++) {
			if(!(diskpresent(hardnr|0x80))) {
					break;
					}
			}
	for(drivenr=0;;drivenr++) {
			if(!(diskpresent(drivenr))) {
					break;
					}
			}
	}
else 
#endif
{
	for(hardnr=0;hardnr<maxhard;hardnr++) 
	{
		const int end=17;
		char disk[end+4+1]="\\\\.\\PHYSICALDRIVE";
		sprintf(disk+end,"%d",hardnr);	
		if((filehandle=openreading(disk))==MY_INVALID_HANDLE_VALUE) 
		{
			break;	
		}	
		CloseHandle(filehandle);
		int len;
		char parti[80];
		int partnr;
		for(partnr=0;;partnr++) {
			len=sprintf(parti,"\\Device\\Harddisk%d\\Partition%d",hardnr,partnr);
				if((filehandle=openreading(parti))==MY_INVALID_HANDLE_VALUE) 
				{
					break;	
				}	
			CloseHandle(filehandle);
			}
		totpart+=partnr;
		partnums[hardnr++]=partnr;
	}
	driv=	GetLogicalDrives();
	for(int i=0;i<32;i++) 
				if(driv&(i<<i)) 
					drivenr++;

flops=0;
	if((filehandle=openreading("\\\\.\\A:"))!=MY_INVALID_HANDLE_VALUE) 
	{
		flops|=1;
		drivenr++;
		CloseHandle(filehandle);
	}	
	if((filehandle=openreading("\\\\.\\B:"))!=MY_INVALID_HANDLE_VALUE) 
	{
		flops|=2;
		drivenr++;
		CloseHandle(filehandle);
	}	
}
int disknr=hardnr+drivenr+totpart;
int deviter=drivenr;
	char **devices=(char**)alloca(disknr*sizeof(char*));
	for(int i=0;i<hardnr;i++) {
			const int end=17;
			char disk[]="\\\\.\\PHYSICALDRIVE";
			char *dev=devices[deviter++]=(char *)alloca(end+4+1);	
			strcpy(dev,disk);
			sprintf(dev+end,"%d",i);	
			for(int j=0;j<partnums[i];j++) 
			{
				  char *parti;
					parti=devices[deviter++]=(char *)alloca(35);
					sprintf(parti,"\\Device\\Harddisk%d\\Partition%d",i,j);
				
			}
			}
#ifdef BIOSDISK
if(WindowsVersion.dwOSVersionInfoSize&&WindowsVersion.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS) 
{
	for(int i=0;i<drivenr;i++) 
	{
		devices[i]=(char *)alloca(7);	
		sprintf(devices[i],"\\\\.\\%c:",'A'+i);
	}
}
else 
#endif
{
int diskiter=0;
		if(flops&1)
			devices[diskiter++]="\\\\.\\A:";
		if(flops&2)
			devices[diskiter++]="\\\\.\\B:";
	for(int i=0;i<32;i++) 
		if(driv&(i<<i)) {
			devices[diskiter]=(char *)alloca(3);	
			sprintf(devices[diskiter++],"\\\\.\\%c:",'A'+i);
			}
}
#else
#ifdef __linux__
	char parts[]="/proc/partitions";
	FILE *han=fopen(parts,"rb");
	char buf[1024];
	long long bl;
	long maj,min;
	char name[512];
	long line=0,maxline,disknr;
	char **devices;
	if(!han) {
			fprintf(stderr,"Can't open %s\n",parts);
			return -1;
			}
	
	for(maxline=0;fgets(	buf,1024,han);maxline++) 
			;
	fseek(han,0L,SEEK_SET);
	if(!fgets(	buf,1024,han)) {
			fprintf(stderr,"%ld: read from %s failed\n",line,parts);
			return -1;
			}
	line++;
	if(!fgets(	buf,1024,han)) {
			fprintf(stderr,"%ld: read from %s failed\n",line,parts);
			return -1;
			}
	devices=(char **)malloc((maxline-2)*sizeof(char *));
	if(!devices) {
			fprintf(stderr,__FILE__ "%d malloc failed\n",__LINE__);
			return -1;
			}
	while(fgets(	buf,1024,han)) 
	{
		int len,num;
	  line++;
		if(sscanf(buf,"%ld%ld%lld%s",&maj,&min,&bl,name)!=4)
			fprintf(stderr,"%s line %ld: sscanf failed\n",parts,line);
		len=strlen(name);
		num=line-2;
		devices[num]=(char* )malloc(len+5+1);
		strcpy(devices[num],"/dev/");
		strcpy(devices[num]+5,name);
	}
	fclose(han);
	disknr=line-1;
#endif
#endif
	static int take=0;
	int ant;
	if(take<0||take>=disknr)
		take=0;
	char label[]="Select drive and Press Enter";
	while((ant=selectitemindex(label,devices,disknr,take))!=LONG_MAX) {
			if(ant==(LONG_MAX-1)) {
				menuresized();
				wrefresh(editscreen);
				}
			else {
					editup();
					if(ant>=0&&ant<disknr) {
						take=ant;
							char *disk=devices[ant];
						if(!editfile(disk)) {
								beep();	
								if(errno)
									message("Can't open %s: %s",disk,strerror(errno));
								else 
									message("Can't open %s: zero size device?",disk);
								return -1;
								}
					return ant;
						}
					else {	
						message("selectdisk: %d out of range",ant);
						return -2;
						}
				 }
		}
	editup();
	return 0;
	}
	/*
#ifdef BIOSDISK
int Editor::freelib16(void) {
	if(::freelib16)	{
		freelib16();
		message("freed lib16");
		}
	return 0;
	}
#endif
*/
#endif
