]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - os_win32/runcmd.c
import smartmontools 7.0
[mirror_smartmontools-debian.git] / os_win32 / runcmd.c
1 /*
2 * Run console command and wait for user input
3 *
4 * Home page of code is: http://www.smartmontools.org
5 *
6 * Copyright (C) 2011 Christian Franke
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 char svnid[] = "$Id: runcmd.c 4760 2018-08-19 18:45:53Z chrfranke $";
12
13 #include <stdio.h>
14 #include <windows.h>
15
16 int main(int argc, char **argv)
17 {
18 char * cmd = GetCommandLineA();
19 DWORD exitcode;
20 STARTUPINFOA si = { sizeof(si), };
21 PROCESS_INFORMATION pi;
22 int key;
23
24 if (*cmd == '"') {
25 cmd++;
26 while (*cmd && !(*cmd == '"' && cmd[-1] != '\\'))
27 cmd++;
28 if (*cmd)
29 cmd++;
30 }
31 else {
32 while (*cmd && !(*cmd == ' ' || *cmd == '\t'))
33 cmd++;
34 }
35
36 while (*cmd == ' ' || *cmd == '\t')
37 cmd++;
38
39 if (*cmd) {
40 printf("%s\n\n", cmd); fflush(stdout);
41 }
42
43 if (!*cmd) {
44 printf("Usage: %s COMMAND [ARG ...]\n", argv[0]);
45 exitcode = 1;
46 }
47 else if (!CreateProcessA((char *)0, cmd,
48 (SECURITY_ATTRIBUTES *)0, (SECURITY_ATTRIBUTES *)0,
49 TRUE/*inherit*/, 0/*no flags*/, (void *)0, (char *)0, &si, &pi)
50 ) {
51 DWORD err = GetLastError();
52 if (err == ERROR_FILE_NOT_FOUND)
53 printf("Command not found\n");
54 else
55 printf("CreateProcess() failed with error=%u\n", err);
56 exitcode = 1;
57 }
58 else {
59 CloseHandle(pi.hThread);
60
61 exitcode = 42;
62 WaitForSingleObject(pi.hProcess, INFINITE);
63 GetExitCodeProcess(pi.hProcess, &exitcode);
64 CloseHandle(pi.hProcess);
65
66 if (exitcode)
67 printf("\nExitcode: %u (0x%02x)", exitcode, exitcode);
68 }
69
70 printf("\nType <return> to exit: "); fflush(stdout);
71 while (!((key = getc(stdin)) == EOF || key == '\n' || key == '\r'))
72 ;
73 printf("\n");
74
75 return exitcode;
76 }