Annotation of embedaddon/smartmontools/os_win32/runcmd.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Run console command and wait for user input
3: *
4: * Home page of code is: http://smartmontools.sourceforge.net
5: *
6: * Copyright (C) 2011 Christian Franke <smartmontools-support@lists.sourceforge.net>
7: *
8: * This program is free software; you can redistribute it and/or modify
9: * it under the terms of the GNU General Public License as published by
10: * the Free Software Foundation; either version 2, or (at your option)
11: * any later version.
12: *
13: * You should have received a copy of the GNU General Public License
14: * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
15: *
16: */
17:
18: char svnid[] = "$Id: runcmd.c 3453 2011-10-16 12:45:27Z chrfranke $";
19:
20: #include <stdio.h>
21: #include <windows.h>
22:
23: int main(int argc, char **argv)
24: {
25: char * cmd = GetCommandLineA();
26: DWORD exitcode;
27: STARTUPINFOA si = { sizeof(si), };
28: PROCESS_INFORMATION pi;
29: int key;
30:
31: if (*cmd == '"') {
32: cmd++;
33: while (*cmd && !(*cmd == '"' && cmd[-1] != '\\'))
34: cmd++;
35: if (*cmd)
36: cmd++;
37: }
38: else {
39: while (*cmd && !(*cmd == ' ' || *cmd == '\t'))
40: cmd++;
41: }
42:
43: while (*cmd == ' ' || *cmd == '\t')
44: cmd++;
45:
46: if (*cmd) {
47: printf("%s\n\n", cmd); fflush(stdout);
48: }
49:
50: if (!*cmd) {
51: printf("Usage: %s COMMAND [ARG ...]\n", argv[0]);
52: exitcode = 1;
53: }
54: else if (!CreateProcessA((char *)0, cmd,
55: (SECURITY_ATTRIBUTES *)0, (SECURITY_ATTRIBUTES *)0,
56: TRUE/*inherit*/, 0/*no flags*/, (void *)0, (char *)0, &si, &pi)
57: ) {
58: DWORD err = GetLastError();
59: if (err == ERROR_FILE_NOT_FOUND)
60: printf("Command not found\n");
61: else
62: printf("CreateProcess() failed with error=%u\n", err);
63: exitcode = 1;
64: }
65: else {
66: CloseHandle(pi.hThread);
67:
68: exitcode = 42;
69: WaitForSingleObject(pi.hProcess, INFINITE);
70: GetExitCodeProcess(pi.hProcess, &exitcode);
71: CloseHandle(pi.hProcess);
72:
73: if (exitcode)
74: printf("\nExitcode: %u (0x%02x)", exitcode, exitcode);
75: }
76:
77: printf("\nType <return> to exit: "); fflush(stdout);
78: while (!((key = getc(stdin)) == EOF || key == '\n' || key == '\r'))
79: ;
80: printf("\n");
81:
82: return exitcode;
83: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>