10 HANDLE InputHandle
, OutputHandle
;
14 void debug_printf(char* fmt
, ...)
20 vsprintf(buffer
,fmt
,args
);
22 WriteConsoleA(OutputHandle
, buffer
, strlen(buffer
), NULL
, NULL
);
29 "Reactos Simple Shell\n(compiled on %s, at %s)\n",
35 void ExecuteCd(char* cmdline
)
37 if (!SetCurrentDirectoryA(cmdline
))
39 debug_printf("Invalid directory\n");
43 void ExecuteDir(char* cmdline
)
46 WIN32_FIND_DATA FindData
;
51 shandle
= FindFirstFile("*",&FindData
);
53 if (shandle
==INVALID_HANDLE_VALUE
)
55 debug_printf("Invalid directory\n");
60 debug_printf("%-15.15s",FindData
.cAlternateFileName
);
61 if(FindData
.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY
)
62 debug_printf("<DIR> "),nRep
++;
64 debug_printf(" %10d ",FindData
.nFileSizeLow
),nFile
++;
66 FileTimeToLocalFileTime(&FindData
.ftLastWriteTime
,&fTime
);
67 FileTimeToSystemTime(&fTime
, &sTime
);
68 debug_printf("%02d/%02d/%04d %02d:%02d:%02d "
69 ,sTime
.wMonth
,sTime
.wDay
,sTime
.wYear
70 ,sTime
.wHour
,sTime
.wMinute
,sTime
.wSecond
);
72 debug_printf("%s\n",FindData
.cFileName
);
73 } while(FindNextFile(shandle
,&FindData
));
74 debug_printf("\n %d files\n %d directories\n\n",nFile
,nRep
);
79 void ExecuteReboot(char* cmdline
)
81 NtShutdownSystem (ShutdownReboot
);
85 void ExecuteType(char* cmdline
)
91 FileHandle
= CreateFile(cmdline
,
98 if (FileHandle
== NULL
)
100 debug_printf("Unknown file\n");
103 while (ReadFile(FileHandle
,
109 debug_printf("%c",c
);
112 CloseHandle(FileHandle
);
115 int ExecuteProcess(char* name
, char* cmdline
, BOOL detached
)
117 PROCESS_INFORMATION ProcessInformation
;
118 STARTUPINFO StartupInfo
;
121 memset(&StartupInfo
, 0, sizeof(StartupInfo
));
122 StartupInfo
.cb
= sizeof (STARTUPINFO
);
123 StartupInfo
.lpTitle
= name
;
125 ret
= CreateProcessA(name
,
139 if (TRUE
== detached
)
143 debug_printf("%s detached:\n"
144 "\thProcess = %08X\n"
149 ProcessInformation
.hProcess
,
150 ProcessInformation
.hThread
,
151 ProcessInformation
.dwProcessId
,
152 ProcessInformation
.dwThreadId
);
153 CloseHandle(ProcessInformation
.hProcess
);
154 CloseHandle(ProcessInformation
.hThread
);
158 debug_printf("Could not detach %s\n", name
);
165 debug_printf("ProcessInformation.hThread %x\n",
166 ProcessInformation
.hThread
);
167 WaitForSingleObject(ProcessInformation
.hProcess
, INFINITE
);
168 CloseHandle(ProcessInformation
.hProcess
);
169 debug_printf("Thandle %x\n", ProcessInformation
.hThread
);
170 CloseHandle(ProcessInformation
.hThread
);
176 void ExecuteStart(char* CommandLine
)
178 char *ImageName
= CommandLine
;
182 && (*CommandLine
!= ' ')
183 && (*CommandLine
!= '\t')
187 *CommandLine
++ = '\0';
188 while ( (*CommandLine
)
189 && ( (*CommandLine
== ' ')
190 || (*CommandLine
== '\t')
202 ExecuteKill(char * lpPid
)
207 dwProcessId
= (DWORD
) atol(lpPid
);
208 debug_printf("dwProcessId %d\n",dwProcessId
);
209 hProcess
= OpenProcess(
214 if (NULL
== hProcess
)
217 "Could not open the process with PID = %d\n",
222 if (FALSE
== TerminateProcess(
228 "Could not terminate the process with PID = %d\n",
232 CloseHandle(hProcess
);
236 void ExecuteCommand(char* line
)
241 if (isalpha(line
[0]) && line
[1] == ':' && line
[2] == 0)
244 char szPath
[MAX_PATH
];
247 /* save curent directory in environment variable */
248 GetCurrentDirectory (MAX_PATH
, szPath
);
250 strcpy (szVar
, "=A:");
251 szVar
[1] = toupper (szPath
[0]);
252 SetEnvironmentVariable (szVar
, szPath
);
254 /* check for current directory of new drive */
255 strcpy (szVar
, "=A:");
256 szVar
[1] = toupper (line
[0]);
257 if (GetEnvironmentVariable (szVar
, szPath
, MAX_PATH
) == 0)
259 /* no environment variable found */
260 strcpy (szPath
, "A:\\");
261 szPath
[0] = toupper (line
[0]);
264 /* set new current directory */
265 SetCurrentDirectory (szPath
);
266 GetCurrentDirectory (MAX_PATH
, szPath
);
267 if (szPath
[0] != (char)toupper (line
[0]))
268 debug_printf("Invalid drive\n");
271 line
[0] = toupper (line
[0]);
274 if (SetCurrentDirectoryA(line
) == FALSE
)
275 debug_printf("Invalid drive\n");
282 while ((*tail
)!=' ' && (*tail
)!=0)
294 if (cmd
==NULL
|| *cmd
== '\0' )
298 if (strcmp(cmd
,"cd")==0)
303 if (strcmp(cmd
,"dir")==0)
308 if (strcmp(cmd
,"kill")==0)
313 if (strcmp(cmd
,"reboot")==0)
318 if (strcmp(cmd
,"type")==0)
323 if (strcmp(cmd
,"ver")==0)
328 if (strcmp(cmd
,"validate")==0)
330 debug_printf("Validating heap...");
331 if (HeapValidate(GetProcessHeap(),0,NULL
))
333 debug_printf("succeeded\n");
337 debug_printf("failed\n");
341 if (strcmp(cmd
,"start") == 0)
346 if (strcmp(cmd
,"exit")==0)
348 if (bCanExit
== TRUE
)
354 if (ExecuteProcess(cmd
,tail
,FALSE
))
358 debug_printf("Unknown command\n");
361 void ReadLine(char* line
)
364 UCHAR CurrentDir
[255];
368 GetCurrentDirectoryA(255,CurrentDir
);
369 debug_printf("%s>", CurrentDir
);
373 if (!ReadConsoleA(InputHandle
,
379 debug_printf("Failed to read from console\n");
387 debug_printf("\b \b");
394 debug_printf("%c", ch
);
399 } while (ch
!= '\n');
404 void ParseCommandLine (void)
406 char *pszCommandLine
;
408 pszCommandLine
= GetCommandLineA ();
409 _strupr (pszCommandLine
);
410 if (strstr (pszCommandLine
, "/P") != NULL
)
412 debug_printf("Permanent shell\n");
419 static char line
[255];
422 InputHandle
= GetStdHandle(STD_INPUT_HANDLE
);
423 OutputHandle
= GetStdHandle(STD_OUTPUT_HANDLE
);
425 debug_printf("Shell Starting...\n");
428 // SetCurrentDirectoryA("C:\\");
433 ExecuteCommand(line
);