3 |
#include <stdlib.h> |
#include <stdlib.h> |
4 |
#include <stdio.h> |
#include <stdio.h> |
5 |
#include <errno.h> |
#include <errno.h> |
|
#include <windows.h> |
|
6 |
|
|
7 |
#define BOOL int |
#include "min_shell.h" |
8 |
#define TRUE 1 |
|
9 |
#define FALSE 0 |
|
10 |
|
#define MAX_COMMAND_LENGTH 1024 |
11 |
|
|
12 |
int main(int argc, char * argv[]) { |
int main(int argc, char * argv[]) { |
13 |
if (argc == 1) { |
if (argc == 1) { |
15 |
//exit(EXIT_FAILURE); |
//exit(EXIT_FAILURE); |
16 |
char filename[255] = "abc\0"; |
char filename[255] = "abc\0"; |
17 |
BOOL background = 0; |
BOOL background = 0; |
18 |
start_process(filename, background); |
os_start_process(filename, background); |
19 |
|
} |
20 |
|
|
21 |
|
if (argc == 2) { |
22 |
|
FILE * script = fopen(argv[1], "r"); |
23 |
|
run_commands(script); |
24 |
|
fclose(script); |
25 |
} |
} |
26 |
} |
} |
27 |
|
|
28 |
BOOL start_process(char *filename[], BOOL background) { |
void run_commands(FILE * fp) { |
|
|
|
|
STARTUPINFO si; |
|
|
PROCESS_INFORMATION pi; |
|
|
|
|
|
ZeroMemory(&si,sizeof(si)); |
|
|
si.cb=sizeof(STARTUPINFO); |
|
29 |
|
|
30 |
ZeroMemory(&pi,sizeof(pi)); |
char command[MAX_COMMAND_LENGTH]; |
|
//pi.cb=sizeof(PROCESS_INFORMATION); |
|
31 |
|
|
32 |
CreateProcess( |
fgets(command, MAX_COMMAND_LENGTH, fp); |
33 |
NULL, "notepad.exe", |
chomp(command); |
|
NULL, NULL, FALSE, 0, |
|
|
NULL, NULL, |
|
|
&si, |
|
|
&pi |
|
|
); |
|
34 |
|
|
35 |
if (!background) { |
execute_command(command); |
|
long status; |
|
|
status = WaitForSingleObject(pi.hProcess, INFINITE); |
|
|
if (status == WAIT_FAILED) { |
|
|
check_alert_error("WaitForSingleObject"); |
|
|
} |
|
|
} |
|
|
|
|
36 |
} |
} |
37 |
|
|
|
check_alert_error(char *error_source[]) { |
|
|
DWORD dwError = GetLastError(); |
|
|
|
|
|
HLOCAL hlocal = NULL; |
|
38 |
|
|
39 |
BOOL fOk = FormatMessage( |
void execute_command(char * command) { |
40 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
printf("process_command: %s\n", command); |
41 |
NULL, |
os_start_process(command, 0); |
|
dwError, |
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), |
|
|
(PTSTR)&hlocal, |
|
|
0, |
|
|
NULL |
|
|
); |
|
|
|
|
|
if (hlocal != NULL) { |
|
|
fprintf(stderr, "Error with '%s': %s", error_source, hlocal); |
|
|
LocalFree(hlocal); |
|
|
} |
|
|
|
|
42 |
} |
} |
43 |
|
|