Debian Codegraph @

terps/tads/tads3/win32/test/capture.cpp

raw
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /* Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. */
7 /*
8 Name
9 capture.cpp - run a child process, redirecting both stdout and stderr
10 Function
11 Runs the command line as a child process, capturing both stdout and
12 stderr to stdout. This allows redirection of both stdout and stderr to
13 a single file, with a command line this:
14
15 capture > file.txt mycmd arg1 arg2 arg3
16
17 This runs the command "mycmd arg1 arg2 arg3", capturing both stdout
18 and stderr and redirecting them to file.txt.
19
20 Although the Windows NT command shell has a mechanism for redirecting
21 stderr explicitly, the Windows 95 command shell has no such feature.
22 'capture' provides a mechanism for capturing all output that is portable
23 between 95 and NT.
24 Notes
25 This program is specific to Win32 - it is not portable to other operating
26 systems.
27 Modified
28 07/30/99 MJRoberts - Creation
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <Windows.h>
34
35 static void errexit(char *msg)
36 {
37 printf("%s\n", msg);
38 exit(2);
39 }
40
41 int main(int argc, char **argv)
42 {
43 HANDLE new_stdout;
44 STARTUPINFO sinfo;
45 PROCESS_INFORMATION pinfo;
46 DWORD exit_code;
47 char cmdline[4096];
48 char exe[MAX_PATH];
49 char cmd_dir[MAX_PATH];
50 int i;
51 DWORD result;
52
53 /* make sure we have a command to run */
54 if (argc < 2)
55 errexit("usage: capture command args...");
56
57 /* build the command line */
58 for (i = 1, cmdline[0] = '\0' ; i < argc ; ++i)
59 {
60 strcat(cmdline, argv[i]);
61 if (i + 1 < argc)
62 strcat(cmdline, " ");
63 }
64
65 /* build the executable name */
66 sprintf(exe, "%s.exe", argv[1]);
67
68 /* get the current working directory */
69 GetCurrentDirectory(sizeof(cmd_dir), cmd_dir);
70
71 /* create an inheritable duplicate of our own stdout handle */
72 if (!DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
73 GetCurrentProcess(), &new_stdout, 0, TRUE,
74 DUPLICATE_SAME_ACCESS))
75 errexit("unable to duplicate stdout");
76
77 /* set up our STARTUPINFO */
78 memset(&sinfo, 0, sizeof(sinfo));
79 sinfo.cb = sizeof(sinfo);
80 sinfo.dwFlags = STARTF_USESTDHANDLES;
81 sinfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
82 sinfo.hStdOutput = new_stdout;
83 sinfo.hStdError = new_stdout;
84
85 /* run the child program */
86 result = CreateProcess(0, (char *)cmdline, 0, 0, TRUE,
87 0, 0, cmd_dir, &sinfo, &pinfo);
88
89 /* close the thread handle */
90 if (result == 0)
91 errexit("unable to spawn process");
92
93 /* close the thread handle */
94 CloseHandle(pinfo.hThread);
95
96 /* wait until the program exits */
97 WaitForSingleObject(pinfo.hProcess, INFINITE);
98
99 /* get the child result code */
100 GetExitCodeProcess(pinfo.hProcess, &exit_code);
101
102 /* done with the process handle */
103 CloseHandle(pinfo.hProcess);
104
105 /* exit with the process's exit code */
106 return exit_code;
107 }