sources:
MinShell.cpp (3.0k)


binaries:
Release/MinShell.exe (17.5k)
Release/script.txt (45 bytes)


website:
more info here


screenshot:
studies/betriebssysteme/Betriebssysteme-Code2/MinShell.cpp
download file

  1 ///////////////////////////////////////////////////////////
  2 // Betriebssysteme I - Windows 2000
  3 // Lab 2: A simple command shell
  4 //
  5 // author: Stephan Brumme
  6 // last changes: November 20, 2001
  7
  8
  9 // Win32 API
 10 #include <windows.h>
 11
 12 // console output (printf etc.)
 13 #include <stdio.h>
 14
 15
 16 int main(int argc, char* argv[])
 17 {
 18     // file handle to access the shell commands
 19     FILE* fd;
 20
 21     // at most 512 characters per line
 22     const int MAX_LINE_LEN = 512;
 23     char  cmdLine[MAX_LINE_LEN+1];
 24
 25
 26     // a shell command file must be supplied
 27     if (argc > 2)
 28     {
 29         printf("usage: \"MinShell ShellCommands.txt\"\n");
 30         printf("or just \"MinShell\" if you want to type in the commands.\n");
 31
 32         // wrong parameter count
 33         return 1;
 34     }
 35
 36     if (argc == 2)
 37     {
 38         // open file
 39         fd = fopen(argv[1], "r");
 40         if (fd == NULL)
 41         {
 42             printf("Cannot find %s.\n", argv[1]);
 43             // failed
 44             return 2;
 45         }
 46     }
 47     else
 48     {
 49         // read from console
 50         fd = stdin;
 51         printf("Press [Return] to quit.\n\n");
 52     }
 53
 54
 55     // read and execute shell commands
 56     for (;;)
 57     {
 58         // prompt
 59         printf("MinShell$ ");
 60
 61         // read a single command, abort if [Return] was pressed
 62         if (fgets(cmdLine, MAX_LINE_LEN, fd) == NULL ||
 63             cmdLine[0] == 10)

 64         {
 65             printf("\n... done\n");
 66             return 0;
 67         }
 68
 69         // strip CR-LF
 70         char* pCmdLine = cmdLine;
 71         while(*pCmdLine != 0x0A &&
 72               *pCmdLine != 0x0D &&
 73               *pCmdLine != 0x00)

 74             pCmdLine++;
 75         *pCmdLine = 0x00;
 76
 77         // show shell command (if not typed in from console)
 78         if (fd != stdin)
 79             printf("%s\n", cmdLine);
 80
 81         // fill in start-up info values
 82         STARTUPINFO StartupInfo;
 83         ZeroMemory(&StartupInfo, sizeof(StartupInfo));
 84         StartupInfo.cb = sizeof(StartupInfo);
 85
 86         // CreateProcess returns some process information
 87         PROCESS_INFORMATION ProcessInfo;
 88         ZeroMemory(&ProcessInfo, sizeof(ProcessInfo));
 89
 90         if (CreateProcess(NULL, // application name is first token of the command line
 91                       cmdLine,  // contains application name, too
 92                       NULL,     // default process security settings
 93                       NULL,     // default thread security settings
 94                       FALSE,    // process DOES NOT inherit from minshell.exe
 95                       0,        // child has no access to minshell.exe
 96                       NULL,     // no special environment
 97                       NULL,     // same directory
 98                       &StartupInfo,
 99                       &ProcessInfo)
== 0)

100         {
101             // error handling
102             const int MAX_ERROR_LEN = 255;
103             char strError[MAX_ERROR_LEN+1];
104
105             DWORD nError = GetLastError();
106             FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
107                           NULL,
108                           nError,
109                           0,
110                           strError,
111                           MAX_ERROR_LEN,
112                           NULL)
;
113
114             printf("Failed to create \"%s\": (%d) %s\n", cmdLine, nError, strError);
115
116             // failed to execute a program
117             // return 3;
118         }
119
120
121         // wait until process terminates and clean up
122         WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
123         CloseHandle(ProcessInfo.hProcess);
124         CloseHandle(ProcessInfo.hThread);
125     }
126
127     // successful
128     return 0;
129 }
130