模拟实现一个简单的shell程序,实现一些简单的命令

功能:
/////我把这个程序说明说发到另外一篇博客////////////////////
1、显示当前所在目录的路径名(指令pwd)
2、列出指定目录名中的所有目录及文件(指令list)
3、改变当前工作目录(指令chadir)
4、新建目录(指令makedir)
5、删除目录(指令deldir)
6、退出命令解释程序(指令exit)
7、重命名一个文件或目录(指令rename)
8、复制一个已存在的文件(指令copy)
9、在指定的目录及其子目录中查找指定的文件,并输出查找到的文件的绝对路径(指令find)
10、输入指令错误时会提示,并可以重新输入

下面的代码可以直接复制运行,笔者目前还未发现bug,每个功能封装在一个函数中,可以单个取出使用运行参考慢慢理解;

**用到的系统函数:**open(),close(),read(),write(),chdir(),opendir(),readdir(),closedir(),rmdir(),mkdir(),getcwd();

代码如下(尾端有详细一点的代码解析,不懂可以评论或私聊):

#include"stdio.h"
#include"string.h"
#include"unistd.h"
#include"string.h"
#include"sys/types.h"
#include"sys/stat.h"
#include"stdlib.h"
#include"dirent.h"
#include"stddef.h"
#include"fcntl.h"
///////////////////////////////////////////////////////
void pwd_();
void makedir_();
void deldir_();
void exit_();
void chadir_();
void list_();
void rename_();
void copy_();
void find_();
void error_();
////////////////////////////////////////////////////
#define buffsize 1024

int main(void)
{
	char command[30];
while(1)
{	
	printf("Liangfenkai@");
	scanf("%s",command);
	fflush(stdin);
	if(strcmp(command,"pwd")==0)	
	{
	pwd_();
	}
	else if(strcmp(command,"makedir")==0)  
	{
	makedir_();
	}
	else if(strcmp(command,"deldir")==0) 	
	{
	deldir_();
	}
	else if(strcmp(command,"exit")==0)
	{
	exit_();
	}
	else if(strcmp(command,"chadir")==0) 
	{
	chadir_();
	}
	else if((strcmp(command,"list")==0))
	{
	list_();
	}
	else if(strcmp(command,"rename")==0)	
	{
	rename_();
	}
	else if(strcmp(command,"copy")==0)	
	{
	copy_();
	}
	else if(strcmp(command,"find")==0)	
	{
	find_();
	}
	else 		
	error_();
}
return 0;
}

//打印当前觉得路径//////////////////////////////////////////////////////////////////////////
void pwd_()
{
     char *path=NULL;
	 path=getcwd(NULL,0);
     puts(path);
     free(path);
}
//创建文件夹/////////////////////////////////////////////////////////////////////////
void makedir_()
{
	char command1[30];
	scanf("%s",command1);
    fflush(stdin);
    if(mkdir(command1,0755)!=0)     
    printf("make dir default!\n");
}
//删除文件夹////////////////////////////////////////////////////////////////////////
void deldir_()
{
	char command2[30];
	scanf("%s",command2);
    fflush(stdin);
    if(rmdir(command2)!=0)
    printf("delete dir default!\n");
}
//退出程序//////////////////////////////////////////////////////////////////////////
void exit_()
{
	exit(0);
}
//改变目录路径///////////////////////////////////////////////////////////////////////
void chadir_()
{
	char command3[50];
    scanf("%s",command3);
    fflush(stdin);
    if(chdir(command3)!=0)
    {
              printf("change dir path default!\n");
    }
}
//列出指定目录名中的所有目录及文件///////////////////////////////////////////////////////////////////////
void list_()
{
	char command4[50];
	struct dirent *entry;
	DIR *olist=NULL;
	int i=1;
	scanf("%s",command4);
    fflush(stdin);
    if((olist=opendir(command4))==NULL)
    {
        printf("opendir  default!\n");
    }
    while(entry=readdir(olist))
    {
        printf("file%d:%s\n",i,entry->d_name);
        i++;
     }
     i=1;
     if(closedir(olist)!=0)
     printf("closedir  default!\n");
}
//重命名一个文件或目录///////////////////////////////////////////////////////////
void rename_()
{
	char newname[30],oldname[30];
	 printf("pleace input the old name:");
     scanf("%s",oldname);
     fflush(stdin);
     printf("pleace input the new name:");
     scanf("%s",newname);
     fflush(stdin);
     if(rename(oldname,newname)!=0)
     {
       printf("change name default!");
     }
}
//复制一个已存在的文件///////////////////////////////////////////////////////////////////////////
void copy_()
{
	char cp1[30],cp2[30],buf[buffsize];
	int fd1,fd2;
	int n;
	printf("pleace input the file be copy:\n");
    scanf("%s",cp1);
    fflush(stdin);
    printf("pleace input the new file name:\n");
    scanf("%s",cp2);
    if((fd1=open(cp1,O_RDWR|O_CREAT,0664))==-1)
        printf("open the file false!\n");
    if((n=read(fd1,buf,buffsize))==-1)
        printf("read the file false!\n");
    if((fd2=open(cp2,O_RDWR|O_CREAT,0664))==-1)
        printf("open the file false!\n");
    if(write(fd2,buf,n)==-1)
        printf("open the file false!\n");
     if(close(fd1)==-1)
        printf("close false!\n");
     if(close(fd2)==-1)
        printf("close false!\n");
}
//在指定的目录及其子目录中查找指定的文件,并输出查找到的文件的绝对路径///////////////
void find_()
{
	char *path1;
    char str1[30],str2[30];
    struct dirent *re;
	int j=0;
	DIR *dir;
	scanf("%s",str1);
    fflush(stdin);
    scanf("%s",str2);
    fflush(stdin);
    if((dir=opendir(str1))==NULL)
       printf("open dir error!\n");
    while((re=readdir(dir))!=NULL)
    {
       if(strcmp(re->d_name,str2)==0)
        {
         chdir(str1);
         path1=getcwd(NULL,0);
         puts(path1);
         chdir("..");
         j=1;
        }
     }
     if(j==0)
     {
     printf("no the file!\n");
     }
}
//输入指令错误时会提示,并可以重新输入//////////////////////////////////////////////////////////////////////
void error_()
{
	printf("input default!\n");
}



(1)strcmp()原型为:int strcmp(const char *str1, const char *str2)作用是:比较字符串str1与str2,若str1>str2则返回值大于0,str1==str2返回值等于0,str1

你可能感兴趣的:(操作系统,Linux,C语言)