/[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.1 by joko, Thu Jun 15 12:22:56 2006 UTC revision 1.5 by joko, Thu Jun 15 22:15:10 2006 UTC
# Line 1  Line 1 
1  /* $Id$ */  /* $Id$ */
2    
3  #include <stdlib.h>  #include <stdlib.h>
4    #include <stdio.h>
5  #include <errno.h>  #include <errno.h>
6    #include <windows.h>
7    
8    #include "min_shell.h"
9    
10    #define BOOL int
11    #define TRUE 1
12    #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.");
20      exit(EXIT_FAILURE);      //exit(EXIT_FAILURE);
21        char filename[255] = "abc\0";
22        BOOL background = 0;
23        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    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;
57      PROCESS_INFORMATION pi;
58    
59      //printf("start_process: '%s'\n", filename);
60      
61      ZeroMemory(&si,sizeof(si));
62      si.cb = sizeof(STARTUPINFO);
63      
64      ZeroMemory(&pi,sizeof(pi));
65      
66      CreateProcess(
67        NULL, filename,
68        NULL, NULL, FALSE, 0,
69        NULL, NULL,
70        &si,
71        &pi
72      );
73      check_alert_error("CreateProcess");
74      
75      if (!background) {
76        long status;
77        status = WaitForSingleObject(pi.hProcess, INFINITE);
78        if (status == WAIT_FAILED) {
79          check_alert_error("WaitForSingleObject");
80          return FALSE;
81        }
82      }
83      
84      return TRUE;
85    
86    }
87    
88    void check_alert_error(const char * error_source) {
89      DWORD dwError = GetLastError();
90    
91      HLOCAL message = NULL;
92      BOOL fOk;
93      
94      if (dwError == 0)
95        return;
96    
97      fOk = FormatMessage(
98        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
99        NULL,
100        dwError,
101        MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
102        (PTSTR)&message,
103        0,
104        NULL
105      );
106    
107      if (message != NULL) {
108        fprintf(stderr, "Error with '%s': %s (Code %i)\n", error_source, message, dwError);
109        LocalFree(message);
110    }    }
111      
112  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

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