aboutsummaryrefslogtreecommitdiff
path: root/run.c
blob: a9abca896eb045bb966f2225f4dfcb2451a4b674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <assert.h>
#include <stdio.h>
#include <windows.h>
#include <winbase.h>
#include <winuser.h>

#include <shlwapi.h>
#pragma comment(lib, "Shlwapi")

#ifdef _CONSOLE
#pragma comment(linker, "/SUBSYSTEM:CONSOLE")
#else
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
#endif

#pragma comment(lib, "User32.lib")

#define MAX_ENV 32767

#ifdef _CONSOLE
#define die(...) do { fprintf(stderr , __VA_ARGS__); exit(1); } while (0)
#else
#define die(...) do { \
  sprintf_s(diemsg, MAX_ENV, __VA_ARGS__); \
  MessageBox(NULL, diemsg, "Error", MB_OK | MB_ICONERROR); \
  exit(1); \
} while (0)
char diemsg[MAX_ENV];
#endif

#ifdef _CONSOLE
int main(int argc, char *argv[]) {
#else
int WinMain(HINSTANCE inst, HINSTANCE previnst, LPSTR _cmdline, int show) {
#endif
  TCHAR *args, *cmd, *cmdline, *file, *newcmdline, *p;
  TCHAR exefile[MAX_PATH+1], objfile[MAX_PATH+1];
  TCHAR include[MAX_ENV+1], lib[MAX_ENV+1], path[MAX_ENV+1];
  TCHAR vs[MAX_PATH+1], sdk[MAX_PATH+1];
  DWORD attr, exitcode;
  FILETIME fwrite, cwrite;
  HANDLE fh, ch;
  PROCESS_INFORMATION pi;
  STARTUPINFO si;
  int i;
  
  cmdline = PathGetArgs(GetCommandLine());
  if (cmdline[0] == '\0')
#ifdef _CONSOLE
    die("%s: file [arguments ...]\n", argv[0]);
#else
    die("No file provided.\n");
#endif
  newcmdline = malloc(1 + strlen(cmdline) * sizeof(char));
  if (newcmdline == NULL) die("Could not allocate memory for command string\n");
  strcpy(newcmdline, cmdline);
  for (; *newcmdline == ' '; newcmdline++) ;
  PathRemoveArgs(cmdline);
  if (cmdline[0] == '\0')
    strcpy(cmdline, newcmdline);
  file = cmdline;
  if (file[0] == '"') file++;
  if (file[strlen(file)-1] == '"') file[strlen(file)-1] = '\0';
  
  if (strlen(file) + 1 > MAX_PATH)
    die("File path too long\n");
  strcpy(exefile, file);
  strcpy(objfile, file);
  
  for (i = strlen(file); i > 0; i--)
    if (file[i] == '.') {
      i--;
      break;
    } else if (file[i] == '\\') {
      i = strlen(file) + 1;
      break;
    }
  strcpy(exefile + i + 1, ".exe");
  strcpy(objfile + i + 1, ".obj");
    
done:
  ch = CreateFile(exefile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  if (ch != INVALID_HANDLE_VALUE) {
    fh = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    if (fh == INVALID_HANDLE_VALUE) {
      if (GetLastError() == ERROR_FILE_NOT_FOUND)
        die("File not found: %s\n", file);
      else
        die("Could not open file (CreateFile failed with %d)\n", GetLastError());
    }
    assert(GetFileTime(ch, NULL, NULL, &cwrite) != 0);
    assert(GetFileTime(fh, NULL, NULL, &fwrite) != 0);
    if (CompareFileTime(&fwrite, &cwrite) == -1)
      goto run;
  }
  
  CloseHandle(fh);
  CloseHandle(ch);
  
#define set(var, val) \
  if (SetEnvironmentVariable(var, val) == 0) \
    die("Could not create environment variable");
  
  include[0] = '\0';
  lib[0] = '\0';
  
  if (GetEnvironmentVariable("VSDIR", vs, MAX_PATH+1) != 0) {
    sprintf(include, "%s\\vc\\include", vs);
    sprintf(lib, "%s\\vc\\lib", vs);
    sprintf(path, "%s\\vc\\bin;%s\\Common7\\IDE", vs, vs);
    set("INCLUDE", include);
    set("LIB", lib);
    set("PATH", path);
  }
  
  if (GetEnvironmentVariable("SDKDIR", sdk, MAX_PATH+1) != 0) {
    sprintf(include + strlen(include), ";%s\\include", sdk);
    sprintf(lib + strlen(lib), ";%s\\lib", sdk);
    set("INCLUDE", include);
    set("LIB", lib);
  }
  
  DeleteFile(exefile);
  cmd = malloc(sizeof("cmd /k cl \"\" && exit 0") + strlen(file) * sizeof(char));
  if (cmd == NULL) die("Could not allocate memory for command string\n");
#if _CONSOLE
  sprintf(cmd, "cmd /c cl \"%s\"", file);
#else
  sprintf(cmd, "cmd /k cl \"%s\" && exit 0", file);
#endif
  i = CreateProcess(
    NULL,
    cmd,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL, // use parent's starting directory
    &si,
    &pi);
  if (i == 0) die("CreateProcess failed with %d\n", GetLastError());
  WaitForSingleObject(pi.hProcess, INFINITE);
  GetExitCodeProcess(pi.hProcess, &exitcode);
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  DeleteFile(objfile);
  if (exitcode) {
    return exitcode;
  }
  printf("\n");
  
  attr = GetFileAttributes(exefile);
  if ((attr & FILE_ATTRIBUTE_HIDDEN) == 0)
    SetFileAttributes(exefile, attr | FILE_ATTRIBUTE_HIDDEN);
  
run:
#ifndef _CONSOLE
  ZeroMemory(&si, sizeof(STARTUPINFO));
  si.cb = sizeof(STARTUPINFO);
  si.dwFlags = STARTF_USESHOWWINDOW;
  si.wShowWindow = SW_HIDE;
#endif
  i = CreateProcess(
    exefile,
    newcmdline,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL, // use parent's starting directory
    &si,
    &pi);
  if (i == 0) die("CreateProcess failed with %d\n", GetLastError());
  exitcode = 0;
#ifdef _CONSOLE
  WaitForSingleObject(pi.hProcess, INFINITE);
  GetExitCodeProcess(pi.hProcess, &exitcode);
#endif
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  return exitcode;
}