实现linux pushd功能

/**************************************
 *FILE          :T:\user_pushd\user_pushd.c
 *AUTHOR        :707wk
 *ORGANIZATION  :GT-Soft Studio
 *LAST-MODIFIED :2016/6/18 11:02:38
 *TARGET        :C/C++
 *EMAIL         :gtsoft_wk@foxmail.com
 *LOGO          :
               #########                       
              ############                     
              #############                    
             ##  ###########                   
            ###  ###### #####                  
            ### #######   ####                 
           ###  ########## ####                
          ####  ########### ####               
        #####   ###########  #####             
       ######   ### ########   #####           
       #####   ###   ########   ######         
      ######   ###  ###########   ######       
     ######   #### ##############  ######      
    #######  ##################### #######     
    #######  ##############################    
   #######  ###### ################# #######   
   #######  ###### ###### #########   ######   
   #######    ##  ######   ######     ######   
   #######        ######    #####     #####    
    ######        #####     #####     ####     
     #####        ####      #####     ###      
      #####       ###        ###      #        
        ##       ####        ####              
***************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <unistd.h>

#define FILEPATH_MAX 4096+1

//
//使用循环链表存储目录
struct node
{
	char catalogname[FILEPATH_MAX];
	struct node* next;
};

struct catalogue
{
	struct node* head;
	struct node* end;

	int catalogSum;
};
//

struct catalogue catalog;

void initialization()
{
	char catalogname[FILEPATH_MAX];
	//linux下获取当前路径
	//getcwd(catalogname,FILEPATH_MAX);

	//windows下测试使用
	strcpy(catalogname,"/home/707wk/");

	catalog.head=NULL;
	catalog.end=NULL;
	catalog.catalogSum=0;

	catalog.head=(struct node*)malloc(sizeof(struct node));
	strcpy(catalog.head->catalogname,catalogname);
	catalog.end=catalog.head;
	catalog.head->next=catalog.end;
	catalog.end->next=catalog.head;
	catalog.catalogSum++;
}

void swapnode()
{
	//交换第一与第二节点

	struct node* tmp;

	tmp=catalog.head->next->next;
	catalog.head->next->next=catalog.head;
	catalog.head=catalog.head->next;
	catalog.head->next->next=tmp;
}

void addnode(char option[])
{
	//增加节点

	struct node* tmp=(struct node*)malloc(sizeof(struct node));
	strcpy(tmp->catalogname,option);

	tmp->next=catalog.head;
	catalog.head=tmp;
	catalog.end->next=catalog.head;
	
	catalog.catalogSum++;
}

void movenode(int index)
{
	//循环左移
	
	int i;
	for(i=0;i<index;i++)
	{
		catalog.end=catalog.head;
		catalog.head=catalog.head->next;
		//catalog.end=catalog.end->next;
	}
}

void pushd(char option[])
{
	int index;

	if(option[0]=='\0')
	{
		swapnode();
	}
	else if(option[0]=='/')
	{
		addnode(option);
	}
	else if(option[0]=='+')
	{
		sscanf(option+1,"%d",&index);
		movenode(index);
	}
	else if(option[0]=='-')
	{
		sscanf(option+1,"%d",&index);
		movenode(catalog.catalogSum-1-index);
	}//*/
	else printf("not found\n");
}

void delhead()
{
	struct node* tmpfree=catalog.head;
	catalog.head=catalog.head->next;
	free(tmpfree);
	catalog.catalogSum--;
}

void subnode(int index)
{
	struct node* tmp=catalog.head;
	struct node* tmpfree;

	int i;

	for(i=1;i<index;i++)
	{
		tmp=tmp->next;
	}

	tmpfree=tmp->next;
	tmp->next=tmp->next->next;
	free(tmpfree);

	catalog.catalogSum--;
}

void popd(char option[])
{
	int index;
	
	if(option[0]=='\0')
	{
		delhead();
	}
	else if(option[0]=='+')
	{
		sscanf(option+1,"%d",&index);
		subnode(index);
	}
	else if(option[0]=='-')
	{
		sscanf(option+1,"%d",&index);
		subnode(index);
	}
	else printf("not found\n");
}

void dirs()
{
	int i;
	struct node* tmp=catalog.head;

	/*printf("\t#%s #%s #%d\n\t",
			catalog.head->catalogname,
			catalog.end->catalogname,
			catalog.catalogSum); //*/
	for(i=1;i<catalog.catalogSum;i++)
	{
		printf("%s ",tmp->catalogname);
		tmp=tmp->next;
	}
	printf("%s\n",tmp->catalogname);
}

void freenode()
{
	struct node* tmp;
	int i;
	
	for(i=0;i<catalog.catalogSum;i++)
	{
		tmp=catalog.head;
		free(tmp);
		catalog.head=catalog.head->next;
	}
}

int main()
{
	char inputstr[FILEPATH_MAX];
	char command[FILEPATH_MAX];
	char option[FILEPATH_MAX];

	initialization();
	
	addnode("/bin");
	addnode("/boot");
	addnode("/etc");
	addnode("/mnt");
	addnode("/tmp");
	addnode("/home");
	addnode("/lib");
	
	dirs();

	for(;;)
	{
		option[0]='\0';
		gets(inputstr);
		sscanf(inputstr,"%s %s",command,option);
		if(strcmp(command,"pushd")==0)
		{
			pushd(option);
			dirs();
		}
/*		else if(strcmp(command,"popd")==0)
		{
			printf("unfinished");
			popd(option);
			dirs();
		}//*/
		else if(strcmp(command,"dirs")==0)
		{
			dirs();
		}
		else if(strcmp(command,"exit")==0)
		{
			break;
		}
		else system(inputstr);
	}

	freenode();

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值