/[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.6 by joko, Thu Jun 15 22:34:55 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      
37      fgets(command, MAX_COMMAND_LENGTH, fp);
38      chomp(command);
39      
40      process_command(command);
41    }
42    
43    /* if last char is newline, "strip" it */
44    void chomp(char * string) {
45      int string_length = strlen(string);
46      if (string[string_length-1] == '\n') {
47        string[string_length-1] = '\0';
48      }
49    }
50    
51    void process_command(char * command) {
52      printf("process_command: %s\n", command);
53      start_process(command, 0);
54    }
55    
56    BOOL start_process(char * filename, BOOL background) {
57    
58    STARTUPINFO si;    STARTUPINFO si;
59    PROCESS_INFORMATION pi;    PROCESS_INFORMATION pi;
60    
61      //printf("start_process: '%s'\n", filename);
62        
63    ZeroMemory(&si,sizeof(si));    ZeroMemory(&si,sizeof(si));
64    si.cb=sizeof(STARTUPINFO);    si.cb = sizeof(STARTUPINFO);
65        
66    ZeroMemory(&pi,sizeof(pi));    ZeroMemory(&pi,sizeof(pi));
   //pi.cb=sizeof(PROCESS_INFORMATION);  
67        
68    CreateProcess(    CreateProcess(
69      NULL, "notepad.exe",      NULL, filename,
70      NULL, NULL, FALSE, 0,      NULL, NULL, FALSE, 0,
71      NULL, NULL,      NULL, NULL,
72      &si,      &si,
73      &pi      &pi
74    );    );
75      check_alert_error("CreateProcess");
76        
77    if (!background) {    if (!background) {
78      long status;      long status;
79      status = WaitForSingleObject(pi.hProcess, INFINITE);      status = WaitForSingleObject(pi.hProcess, INFINITE);
80      if (status == WAIT_FAILED) {      if (status == WAIT_FAILED) {
81        check_alert_error("WaitForSingleObject");        check_alert_error("WaitForSingleObject");
82          return FALSE;
83      }      }
84    }    }
85      
86      return TRUE;
87    
88  }  }
89    
90  check_alert_error(char *error_source[]) {  void check_alert_error(const char * error_source) {
91    DWORD dwError = GetLastError();    DWORD dwError = GetLastError();
92    
93    HLOCAL hlocal = NULL;    HLOCAL message = NULL;
94      BOOL fOk;
95      
96      if (dwError == 0)
97        return;
98    
99    BOOL fOk = FormatMessage(    fOk = FormatMessage(
100      FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,      FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
101      NULL,      NULL,
102      dwError,      dwError,
103      MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),      MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
104      (PTSTR)&hlocal,      (PTSTR)&message,
105      0,      0,
106      NULL      NULL
107    );    );
108    
109    if (hlocal != NULL) {    if (message != NULL) {
110      fprintf(stderr, "Error with '%s': %s", error_source, hlocal);      fprintf(stderr, "Error with '%s': %s (Code %i)\n", error_source, message, dwError);
111      LocalFree(hlocal);      LocalFree(message);
112    }    }
113        
114  }  }

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

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