push 0a6b4a6c9e6bf38a9b3d5ed71f415701d1ca15d1
[wine/hacks.git] / programs / start / start.c
blob773544ff9b454708276e4506715abb9de80340d7
1 /*
2 * Start a program using ShellExecuteEx, optionally wait for it to finish
3 * Compatible with Microsoft's "c:\windows\command\start.exe"
5 * Copyright 2003 Dan Kegel
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <windows.h>
23 #include <winuser.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <shlobj.h>
28 #include "resources.h"
30 /**
31 Output given message to stdout without formatting.
33 static void output(const char *message)
35 DWORD count;
36 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), message, strlen(message), &count, NULL);
39 /**
40 Output given message,
41 followed by ": ",
42 followed by description of given GetLastError() value to stdout,
43 followed by a trailing newline,
44 then terminate.
46 static void fatal_error(const char *msg, DWORD error_code)
48 LPVOID lpMsgBuf;
49 int status;
51 output(msg);
52 output(": ");
53 status = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, (LPTSTR) & lpMsgBuf, 0, NULL);
54 if (!status) {
55 output("FormatMessage failed\n");
56 } else {
57 output(lpMsgBuf);
58 LocalFree((HLOCAL) lpMsgBuf);
59 output("\n");
61 ExitProcess(1);
64 /**
65 Output given message from string table,
66 followed by ": ",
67 followed by description of given GetLastError() value to stdout,
68 followed by a trailing newline,
69 then terminate.
71 static void fatal_string_error(int which, DWORD error_code)
73 char msg[2048];
75 if (!LoadString(GetModuleHandle(NULL), which,
76 msg, sizeof(msg)))
77 fatal_error("LoadString failed", GetLastError());
79 fatal_error(msg, error_code);
82 static void fatal_string(int which)
84 char msg[2048];
86 if (!LoadString(GetModuleHandle(NULL), which,
87 msg, sizeof(msg)))
88 fatal_error("LoadString failed", GetLastError());
90 output(msg);
91 ExitProcess(1);
94 static void usage(void)
96 fatal_string(STRING_USAGE);
99 static void license(void)
101 fatal_string(STRING_LICENSE);
104 static char *build_args( int argc, char **argv )
106 int i, len = 1;
107 char *ret, *p;
109 for (i = 0; i < argc; i++ )
111 len += strlen(argv[i]) + 1;
112 if (strchr(argv[i], ' '))
113 len += 2;
115 ret = HeapAlloc( GetProcessHeap(), 0, len );
116 ret[0] = 0;
118 for (i = 0, p = ret; i < argc; i++ )
120 if (strchr(argv[i], ' '))
121 p += sprintf(p, " \"%s\"", argv[i]);
122 else
123 p += sprintf(p, " %s", argv[i]);
125 return ret;
128 int main(int argc, char *argv[])
130 SHELLEXECUTEINFO sei;
131 char *args;
132 int i;
134 memset(&sei, 0, sizeof(sei));
135 sei.cbSize = sizeof(sei);
136 sei.lpVerb = "open";
137 sei.nShow = SW_SHOWNORMAL;
138 /* Dunno what these mean, but it looks like winMe's start uses them */
139 sei.fMask = SEE_MASK_FLAG_DDEWAIT|
140 SEE_MASK_FLAG_NO_UI|
141 SEE_MASK_NO_CONSOLE;
143 /* Canonical Microsoft commandline flag processing:
144 * flags start with /, are case insensitive,
145 * and may be run together in same word.
147 for (i=1; i<argc; i++) {
148 int ci;
150 if (argv[i][0] != '/')
151 break;
153 /* Handle all options in this word */
154 for (ci=0; argv[i][ci]; ) {
155 /* Skip slash */
156 ci++;
157 switch(argv[i][ci]) {
158 case 'l':
159 case 'L':
160 license();
161 break; /* notreached */
162 case 'm':
163 case 'M':
164 if (argv[i][ci+1] == 'a' || argv[i][ci+1] == 'A')
165 sei.nShow = SW_SHOWMAXIMIZED;
166 else
167 sei.nShow = SW_SHOWMINIMIZED;
168 break;
169 case 'r':
170 case 'R':
171 /* sei.nShow = SW_SHOWNORMAL; */
172 break;
173 case 'w':
174 case 'W':
175 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
176 break;
177 default:
178 printf("Option '%s' not recognized\n", argv[i]+ci-1);
179 usage();
181 /* Skip to next slash */
182 while (argv[i][ci] && (argv[i][ci] != '/'))
183 ci++;
187 if (i == argc)
188 usage();
190 sei.lpFile = argv[i++];
192 args = build_args( argc - i, &argv[i] );
193 sei.lpParameters = args;
195 if (!ShellExecuteEx(&sei))
196 fatal_string_error(STRING_EXECFAIL, GetLastError());
198 HeapFree( GetProcessHeap(), 0, args );
200 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
201 DWORD exitcode;
202 DWORD waitcode;
203 waitcode = WaitForSingleObject(sei.hProcess, INFINITE);
204 if (waitcode)
205 fatal_error("WaitForSingleObject", GetLastError());
206 if (!GetExitCodeProcess(sei.hProcess, &exitcode))
207 fatal_error("GetExitCodeProcess", GetLastError());
208 /* fixme: haven't tested whether exit code works properly */
209 ExitProcess(exitcode);
212 ExitProcess(0);