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         // read from console
 49         fd = stdin;
 50         printf("Press [Return] to quit.\n\n");
 51     }
 52
 53
 54     // read and execute shell commands
 55     for (;;)
 56     {
 57         // prompt
 58         printf("MinShell$ ");
 59
 60         // read a single command, abort if [Return] was pressed
 61         if (fgets(cmdLine, MAX_LINE_LEN, fd) == NULL ||
 62             cmdLine[0] == 10)

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

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

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