/***************************************************************************

mycmd

模仿Windows的cmd.exe

(1)提供两个内部命令(不必开启子进程,在当前进程中执行), cd和dir
cd c:\
cd ..
dir

(2)对于执行可执行程序,则开启子进程来执行该程序,例如
C:\Program Files\Internet Explorer\iexplore.exe


/*实验设计:邹昌伟:google_007@sohu.com*/

****************************************************************************/



#include <windows.h>
#include <stdio.h>
#include <string.h>

#define BUFSIZE 4096
#define MAXLINE 4096


BOOL CreateChildProcess(char * szCmdline);
void processCmdline(char * szCmdline);

PROCESS_INFORMATION piProcInfo;

/******************************************************************
显示目录path下的所有文件和子目录
@param char * path 形如 e:\data\*

********************************************************************/
void dir(char * path){
HANDLE hFile;
WIN32_FIND_DATA fd;
BOOL bRet = TRUE;
bRet = TRUE;


hFile = FindFirstFile(path, &fd);
while (hFile != INVALID_HANDLE_VALUE && bRet)
{
printf("%s\n",fd.cFileName);
bRet = FindNextFile(hFile, &fd);
}
FindClose(hFile); //

}

void processCmdline(char * szCmdline){
char path[MAXLINE];
if(strncmp(szCmdline,"dir",3) == 0){
GetCurrentDirectory(MAXLINE,path);
strcat(path,"\\*");
dir(path);
}else if(strncmp(szCmdline,"cd",2) == 0){
char * ptr = szCmdline;
ptr += 2;
while(*ptr == ' ' || *ptr == '\t'){
ptr++;
}
SetCurrentDirectory(ptr);
}else{

if(CreateChildProcess(szCmdline)) {
WaitForSingleObject(piProcInfo.hProcess,INFINITE); //实验要求补全代码部分
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);

}
else
printf("出错\n");

}
printf("\n");
}

int main(){
char cmdline[MAXLINE];
char path[MAXLINE];

ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
printf("Microsoft Windows\n(C)版权所有 FJNU CSE 2013\n\n");

GetCurrentDirectory(MAXLINE,path);

printf("%s>",path);


while (fgets(cmdline, MAXLINE, stdin) != NULL) {
if (cmdline[strlen(cmdline) - 1] == '\n'){
cmdline[strlen(cmdline) - 1] = 0; /* replace newline with null */
}
//用户可能直接输入回车
if(cmdline[0] != 0){
processCmdline(cmdline);

}
GetCurrentDirectory(MAXLINE,path);
printf("%s>",path);
}

return 0;
}


BOOL CreateChildProcess(TCHAR * szCmdline){


STARTUPINFO siStartInfo;

ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);


return CreateProcess(NULL,
szCmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
}