/* ftpwin.c */
/*% cl -W3 -Zi -DWIN32 mftp.c ftpdata.c scon.c ftpwin.c reg.c wsock32.lib advapi32.lib */
#include "scon.h"

#ifdef WIN32
void errbox(char *file, int line, DWORD dwErr, char *more){
	LPVOID lpMsgBuf= NULL;
	FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL, dwErr,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR) &lpMsgBuf,8,NULL );
	if(lpMsgBuf){
		char *cp= (char *)lpMsgBuf;
		cp += strlen(cp);
		while(--cp>=(char *)lpMsgBuf && *cp<=' ')*cp= 0;
		printf("%s,%d: Error %d %s %s", file,line, dwErr, more, lpMsgBuf);
		LocalFree(lpMsgBuf);
	}else{
		printf("%s,%d: Error %d %s", file,line, dwErr, more);
	}
}
void setlocalfiledate(char *fname, char *ymdhms){
	SYSTEMTIME s;
	FILETIME ft;
	HANDLE hfile;
	char *cp= ymdhms+4;
	int n;

	trim(cp);
	n= strlen(cp);
	if(n<14)
		return;
	cp[14]= 0;
	//MDTM returns 213 YYYYMMDDHHMMSS
	s.wSecond= atoi(cp+12); cp[12]= 0;
	s.wMinute= atoi(cp+10); cp[10]= 0;
	s.wHour=   atoi(cp+ 8); cp[ 8]= 0;
	s.wDay=    atoi(cp+ 6); cp[ 6]= 0;
	s.wMonth=  atoi(cp+ 4); cp[ 4]= 0;
	s.wYear=   atoi(cp);
	if(s.wYear<1600 || s.wYear>3200)
		return;
	SystemTimeToFileTime(&s, &ft);
	hfile= CreateFile(fname, GENERIC_WRITE, 0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(hfile!=INVALID_HANDLE_VALUE){
		SetFileTime(hfile, NULL, NULL, &ft);
		CloseHandle(hfile);
	}
}
void setremotefiledate(char *filename, char *fname){
	SYSTEMTIME s;
	FILETIME ft;
	HANDLE hfile;
	char buf[256];

	hfile= CreateFile(filename, GENERIC_READ, 0,NULL,OPEN_EXISTING,0,NULL);
	if(hfile==INVALID_HANDLE_VALUE)
		return;
	GetFileTime(hfile, NULL, NULL, &ft);
	CloseHandle(hfile);
	FileTimeToSystemTime(&ft, &s);
	sprintf(buf, "MDTM %04d%02d%02d%02d%02d%02d %s\r\n",
		s.wYear,s.wMonth,s.wDay,s.wHour,s.wMinute,s.wSecond, fname);
	send_command_fp(buf, 0, 0); 
}
char *localfilelist(char *apath){
	// list the files in apath, nul-separated. List ends with an extra nul.
	WIN32_FIND_DATA wfd;
	HANDLE hf= FindFirstFile(apath, &wfd);
	int i;
	char *cpn;
	int zf= 2*MAX_PATH;
	int nb= 0;
	char *flist= malloc(zf);

	if(!globbing){
		memset(flist, 0, MAX_PATH);
		strcpy(flist, apath);
		return flist;
	}
	for(i= 0; apath[i]; i++)if(apath[i]=='/')apath[i]= '\\';
	if(cpn= strrchr(apath, '\\'))
		*cpn++= 0;

	flist[0]= 0;
	if(hf==INVALID_HANDLE_VALUE){
		DWORD dwErr= GetLastError();
		if(dwErr==ERROR_FILE_NOT_FOUND){
			if(verbose)
				printf("%s: Not found\n", apath);
		}else{
			errbox(__FILE__,__LINE__,dwErr,apath);
			//printf("%s: Error %d\n", apath, dwErr);
		}
		return flist;
	}
	do{
		if(0==(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)){
			int n= strlen(wfd.cFileName);
			int m= cpn? strlen(apath) : 0;
			if(nb+n+m+3>=zf){
				char *fnew= realloc(flist, zf += 2*MAX_PATH);
				if(!fnew){
					flist[0]= 0;
					break;
				}
			}
			if(m){
				strcpy(flist+nb, apath);
				nb += m;
				flist[nb++]= '\\';
			}
			strcpy(flist+nb, wfd.cFileName);
			nb += n;
			flist[nb++]= 0;
			flist[nb]= 0;
		}
	}while(FindNextFile(hf, &wfd));
	FindClose(hf);
	if(cpn)
		*--cpn= '\\';
	return flist;
}
int getraw(char *buf, int nb){
	// read up to nb characters with no echo
	HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
	DWORD cRead, fdwMode, fdwOldMode; 
	int n;

	if (hStdin == INVALID_HANDLE_VALUE )
		return -1;
	if (! GetConsoleMode(hStdin, &fdwOldMode)) 
		return -2;
	fdwMode = fdwOldMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
	if (! SetConsoleMode(hStdin, fdwMode)) 
		return -3;
	FlushConsoleInputBuffer(hStdin);
	for(n= 0; n<nb; ){
		if (!ReadFile(hStdin, buf+n, 1, &cRead, NULL)) 
			break; 
		if (buf[n] == '\b'){
			if(n>0)
				--n;
		}else if (buf[n] != '\r') 
			++n;
		else
			break;
	}
	// Restore the original console mode. 
	if(!SetConsoleMode(hStdin, fdwOldMode)) 
		return -4;
	return n;
}
int lasterror(){
	int e= WSAGetLastError();
///	WSASetLastError(0);
	return e;
}
int errormsg(char *msg, char *file, int lineno){
	int err= lasterror();
	if(err)
		errbox(file, lineno, err, msg);
	return err;
}
#endif /* WIN32 */

