#include <windows.h>
#include <string.h>
#include <malloc.h>
int WINAPI
WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
{
STARTUPINFO start;
SECURITY_ATTRIBUTES sec_attrs;
PROCESS_INFORMATION child;
int wait_for_child = FALSE;
DWORD priority_class = NORMAL_PRIORITY_CLASS;
DWORD ret_code = 0;
char *new_cmdline;
char *p;
char modname[MAX_PATH];
if (!GetModuleFileName (NULL, modname, MAX_PATH))
goto error;
if ((p = strrchr (modname, '\\')) == NULL)
goto error;
*p = 0;
new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 3);
*new_cmdline = '"';
strcpy (new_cmdline + 1, modname);
#ifdef CHOOSE_NEWEST_EXE
{
char * best_name = alloca (MAX_PATH + 1);
FILETIME best_time = {0,0};
WIN32_FIND_DATA wfd;
HANDLE fh;
p = new_cmdline + strlen (new_cmdline);
strcpy (p, "\\emacs*.exe\" ");
fh = FindFirstFile (new_cmdline, &wfd);
if (fh == INVALID_HANDLE_VALUE)
goto error;
do
{
if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
|| (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
&& wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
{
best_time = wfd.ftLastWriteTime;
strcpy (best_name, wfd.cFileName);
}
}
while (FindNextFile (fh, &wfd));
FindClose (fh);
*p++ = '\\';
strcpy (p, best_name);
strcat (p, " ");
}
#else
strcat (new_cmdline, "\\emacs.exe\" ");
#endif
while (cmdline[0] == '-' || cmdline[0] == '/')
{
if (strncmp (cmdline+1, "wait", 4) == 0)
{
wait_for_child = TRUE;
cmdline += 5;
}
else if (strncmp (cmdline+1, "high", 4) == 0)
{
priority_class = HIGH_PRIORITY_CLASS;
cmdline += 5;
}
else if (strncmp (cmdline+1, "low", 3) == 0)
{
priority_class = IDLE_PRIORITY_CLASS;
cmdline += 4;
}
else
break;
while (*++cmdline == ' ');
}
strcat (new_cmdline, cmdline);
if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
{
*p = 0;
for (p = modname; *p; p++)
if (*p == '\\') *p = '/';
SetEnvironmentVariable ("emacs_dir", modname);
}
memset (&start, 0, sizeof (start));
start.cb = sizeof (start);
start.dwFlags = STARTF_USESHOWWINDOW | STARTF_USECOUNTCHARS;
start.wShowWindow = SW_HIDE;
start.dwXCountChars = 80;
start.dwYCountChars = 25;
sec_attrs.nLength = sizeof (sec_attrs);
sec_attrs.lpSecurityDescriptor = NULL;
sec_attrs.bInheritHandle = FALSE;
if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, priority_class,
NULL, NULL, &start, &child))
{
if (wait_for_child)
{
WaitForSingleObject (child.hProcess, INFINITE);
GetExitCodeProcess (child.hProcess, &ret_code);
}
CloseHandle (child.hThread);
CloseHandle (child.hProcess);
}
else
goto error;
return (int) ret_code;
error:
MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
return 1;
}