Debian Codegraph @

Contrib/SubStart/substart.c

raw
1 /*
2 * substart.c - This app runs the executable of the same name in the 'Bin'
3 * sub-folder and passes along the command line options.
4 *
5 * Copyright (c) 2013-2026 Anders Kjersem
6 *
7 * Licensed under the zlib/libpng license (the "License");
8 * you may not use this file except in compliance with the License.
9 *
10 * Licence details can be found in the file COPYING.
11 *
12 * This software is provided 'as-is', without any express or implied
13 * warranty.
14 */
15
16 #include <windows.h>
17 #include <tchar.h>
18
19 #define SUBFOLDER _T("Bin")
20
21 /*
22 * Leaking things Windows is going to clean up after us anyway avoids
23 * linking to a couple of functions, this saves just enough space
24 * to get the file size down to 2 KiB (MSVC 7.1)
25 */
26 #ifdef _DEBUG
27 # define CANLEAK(cod) cod
28 #else
29 # define CANLEAK(cod)
30 #endif
31
32 #define MemFree LocalFree
33 static void* MemReAlloc(void*OrgMem, size_t cb)
34 {
35 if (!OrgMem) return LocalAlloc(LMEM_FIXED, cb);
36 return LocalReAlloc(OrgMem, cb, LMEM_MOVEABLE);
37 }
38 static void MemZero(void*pMem, size_t cb)
39 {
40 char*p=(char*)pMem;
41 for(; cb;) p[--cb] = 0;
42 }
43
44 static UINT SIOPut(LPCSTR Str, UINT cch)
45 {
46 HANDLE hSO = GetStdHandle(STD_OUTPUT_HANDLE);
47 DWORD cbio;
48 return WriteFile(hSO, Str, cch, &cbio, 0) ? cch : 0;
49 }
50 static UINT SIOFmtPut(LPCSTR Fmt, ...)
51 {
52 UINT cch;
53 char buf[150]; // Plenty for our simple strings
54 va_list val;
55 va_start(val, Fmt);
56 cch = wvsprintfA(buf, Fmt, val);
57 cch = SIOPut(buf, cch);
58 va_end(val);
59 return cch;
60 }
61
62 void mainCRTStartup()
63 {
64 const UINT cchSubDir = (sizeof(SUBFOLDER) / sizeof(TCHAR)) - 1;
65 STARTUPINFO si;
66 PROCESS_INFORMATION pi;
67 UINT ec, cch, cchParams;
68 TCHAR *p = GetCommandLine(), *cmd = 0;
69
70 if (*p == _T('\"'))
71 do ++p; while(*p && *p != _T('\"'));
72 else
73 while(*p && *p > _T(' ')) ++p;
74
75 /* Skip end quote and whitespace */
76 do if (!*p) break; else ++p; while(*p <= ' ');
77
78 ec = ERROR_OUTOFMEMORY;
79 cchParams = lstrlen(p), cch = MAX_PATH;
80 for (;;)
81 {
82 TCHAR *mem;
83 UINT cchTot = 1 + cch + 1 + cchSubDir + 2 + cchParams + 1, cchSelf;
84 mem = (TCHAR*) MemReAlloc(cmd, cchTot * sizeof(TCHAR));
85 if (!mem) goto app_die;
86 cmd = mem;
87 cchSelf = GetModuleFileName(NULL, cmd + 1, cch);
88 if (!cchSelf) goto app_diegle;
89 if (cchSelf < cch)
90 {
91 /* Insert subfolder before the filename */
92 TCHAR *src = cmd + cchSelf + 1, *dst = src + cchSubDir + 1;
93 for(; src > cmd; --src, --dst)
94 {
95 *dst = *src;
96 if (_T('\\') == *src || _T('/') == *src) break;
97 }
98 *++src = _T('\0');
99 lstrcat(src, SUBFOLDER);
100 src[cchSubDir] = _T('\\');
101
102 /* Quote path and append parameters */
103 cmd[0] = _T('\"');
104 lstrcat(cmd, _T("\" "));
105 lstrcat(cmd, p);
106 break;
107 }
108 cch *= 2;
109 }
110
111 MemZero(&si, sizeof(si));
112 si.cb = sizeof(si);
113
114 if (CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
115 {
116 DWORD forkec;
117 WaitForSingleObject(pi.hProcess, INFINITE);
118 GetExitCodeProcess(pi.hProcess, &forkec);
119 ec = forkec;
120 CANLEAK(CloseHandle(pi.hProcess));
121 CANLEAK(CloseHandle(pi.hThread));
122 goto app_end;
123 }
124
125 app_diegle:
126 ec = GetLastError();
127 app_die:
128 SIOFmtPut("Unable to start child process, error %#x\n", ec);
129 app_end:
130 CANLEAK(MemFree(cmd));
131 ExitProcess(ec);
132 }