/[cvs]/joko/Uni/BSArch/03/win32/min_shell.c
ViewVC logotype

Diff of /joko/Uni/BSArch/03/win32/min_shell.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by joko, Thu Jun 15 13:21:05 2006 UTC revision 1.4 by joko, Thu Jun 15 14:21:16 2006 UTC
# Line 5  Line 5 
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.");
# Line 17  int main(int argc, char * argv[]) { Line 22  int main(int argc, char * argv[]) {
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));
65    //pi.cb=sizeof(PROCESS_INFORMATION);    //pi.cb=sizeof(PROCESS_INFORMATION);
66        
67    CreateProcess(    CreateProcess(
68      NULL, "notepad.exe",      //"C:\windows\system32\notepad.exe", NULL,
69        //"C:\\windows\\system32\\notepad.exe", NULL,
70        NULL, filename,
71      NULL, NULL, FALSE, 0,      NULL, NULL, FALSE, 0,
72      NULL, NULL,      NULL, NULL,
73      &si,      &si,
74      &pi      &pi
75    );    );
76      check_alert_error("CreateProcess");
77        
78    if (!background) {    if (!background) {
79      long status;      long status;
80      status = WaitForSingleObject(pi.hProcess, INFINITE);      status = WaitForSingleObject(pi.hProcess, INFINITE);
81      if (status == WAIT_FAILED) {      if (status == WAIT_FAILED) {
82        check_alert_error("WaitForSingleObject");        check_alert_error("WaitForSingleObject");
83          return FALSE;
84      }      }
85    }    }
86      
87      return TRUE;
88    
89  }  }
90    
91  check_alert_error(char *error_source[]) {  void check_alert_error(const char * error_source) {
92    DWORD dwError = GetLastError();    DWORD dwError = GetLastError();
93    
94    HLOCAL hlocal = NULL;    HLOCAL message = NULL;
95      BOOL fOk;
96      
97      if (dwError == 0)
98        return;
99    
100    BOOL fOk = FormatMessage(    fOk = FormatMessage(
101      FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,      FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
102      NULL,      NULL,
103      dwError,      dwError,
104      MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),      MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
105      (PTSTR)&hlocal,      (PTSTR)&message,
106      0,      0,
107      NULL      NULL
108    );    );
109    
110    if (hlocal != NULL) {    if (message != NULL) {
111      fprintf(stderr, "Error with '%s': %s", error_source, hlocal);      fprintf(stderr, "Error with '%s': %s (Code %i)\n", error_source, message, dwError);
112      LocalFree(hlocal);      LocalFree(message);
113    }    }
114        
115  }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed