diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 17:14:45 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 17:14:45 +0000 |
commit | 43e8530e93493bb978c446a2023134bdd4277e50 (patch) | |
tree | e8c0d3c0c394b17381f48fb2d288f166b4f22440 /os_win32/runcmd.c | |
parent | Initial commit. (diff) | |
download | smartmontools-43e8530e93493bb978c446a2023134bdd4277e50.tar.xz smartmontools-43e8530e93493bb978c446a2023134bdd4277e50.zip |
Adding upstream version 7.4.upstream/7.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'os_win32/runcmd.c')
-rw-r--r-- | os_win32/runcmd.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/os_win32/runcmd.c b/os_win32/runcmd.c new file mode 100644 index 0000000..0261717 --- /dev/null +++ b/os_win32/runcmd.c @@ -0,0 +1,76 @@ +/* + * Run console command and wait for user input + * + * Home page of code is: http://www.smartmontools.org + * + * Copyright (C) 2011 Christian Franke + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +char svnid[] = "$Id: runcmd.c 4760 2018-08-19 18:45:53Z chrfranke $"; + +#include <stdio.h> +#include <windows.h> + +int main(int argc, char **argv) +{ + char * cmd = GetCommandLineA(); + DWORD exitcode; + STARTUPINFOA si = { sizeof(si), }; + PROCESS_INFORMATION pi; + int key; + + if (*cmd == '"') { + cmd++; + while (*cmd && !(*cmd == '"' && cmd[-1] != '\\')) + cmd++; + if (*cmd) + cmd++; + } + else { + while (*cmd && !(*cmd == ' ' || *cmd == '\t')) + cmd++; + } + + while (*cmd == ' ' || *cmd == '\t') + cmd++; + + if (*cmd) { + printf("%s\n\n", cmd); fflush(stdout); + } + + if (!*cmd) { + printf("Usage: %s COMMAND [ARG ...]\n", argv[0]); + exitcode = 1; + } + else if (!CreateProcessA((char *)0, cmd, + (SECURITY_ATTRIBUTES *)0, (SECURITY_ATTRIBUTES *)0, + TRUE/*inherit*/, 0/*no flags*/, (void *)0, (char *)0, &si, &pi) + ) { + DWORD err = GetLastError(); + if (err == ERROR_FILE_NOT_FOUND) + printf("Command not found\n"); + else + printf("CreateProcess() failed with error=%u\n", err); + exitcode = 1; + } + else { + CloseHandle(pi.hThread); + + exitcode = 42; + WaitForSingleObject(pi.hProcess, INFINITE); + GetExitCodeProcess(pi.hProcess, &exitcode); + CloseHandle(pi.hProcess); + + if (exitcode) + printf("\nExitcode: %u (0x%02x)", exitcode, exitcode); + } + + printf("\nType <return> to exit: "); fflush(stdout); + while (!((key = getc(stdin)) == EOF || key == '\n' || key == '\r')) + ; + printf("\n"); + + return exitcode; +} |