| 5 |
#include <errno.h> |
#include <errno.h> |
| 6 |
#include <windows.h> |
#include <windows.h> |
| 7 |
|
|
| 8 |
|
#include "min_shell.h" |
| 9 |
|
|
| 10 |
#define BOOL int |
#define BOOL int |
| 11 |
#define TRUE 1 |
#define TRUE 1 |
| 12 |
#define FALSE 0 |
#define FALSE 0 |
| 13 |
|
|
| 14 |
|
|
| 15 |
|
#define MAX_COMMAND_LENGTH 1024 |
| 16 |
|
|
| 17 |
int main(int argc, char * argv[]) { |
int main(int argc, char * argv[]) { |
| 18 |
if (argc == 1) { |
if (argc == 1) { |
| 19 |
//printf("No arguments given."); |
//printf("No arguments given."); |
| 22 |
BOOL background = 0; |
BOOL background = 0; |
| 23 |
start_process(filename, background); |
start_process(filename, background); |
| 24 |
} |
} |
| 25 |
|
|
| 26 |
|
if (argc == 2) { |
| 27 |
|
FILE * script = fopen(argv[1], "r"); |
| 28 |
|
read_commands(script); |
| 29 |
|
fclose(script); |
| 30 |
|
} |
| 31 |
} |
} |
| 32 |
|
|
| 33 |
BOOL start_process(char *filename[], BOOL background) { |
void read_commands(FILE * fp) { |
| 34 |
|
|
| 35 |
|
char command[MAX_COMMAND_LENGTH]; |
| 36 |
|
int command_length; |
| 37 |
|
|
| 38 |
|
fgets(command, MAX_COMMAND_LENGTH, fp); |
| 39 |
|
command_length = strlen(command); |
| 40 |
|
|
| 41 |
|
/* if last char is newline, strip it */ |
| 42 |
|
if (command[command_length-1] == '\n') { |
| 43 |
|
command[command_length-1] = '\0'; |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
process_command(command); |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
void process_command(char * command) { |
| 50 |
|
printf("process_command: %s\n", command); |
| 51 |
|
start_process(command, 0); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
BOOL start_process(char * filename, BOOL background) { |
| 55 |
|
|
| 56 |
STARTUPINFO si; |
STARTUPINFO si; |
| 57 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
| 58 |
|
|
| 59 |
|
//printf("start_process: '%s'\n", filename); |
| 60 |
|
|
| 61 |
ZeroMemory(&si,sizeof(si)); |
ZeroMemory(&si,sizeof(si)); |
| 62 |
si.cb=sizeof(STARTUPINFO); |
si.cb = sizeof(STARTUPINFO); |
| 63 |
|
|
| 64 |
ZeroMemory(&pi,sizeof(pi)); |
ZeroMemory(&pi,sizeof(pi)); |
|
//pi.cb=sizeof(PROCESS_INFORMATION); |
|
| 65 |
|
|
| 66 |
CreateProcess( |
CreateProcess( |
| 67 |
NULL, "notepad.exe", |
NULL, filename, |
| 68 |
NULL, NULL, FALSE, 0, |
NULL, NULL, FALSE, 0, |
| 69 |
NULL, NULL, |
NULL, NULL, |
| 70 |
&si, |
&si, |
| 71 |
&pi |
&pi |
| 72 |
); |
); |
| 73 |
|
check_alert_error("CreateProcess"); |
| 74 |
|
|
| 75 |
if (!background) { |
if (!background) { |
| 76 |
long status; |
long status; |
| 77 |
status = WaitForSingleObject(pi.hProcess, INFINITE); |
status = WaitForSingleObject(pi.hProcess, INFINITE); |
| 78 |
if (status == WAIT_FAILED) { |
if (status == WAIT_FAILED) { |
| 79 |
check_alert_error("WaitForSingleObject"); |
check_alert_error("WaitForSingleObject"); |
| 80 |
|
return FALSE; |
| 81 |
} |
} |
| 82 |
} |
} |
| 83 |
|
|
| 84 |
|
return TRUE; |
| 85 |
|
|
| 86 |
} |
} |
| 87 |
|
|
| 88 |
check_alert_error(char *error_source[]) { |
void check_alert_error(const char * error_source) { |
| 89 |
DWORD dwError = GetLastError(); |
DWORD dwError = GetLastError(); |
| 90 |
|
|
| 91 |
HLOCAL hlocal = NULL; |
HLOCAL message = NULL; |
| 92 |
|
BOOL fOk; |
| 93 |
|
|
| 94 |
|
if (dwError == 0) |
| 95 |
|
return; |
| 96 |
|
|
| 97 |
BOOL fOk = FormatMessage( |
fOk = FormatMessage( |
| 98 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
| 99 |
NULL, |
NULL, |
| 100 |
dwError, |
dwError, |
| 101 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), |
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), |
| 102 |
(PTSTR)&hlocal, |
(PTSTR)&message, |
| 103 |
0, |
0, |
| 104 |
NULL |
NULL |
| 105 |
); |
); |
| 106 |
|
|
| 107 |
if (hlocal != NULL) { |
if (message != NULL) { |
| 108 |
fprintf(stderr, "Error with '%s': %s", error_source, hlocal); |
fprintf(stderr, "Error with '%s': %s (Code %i)\n", error_source, message, dwError); |
| 109 |
LocalFree(hlocal); |
LocalFree(message); |
| 110 |
} |
} |
| 111 |
|
|
| 112 |
} |
} |