用IO 完成minishell 基本功能(多文件编程)

本文介绍了一个使用C语言实现的Linux命令行工具,包括用户输入解析、基本文件操作函数(如touch、rm、cd等)以及命令执行功能。
摘要由CSDN通过智能技术生成
1 #include "head.h"
  2 
  3 /* 显示终端命令行 */                                                                                           
  4 int ShowTerminal(void)
  5 {
  6     char c[4096] = {0};
  7     char *pp = NULL;
  8 
  9     getcwd(c, sizeof(c));
 10     
 11     /* 找到当前所在目录 */
 12     pp = c+strlen(c) -1;
 13 
 14     while (*pp != '/')
 15     {
 16         pp--;
 17     }
 18     pp++;
 19     printf("[linux@Ubuntu:%s]", pp);
 20     return 0;
 21 }
 22 
 23 int GetUserCommand(char *cc, int maxlen)
 24 {
 25     fgets(cc, maxlen, stdin);
 26     cc[strlen(cc)-1] = '\0';
 27     return 0;
 28 }
 29 
 30 int ExecCommand(char *cc)
 31 {
 32     if (!strncmp(cc, "ls -a", 5))
 33     {
 34         Mylsa();    
 35     }
 36     else if (!strncmp(cc, "ls -l", 5))
 37     {
 38         Mylsl();        
 39     }
 40     else if (!strncmp(cc, "ls", 2))
 41     {
 42         Myls(); 
 43     }
 44     else if (!strncmp(cc, "cd", 2))
 45     {
 46         Mycd(cc);
 47     }else if(!strncmp(cc,"touch",5))
 48     {
 49         Mytouch(cc);
 50     }else if(!strncmp(cc,"rm",2))
 51     {
 52         Myrm(cc);
 53 
 54     }else if(!strncmp(cc,"rmdir",5))
 55     {
 56         Myrmdir(cc);
 57     }else if(!strncmp(cc,"mkdir",5))
 58     {
 59         Mymkdir(cc);
 60     }else if(!strncmp(cc,"cp",2))
 61     {   
 62         Mycp(cc);
 63     }else if(!strncmp(cc,"cat",3))
 64     {
 65         Mycat(cc);
 66     }else if(!strncmp(cc,"ln",2))
 67     {
 68         Myln(cc);
 69     }else if(!strncmp(cc,"pwd",3))
 70     {
 71         Mypwd();                                                                                               
 72     }else if(!strncmp(cc,"mv",2))
 73     {
 74         Mymv(cc);
 75     }else if(!strncmp(cc,"chmod",5))
 76     {
 77         Mychmod(cc);
 78     }
 79     
 80     return 0;
 81 }
 82                             
//主函数!

  1 #include "head.h"
  2 #include "record.h"
  3 int main(void)
  4 {
  5     char ccc[10000] = {0};
  6     int a =0;
  7     Initrecord(); //打开计时文件
  8 
  9     while(1)
 10     {
 11         ShowTerminal();
 12 
 13         GetUserCommand(ccc,4096);
 14 
 15         if(strcmp(ccc,"exit")==0)
 16         {
 17             Deinitrecord();//结束计时文件
 18             break;
 19         }
 20                                                                                                                
 21         Recordcommand(ccc);
 22 
 23         ExecCommand(ccc);
 24 
 25     }
 26     return 0;
 27 }
 28 
 29 
 30 

具体功能的实现

#include "head.h"

int Mytouch(char *cc)
{  
	char *pp=NULL;
	int a = 0;
	pp = cc;
	 
	while(*pp != ' ')
	{
		pp++;
	}
	*pp = '\0'; pp++;
	/*
	char *dp=strtok(pp," ");
	while(dp !=NULL)
	{
		strcpy(pp,dp);
		dp = strtok(NULL," ");
		pp++;
	}
	*/
	a=open(pp,O_WRONLY|O_CREAT,0664);
	close(a);
	return 0;
}

int Myrm(char *cc)
{
	char * pp =NULL;
	pp = cc;
	while(*pp != ' ')
	{
		pp++;
	}
	*pp = '\0';pp++;
	remove(pp);
	return 0;
}

