实验一 命令解释程序.docx
- 文档编号:9744440
- 上传时间:2023-02-06
- 格式:DOCX
- 页数:34
- 大小:358.84KB
实验一 命令解释程序.docx
《实验一 命令解释程序.docx》由会员分享,可在线阅读,更多相关《实验一 命令解释程序.docx(34页珍藏版)》请在冰豆网上搜索。
实验一命令解释程序
浙江万里学院《操作系统》实验报告
实验名称:
命令解释程序
实验时间:
2018.9.26指导教师:
詹卫华
组号:
9
学号:
2016011147姓名:
林文辉班级:
计算机164
学号:
2016011133姓名:
王旭升班级:
计算机164
一、实验目的
(1)掌握命令解释程序的设计方法。
(2)学习Windows系统调用的方法,了解目录操作、进程控制等相关知识
二、实验内容
实现一个简单的命令解释程序,具有如下特征:
(1)能够执行cd,dir,tasklist,taskkill,history,exit等内部命令
(2)能够创建前台进程和后台进程。
1.功能设计
(1)重要的数据结构设计
1.历史命令循环数组
在history命令中,用数组来存放输入过的历史命令。
假设一个能够记录12条历史记录的数组,数组的定义如下:
typedefstructENV_HISTORY{
intstart=0;//队列的头指针
intend=0;//队列的尾指针
charhis_cmd[12][100];//队列数组(顺序结构的队列)
}ENV_HISTORY;
ENV_HISTORYenvhis;//定义队列变量(为队列分配内存空间)
2.文件信息链表
在实验中中,我们需要把dir命令取得的文件信息用链表保存,输出这些信息时对链表遍历。
链表结点的定义如下:
structfiles_Content
{
FILETIMEtime;//文件创建时间
charname[200];//文件名
inttype;//普通文件,目录标识,其中1为普通文件,0为目录
intsize;//文件大小
files_Content*next;//构成链表的链接指针
}files_Conten;;
(2)主要函数或接口设计
主要函数
cd命令处理函数
voidcd_cmd(char*dir)
dir命令处理函数
voiddir_cmd(char*dir)
时间处理函数
voidftime(FILETIMEfiletime)
获得系统当前进程列表
voidGetProcessList()
获得最近输入的命令
voidhistory_cmd()
将输入命令行添加到命令历史中
voidadd_history(char*)
创建进程
HANDLEprocess(int,char[])
kill进程
BOOLkillProcess(char*)
回调函数
OOLWINAPIConsoleHandler(DWORDCEvent)
显示帮助信息
voidhelp()
接口设计
1.GetCurrentDirectory函数
功能:
查找当前进程的当前目录,调用成功返回装载到lpBuffer的字节数。
格式:
DWORDGetCurrentDirectory(
DWORDnBufferLength,
LPTSTRlpBuffer
)
2.WaitForSingleObject函数
功能:
等待一个事件信号直至信号出现或者超时。
若等到信号则返回WAIT_OBJECT_0,若等待超过dwMiliseconds时间还是无信号,则返回WAIT_TIMEOUT
格式:
DWORDWaitForSingleObject (
HANDLEhHandle,
DWORDdwMilliseconds
)
3.SetCurrentDirectory函数
功能:
设置当前目录。
返回非0表示成功,返回0表示失败。
格式:
BOOLSetCurrentDirectory (
LPCTSTRlpPathName
)
4.FindFirstFile函数
功能:
用于从一个文件夹(包括子文件夹)中搜索指定文件。
格式:
HANDLE FindFirstFile (
LPCTSTR lpFileName,
LPWIN32_FIND_DATAlpFindFileData
)
5.FindNextFile函数
功能:
继续查找FindFirstFile函数搜索后的文件。
格式:
HANDLE FindNextFile (
HANDLE hFindFile,
LPWIN32_FIND_DATAlpFindFileData
)
6.GetVolumeInformation函数
功能:
用于获取磁盘相关信息。
格式:
BOOL GetVolumeInformation (
LPCTSTR lpRootPathName,
LPCTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPWORDlpVolumeSerialNumber,
LPWORDlpMaximunComponentLength,
LPWORDlpFileSystemFlags,
LPCTSTRlpFileSystemNameBuffer,
DWORDnFileSystemNameSize
)
7.GetDiskFreeSppaceEx函数
功能:
获取与一个磁盘的组织以及剩余容量有关的信息
格式:
HANDLEGetDiskFreeSppaceEx(
LPCTSTR lpRootPathName,
PULARGE_INTEGERlpFreeBytesAvailableToCaller,
PULARGE_INTEGERlpTotalNumberOfBytes,
PULARGE_INTEGERlpTotalNumberOfFreeBytes
)
8.FileTimeToLocalFileTime函数
功能:
将一个FILETIME结构转换成本地时间。
格式:
BOOL FileTimeToLocalFileTime (
constFILETIME*lpFileTime,
LPFILETIME lpLocalFileTime
)
9.FileTimeToSystemTime函数
功能:
根据一个FILETIME结构的内容,装载一个SYSTENTIME结构。
格式:
BOOL FileTimeToSystemTime (
constFILETIME*lpFileTime,
LPFILETIME lpSystemTime
)
10.CreateToolhelp32Snapshot函数
功能:
为指定的进程、进程使用的堆(heap)、模块(module)、线程(thread)建立一个快照(snapshot)。
快照建立成功则返回快照的句柄,失败则返回INVALID_HANDL_VALUE。
格式:
HANDLEWINAPI CreateToolhelp32Snapshot (
DWORDdwFlags,
DWORDth32ProcessID
)
11.Process32First函数
功能:
是一个进程获取函数,当使用CreateToolhelp32Snapshot()函数获得当前运行进程的快照后,可以使用Process32First ()函数获得第一个进程的句柄。
格式:
BOOLWINAPI Process32First (
HANDLEhSnapshot,
LPPROCESSENTRY32lppe
)
12.Process32Next函数
功能:
获取快照中下一个进程信息。
格式:
BOOLWINAPI Process32Next(
HANDLEhSnapshot,
LPPROCESSENTRY32lppe
)
13.OpenProcess函数
功能:
该函数打开一个已经存在的进程对象,若成功,返回值是指定进程的打开句柄。
若失败,则返回空。
格式:
HANDLE OpenProcesst(
DWORD dwDesiredAccess,
BOOLbInheritHandle,
DWORDdwProcessID
)
14.SetConsoleCtrlHandler函数
功能:
添加或删除一个事件钩子(Handler)。
格式:
BOOL SetConsoleCtrlHandler (
PHANDLER_ROUTINEHandlerRoutine,
BOOLAdd
)
15.CreateProcess函数
功能:
创建一个新进程和它的主线程,这个新进程运行指定的可执行文件。
格式:
BOOL CreateProcess(
LPCTSTRlpApplicationName,
LPCTSTRlpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOLbInheritHandles,
DWORDdwCreationFalgs,
LPVOID lpEnvironment,
LPCTSTRlpCurrentDirectory,
LPSTARTUPINFOlpStartupInfo,
LPPROCESS_INFORMATIONlpProcessInformation
)
16.GetExitCodeProcess函数
功能:
获取一个已中断进程的退出代码。
格式:
BOOL GetExitCodeProcess (
HANDLEhProcess,
LPDWORDlpExitCode
)
17.TerminateProcess函数
功能:
以给定的退出码终止进程。
格式:
BOOL TerminateProcee(
HANDLEhProcess,
UINTuExitCode
)
2.源程序
WinShell.c
//WinShell.cpp:
定义控制台应用程序的入口点。
//
#define_WIN32_WINNT0x0501/*WindowsAPI*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"WinShell.h"
/**************************时间处理函数**************************/
voidftime(FILETIMEfiletime)
{
SYSTEMTIMEsystemtime;
if(filetime.dwLowDateTime==-1)
wprintf(L"NeverExpires");
else
{
if(FileTimeToLocalFileTime(&filetime,&filetime)!
=0)
{
if(FileTimeToSystemTime(&filetime,&systemtime)!
=0)
{
charstr[50];
wsprintf(str,"%d-%02d-%02d%02d:
%02d",systemtime.wYear,systemtime.wMonth,systemtime.wDay,systemtime.wHour,systemtime.wMinute);
printf("%s",str);
}
else
{
wprintf(L"FileTimeToSystemTimefailed");
}
}
else
{
wprintf(L"FileTimeToLocalFileTimefailed");
}
}
}
/**************************回调函数**************************/
BOOLWINAPIConsoleHandler(DWORDCEvent)
{
switch(CEvent)
{
caseCTRL_C_EVENT:
break;
caseCTRL_BREAK_EVENT:
break;
caseCTRL_CLOSE_EVENT:
break;
caseCTRL_LOGOFF_EVENT:
break;
caseCTRL_SHUTDOWN_EVENT:
break;
}
returnTRUE;
}
intmain()
{
/**************************函数声明**************************/
voidcd_cmd(char*dir);
voiddir_cmd(char*dir);
// voidftime(FILETIMEfiletime);
voidGetProcessList();
voidhistory_cmd();
voidadd_history(char*inputcmd);
HANDLEprocess(intbg,charappName[]);
BOOLkillProcess(char*pid);
// BOOLWINAPIConsoleHandler(DWORDCEvent);
voidhelp();
charc,*input,*arg[2],path[BUFSIZE];
intinput_len=0,is_bg=0,i,j,k;
HANDLEhprocess;
DWORDdwRet;
while
(1)
{
/*指针数组初始化*/
for(i=0;i<2;i++)
arg[i]=NULL;
/*获得当前目录,返回地址存入pathBUFSIZE为最多能够保存的地址长度*/
dwRet=GetCurrentDirectory(BUFSIZE,path);
if(dwRet==0)
{
/*返回当前目录失败*/
printf("GetCurrentDirectoryfailed(%d)\n",GetLastError());
}
elseif(dwRet>BUFSIZE)
{
/*长度小于返回地址长度,输出需要多少长度*/
printf("GetCurrentDirectoryfailed(buffertoosmall;need%dchars)\n",dwRet);
}
else
{
/*输出当前目录*/
printf("%s>",path);
}
/**************************输入**************************/
input_len=0;
/*过滤无用字符*/
while((c=getchar())==''||c=='/t'||c==EOF)
if(c=='\n')
continue;
while(c!
='\n')
{
buf[input_len++]=c;
c=getchar();
}
buf[input_len++]='\0';
/*分配动态存储空间,将命令从缓存复制到input*/
input=(char*)malloc(sizeof(char)*(input_len));
strcpy(input,buf);
/**************************解析指令**************************/
for(i=0,j=0,k=0;i { if(input[i]==''||input[i]=='\0') { if(j==0) continue; else { buf[j++]='\0'; arg[k]=(char*)malloc(sizeof(char)*j); strcpy(arg[k++],buf); j=0; } } else { buf[j++]=input[i]; } } /**************************内部命令处理**************************/ /*cd*/ if(strcmp(arg[0],"cd")==0){ add_history(input); for(i=3,j=0;i<=input_len;i++) buf[j++]=input[i]; buf[j]='\0'; arg[1]=(char*)malloc(sizeof(char)*j); strcpy(arg[1],buf); cd_cmd(arg[1]); free(input); continue; } /*dir*/ if(strcmp(arg[0],"dir")==0){ char*route; add_history(input); if(arg[1]==NULL) { route=path; dir_cmd(route); } else dir_cmd(arg[1]); free(input); continue; } /*tasklst*/ if(strcmp(arg[0],"tasklist")==0){ add_history(input); GetProcessList(); free(input); continue; } /*前台进程*/ if(strcmp(arg[0],"fp")==0){ add_history(input); if(arg[1]==NULL) { printf("没有指定可执行文件\n"); free(input); continue; } is_bg=0; hprocess=process(is_bg,arg[1]); if(WaitForSingleObject(hprocess,INFINITE)==WAIT_OBJECT_0) free(input); continue; } /*后台进程*/ if(strcmp(arg[0],"bg&")==0){ add_history(input); if(arg[1]==NULL) { printf("没有指定可执行文件\n"); free(input); continue; } is_bg=1; process(is_bg,arg[1]); free(input); continue; } /*kill进程*/ if(strcmp(arg[0],"taskkill")==0){
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验一 命令解释程序 实验 命令 解释程序