实现了四个简单的功能pwd rm ls copy
#include<iostream>
#include<cstring>//标准C++函数库,主要用于字符串处理
#include<sys/types.h>//基本系统数据类型
#include<sys/stat.h>//文件状态
#include<dirent.h>//文件操作函数
#include<stdlib.h>//标准库函数
#include<fcntl.h>//文件控制
#include<time.h>//定义关于时间的函数
#include<queue>//队列的定义
#include<ftw.h>//文件树漫游
#include<unistd.h>//UNIX系统服务的函数
#include <pwd.h>
#include<stdio.h>
#include<string.h>
#include <fstream>
using namespace std;
void printpath();
char *inode_to_name(int);
int getinode(char *);
char p[200];
void pwd();
int rm(std::string);
void d_ls( const char *);
int readWriteFile(string,string);
int main(int argc, char *argv[])
{
cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
cout<<">>>>>>Welcome to Myshell<<<<<<"<<endl;
cout<<"You can use the commands as follows:"<<endl;
cout<<"1. pwd "<<endl;
cout<<"2. rm <dirname> "<<endl;
cout<<"3. ls"<<endl;
cout<<"4. copy"<<endl;
cout<<"5. exit"<<endl;
cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
string str;
while(str != "exit"){
cout<<"123@";
cin>>str; //enter the command
if(str == "pwd"){
printpath();
putchar('\n');
chdir(p);
memset(p,0,200);
}
if(str == "rm") {
string a;
cin>>a;
rm(a);
}
if(str == "pwd1") {
pwd();
}
if(str == "ls") {
char a[100];
cout<<"请输入目录:";
cin>>a;
d_ls(a);
}
if(str == "copy") {
string a,b;
cout<<"Original document :";
cin>>a;
cout<<"new document :";
cin>>b;
readWriteFile(a,b);
}
}
}
//readwritefile
int readWriteFile(string srcFile, string dstFile)
{
ifstream in(srcFile.c_str());
if (!in.is_open())
{
cout << "open src File Error opening file" << endl;
return -1;
}
ofstream out(dstFile.c_str());
if(!out.is_open())
{
cout << "open dst File Error opening file" << endl;
return -1;
}
char* buf = new char[5120];
while (!in.eof())
{
in.read(buf, 5120);
cout << buf << endl;
out.write(buf, 5120);
}
in.close();
out.close();
return 0;
}
//ls
void d_ls( const char *Dir){
DIR *dir;
struct dirent *rent;
if(dir =opendir(Dir))
{
while((rent=readdir(dir))!=NULL)
{
cout<<rent->d_name<<endl;
}
closedir(dir);
}
else
cout<<"ls: 无法访问: 没有那个文件或目录\n";
}
void pwd()
{
char ptr[80];
getcwd(ptr,sizeof(ptr));
cout<<ptr<<endl;
}
//rm 删除
int rm(std::string file_name)
{
std::string file_path = file_name;
struct stat st;
if(lstat(file_path.c_str(),&st) == -1)
{
return -1;
}
if(S_ISREG(st.st_mode))
{
if(unlink(file_path.c_str()) == -1)
{
return -1;
}
}
else if(S_ISDIR(st.st_mode))
{
if(file_name == "." || file_name == "..")
{
return -1;
}
}
return 0;
}
//pwd
void printpath()
{
int inode,up_inode;
char *str;
char abc[]=".";
char aaa[]="..";
inode = getinode(abc);
up_inode = getinode(aaa);
chdir("..");
str = inode_to_name(inode);
if(inode == up_inode) {
return;
}
printpath();
printf("/%s",str);
strcat(p,"/");
strcat(p,str);
}
int getinode(char *str)
{
struct stat st;
stat(str,&st);
return st.st_ino;
}
char *inode_to_name(int inode)
{
char *str;
DIR *dirp;
struct dirent *dirt;
dirp = opendir(".");
while((dirt = readdir(dirp)) != NULL)
{
if(dirt->d_ino == inode){
str = (char *)malloc(strlen(dirt->d_name)*sizeof(char));
strcpy(str,dirt->d_name);
return str;
}
}
}