資源簡介
目標:為Windows操作系統建立一個兼容Unix命令的命令接口,實現命令包括ls,cat,cp,mv,md,rd,cd,sort,more命令。
環境:使用CodeBlock開發(g++編譯)
效果:尚未發現BUG
優點:代碼可讀性高、比較精簡、錯誤處理完善,經測試,略微改動幾行代碼可在mac-lion-10.8(蘋果系統,其實也是unix)上運行
展望:其實開發命令行接口,使用getopt()等函數是比較方便的,但這是作者開發到一半的時間才知道的事情,所以未能及時更換開發方法,希望有緣人能彌補這一缺憾。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?“Command.h“
using?namespace?std;
#define?PATH_BUF?256
#define?FILE_BUF?4096
char?path[PATH_BUF]={0};
void?print_path()
{
????if?(getcwd(path?PATH_BUF))?{
????????cout?<“;
????}?else?{
????????cout?<“Error:?Cannot?get?current?path\n“;
????????exit(1);
????}
}
void?CAT(int?argc?char?*argv[])
{
????char?buf[FILE_BUF]={0};
????try?{
????????if?(1?==?argc)?{
????????????cerr?<“Error:?Do?not?specify?any?file.\n“;
????????????return;
????????}?else?if?(2?==?argc)?{
????????????if?(0?==?strcmp(argv[1]?“--help“))?{
????????????????cout?<“Usage:?cat?[--help]/[file]/[file1?file2]\n“;
????????????????cout?<“View?file?or?catenate?files.\n“;
????????????????return;
????????????}?else?{
????????????????char?*filename=argv[1];
????????????????ifstream?in(filename);
????????????????if?(!in)?throw?errno;
????????????????do{
????????????????????in.read(buf?FILE_BUF-1);
????????????????????buf[in.gcount()]=‘\0‘;
????????????????????cout?<????????????????}while(in.gcount()?>?0);
????????????????cout?<“\n“;
????????????????in.close();
????????????}
????????????return;
????????}?else?if?(3?==?argc)?{
????????????char?*source=argv[1];
????????????char?*dest=argv[2];
????????????ifstream?in(source?ifstream::binary);
????????????if?(!in)?throw?errno;
????????????ofstream?out(dest?ofstream::binary?|?ofstream::app);
????????????if?(!out)?throw?errno;
????????????do{
????????????????in.read(buf?FILE_BUF);
????????????????out.write(buf?in.gcount());
????????????}while(in.gcount()?>?0);
????????????in.close();
????????????out.close();
????????}?else?{
????????????cerr?<“Error:?Too?many?options?typed.\n“;
????????????return;
????????}
????}?catch?(int?err)?{
????????cerr?<“Error:?“?<????????return;
????}
}
void?CD(int?argc?char?*argv[])
{
????char?*dirname=NULL;
????if?(1?==?argc)?{
????????cerr?<“Error:?Do?not?specify?any?file.\n“;
????????return;
????}?else?if?(2?==?argc)?{
????????if?(0?==?strcmp(argv[1]?“--help“))?{
????????????cout?<“Usage:?cd?[--help]/[file]\n“;
????????????cout?<“Change?the?shell?working?directory.\n“;
????????????return;
????????}?else?{
????????????dirname?=?argv[1];
????????????if?((-1?==?access(dirname?0))?||?(0?!=?chdir(dirname)))
????????????????cerr?<“Error:?“?<????????????else?{
????????????????if?(!getcwd(path?PATH_BUF))
????????????????????cerr?<“Error:?Cannot?get?current?path.\n“;
????????????}
????????}
????}?else?{
????????cerr?<“Error:?Too?many?options?typed.\n“;
????????return;
????}
}
void?CP(int?argc?char?*argv[])
{
????if?(1?==?argc)?{
????????cerr?<“Error:?Do?not?specify?any?files.\n“;
????????return;
????}?else?if?(2?==?argc)?{
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-07-04?18:40??all\
?????文件????????1272??2013-06-29?16:08??all\all.cbp
?????文件?????????466??2013-07-03?21:52??all\all.depend
?????文件?????????691??2013-07-04?18:40??all\all.layout
?????目錄???????????0??2013-07-04?18:31??all\bin\
?????目錄???????????0??2013-07-04?18:40??all\bin\Debug\
?????文件?????1142601??2013-07-04?18:40??all\bin\Debug\all.exe
?????文件????????9921??2013-07-03?21:50??all\Command.cpp
?????文件?????????834??2013-07-03?21:52??all\Command.h
?????文件??????????24??2013-06-21?15:57??all\icon.rc
?????文件????????2806??2013-07-04?18:40??all\main.cpp
?????目錄???????????0??2013-07-04?18:31??all\obj\
?????目錄???????????0??2013-07-04?18:40??all\obj\Debug\
?????文件??????223566??2013-07-04?18:31??all\obj\Debug\Command.o
?????文件????????3150??2013-07-04?18:31??all\obj\Debug\icon.res
?????文件???????15053??2013-07-04?18:40??all\obj\Debug\main.o
?????文件????????2870??2013-06-21?15:55??all\UNIX.ico
- 上一篇:中英文北京地鐵線路圖全矢量圖
- 下一篇:2018年國際會議列表人工智能大類
評論
共有 條評論