int Myrmdir(char *cc)
{
	char *pp =NULL;
	pp = cc;
	while(*pp !=' ')
	{
		pp++;
	}
	*pp = '\0';pp++;
	rmdir(pp);
}
int Mymkdir(char *cc)
{

	char *pp =NULL;
	pp = cc;
	while(*pp !=' ')
	{
		pp++;
	}
	*pp = '\0';pp++;
	mkdir(pp,0777);
}
int Mycp(char *cc)
{
	
	FILE *p=NULL;
	FILE *q=NULL;
	char *pp=NULL;
	char ww[4096] = {0};
	char *w[10]={NULL};
	int a = 0;
	pp=cc;
	while(1)
	{

		w[a]=pp;	
		a++;
		while(*pp !='\0' && *pp!=' ')
		{
			pp++;
		}
	
		if('\0'==*pp)
		{
			break;
		}
	    *pp='\0';pp++;
		while(*pp ==' ')
		{
			pp++;
		}
	}
	
		p = fopen(w[1],"r");
		q = fopen(w[2],"w+");
	
	while(1)
	{
		if(NULL==fgets(ww,sizeof(ww),p))
		{
			break;
		}
		fputs(ww,q);
	}
	fclose(p);fclose(q);

}

int Mycat(char *cc)
{
	FILE *p=NULL;
	char *pp=NULL;
	int a=0; char *w[10]={NULL};
	char ww[4096]={0};
	pp=cc;
	while(1)
	{
		w[a]=pp;a++;
		while(*pp !='\0' && *pp!=' ')
		{
			pp++;
		}
		if('\0'==*pp)
		{
			break;
		}
		*pp = '\0';pp++;
		while(*pp==' ')
		{
			pp++;
		}
	}

	p = fopen(w[1],"r");
	
	while(NULL != fgets(ww,sizeof(ww),p))
	{
		printf("%s",ww);
	}
	fclose(p);
}

int Myln(char *cc)
{
	char *w[10]={NULL};
	int a=0;
	char *p =NULL;char *q =NULL;
	char *token = strtok(cc," ");
	while(token != NULL)
	{
		w[a]=token;
		token  = strtok(NULL," ");
		++a;
	}
	p=w[1];
	q=w[2];
	link(p,q);
	
}

int Mypwd(void)
{
	char w[4096]={0};
	getcwd(w,sizeof(w));
	printf("%s\n",w);

}

int Mymv(char *cc)
{
	char *w[10]={NULL};
	int a=0;
	char *p =NULL;char *q =NULL;
	char *token = strtok(cc," ");
	while(token != NULL)
	{
		w[a]=token;
		token  = strtok(NULL," ");
		++a;
	}
	rename(w[1],w[2]);
}

int Mychmod(char *cc)
{
	char *pp =NULL;char *qq =NULL;
	mode_t ww = 0;
	char *w[10]={NULL};
	int a=0;
	char *p =NULL;char *q =NULL;
	char *token = strtok(cc," ");
	while(token != NULL)
	{
		w[a]=token;
		token  = strtok(NULL," ");
		++a;
	}
	pp = w[1];
	qq = w[2];
	ww = strtol(pp,NULL,8); //字符型转换为8

	chmod(qq,ww);
}

对库函数进行封装 封装为 head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>

extern int ShowTerminal(void);
extern int GetUserCommand(char *cc,int maxlen);
extern int ExecCommand(char *cc);
extern int Mycd(char *cc);
extern int Myls(void);
extern int Mylsa(void);
extern int Mylsl(void);
extern int Message(const char *pdirname);
extern int Mytouch(char *filename);
extern int Myrm(char *cc);
extern int Myrmdir(char *cc);
extern int Mymkdir(char *cc);
extern int Mycp(char *cc);
extern int Mycat(char *cc);
extern int Myln(char *cc);
extern int Mypwd(void);
extern int Mymv(char *cc);
extern int Mychmod(char *cc);


#endif

多文件编程‘管理器’

  1 OBJ=a.out
  2 OBJS=main.c terminl.c mycd.c myls.c mylsa.c mylsl.c mytouch.c record.c      
  3 
  4 $(OBJ):$(OBJS)
  5     gcc $^ -o $@
~                                                                               
~                             

加深了我对标准IO命令的运用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值