手写实现Linux中的ls命令:

此ls命令采用Linux C实现,实现功能为可以输入“./Ls -l”(展示当前目录下的所有文件信息),“./Ls -li”(展示当前目录下的所有信息含inode) ,“./Ls -li file”(展示特定文件),"./Ls -li directory"(采用递归访问的方式展示目录下的所有文件)

此为工程所有源代码文件:

此部分用于处理命令行参数以及总功能函数的调用:

//    main.c
//    Author:Kaier
//    Version:1.0

#include <stdlib.h>
#include <stdio.h>
//#include"my_parse.h"
extern void my_parse(char *option,char *path);

int main(int argc,char *argv[])
{
	unsigned int i;
	if (argc < 2)
	{
		perror("arguments' count error!");
		exit(EXIT_FAILURE);					
	}else if(argc == 2){
		my_parse(argv[1],"./");
	}else if(argc >2){
		for(i=2;i<argc;i++)
		{
			my_parse(argv[1],argv[i]);
			puts("-------------------------------");
		}//for
	}//else

	return 0;
}

接下来解析命令行参数:

//     my_parse.h
//       Author:Kaier
//       version:1.0

#ifndef _MY_PARSE_H
#define _MY_PARSE_H
void my_parse(char *option,char *path);
#endif
//    my_parse.c
//    Author:Kaier
//    Version:1.0

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
//#include "handle.h"
extern void handle_reg(char *option,char *path);
extern void handle_dir(char *option,char *path);

void my_parse(char *option,char *path)
{
	struct stat buf;
	if(lstat(path,&buf)<0)	
	{
		perror("stat");
		exit(EXIT_FAILURE);	
	}
	/*handle directory file*/
	if(S_ISDIR(buf.st_mode))	{
		handle_dir(option,path);
	}
	/*handle regular file*/
	else if(S_ISREG(buf.st_mode))
		handle_reg(option,path);
}//my_parse

此部分分别对普通文件和目录及目录文件项的信息展示,其中遇到目录就进行递归:

//    handle.h
//    Author:Kaier
//    Version:1.0

#ifndef _HANDLE_H
#define _HANDLE_H

void handle_reg(char *option,char *path);
void handle_dir(char *option,char *path);

#endif
//    handle.c
//    Author:Kaier
//    Verison:1.0

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include<libgen.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
#include<time.h>
#include <sys/types.h>
#include <sys/stat.h>
static void show_info(struct stat *buf,char *path);

/*******************************/

/*handle the regular file*/
void handle_reg(char *option,char *path)
{
	struct stat buf;
	if((lstat(path,&buf))<0)
	{
		perror("stat");	
		exit(EXIT_FAILURE);
	}//if	
	if(strcmp(option,"-l")==0)
	{
		show_info(&buf,path);		
	}
	else if(strcmp(option,"-li")==0)
	{
		printf("%d ",buf.st_ino);
		show_info(&buf,path);		
	}else{
		printf("No option with %s\n!",option);
	}//else		
}

/*handle the directory file*/
void handle_dir(char *option,char *path)
{		
	/*create a stat obj*/
	struct stat buf;
	/*allocate the directory stream pointer if the file is directory*/	
	DIR *dp;
	/*allocate the pointer of entry in directory*/
	struct dirent *p_dentry;

	if((dp = opendir(path))==NULL){
		perror("opendir");
		exit(EXIT_FAILURE);
	}
	chdir(path);
	while((p_dentry=readdir(dp))!=NULL)
	{
			lstat(p_dentry->d_name,&buf);		
			if(S_ISDIR(buf.st_mode)){
			
				if(strcmp(p_dentry->d_name,".")==0||
				strcmp(p_dentry->d_name,"..")==0)  continue;			
				
				handle_reg(option,p_dentry->d_name);
				handle_dir(option,p_dentry->d_name);	//curse the function		
			
			}
			else
			{
				//end of curse,handle regular file
				handle_reg(option,p_dentry->d_name);
			}

	}//while
	chdir("..");
	closedir(dp);
}

/*-----------------all the subfunc of show info------------------*/
/*show authorities for user,group and others*/
static void show_perm(struct stat *buf)
{
	unsigned int i,mask=0700;
	char *perm[] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx" };
	/*print specified file flag*/
	if(S_ISDIR(buf->st_mode))
	{
		putchar('d');
	}else if(S_ISLNK(buf->st_mode))
		putchar('l');
	else if(S_ISBLK(buf->st_mode)){
		putchar('b');
	}else 
		putchar('-');
	/*print authorities of all user*/	
	for(i=3;i;i--)
	{
		printf("%3s",perm[(buf->st_mode & mask)>>(i-1)*3]);	
		mask >>= 3;
	}//for
	putchar(' ');
}

/*get the owner name and group name on file*/
static void show_ugname(struct stat *buf)
{
	struct passwd *pwd;	
	struct group *gp;
	pwd = getpwuid(buf->st_uid);					
	printf("%s ",pwd->pw_name);
	gp = getgrgid(buf->st_gid);
	printf("%s ",gp->gr_name);		
}
/*show the last modified time for file*/
static void show_time(struct stat *buf)
{
	char *date=NULL;	
	char *s=NULL;
	int len=0;
	/*get time*/
	time_t time;
	time = buf->st_mtime;
	s = ctime(&time);
	
	len = strlen(s);

	date = (char*)malloc(sizeof(char)*len);
	memcpy(date,s,len-1);
	date[len-1] = '\0';
	printf("%s ",date);
}
/*show the file's name.If file is linking file,also show the source file*/
static void show_name(struct stat *buf,char *path)
{
	char *name;
	char src_name[20];
	name = basename(path);	
	if(S_ISLNK(buf->st_mode))
	{
		//color
		printf("\033[47;32m%s -> \033[0m",name);
		memset(src_name,'\0',sizeof src_name);
		readlink(path,src_name,20);
		printf("\033[47;32m%s\033[0m",src_name);

	}else if(access(path,X_OK)==0){
		//color
		printf("\033[47;36m%s\033[0m",name);		
	}else{
		printf("%s",name);
	}//else

}

//show the status infomation of file in list
static void show_info(struct stat *buf,char *path)
{
	/*show authorities for user,group and others*/
	show_perm(buf);		
	/*show the number of hard link*/	
	printf("%d ",buf->st_nlink);		
	/*show the owner name and group name on file*/	
	show_ugname(buf);
	/*show the storage in byte*/	
	printf("%d ",buf->st_size);	
	/*show the last modified time for file*/	
	show_time(buf);	
	/*show the file's name*/
	show_name(buf,path);	
	
	putchar('\n');
}


/*****************end of file************************/

Makefile如下:

Ls:main.o my_parse.o handle.o
	gcc -g -o $@ $^
main.o:main.c
	gcc -c $^ -o $@
my_parse.o:my_parse.c 
	gcc -g -c $^  -o $@
handle.o:handle.c 
	gcc -g -c $^  -o $@
clean:
	rm -f *.o	

运行示例:

 

对特定文件(此处123为目录文件)进行信息展示:

使用了递归方式进行目录访问:

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值