手写实现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
    评论
myls命令包含9个选项: (1) myls : 在缺省选项的情况下,列出当前文件夹下的普通文件(不包含隐藏文件)。 例如:当前目录包含文件home1.c, home2.c, .home3.c,输入myls后,列出的文件名为home1.c, home2.c. (2) myls –a: 列出当前文件夹下的所有文件(包含隐藏文件)。 例如:当前目录包含文件home1.c、home2.c、.home3.c,输入myls -a后,列出所有的文件名为home1.c, home2.c, .home3.c. (3) myls –l: 列出当前文件夹下普通文件的详细信息,包括文件模式,文件链接数,文件所属用户,文件所属用户组,文件大小,文件最后修改时间,文件名。并且在最后一行显示该目录下所显示的文件的文件块数。 例如:显示drwxr-xr-x 3 eli eli 4096 Nov 16 23:07 Desktop (4) myls -R 递归列出文件及其子文件。 例如:当前目录为home, 其包含文件home1, home2, home3. 其home1是目录文件,包含文件home11, home12, home2不是目录文件,home3是目录文件,包含文件home31, home32。 输入myls –R后,列出的文件名为 ./home: home1 home2 home3 ./home/home1: home11 home12 ./home/home3: home31 home32. (5) myls –u: 列出当前文件夹下用户x的普通文件,若输入myls -u bb,则显示所属bb的普通文件。 例如:文件home1, home2, home3属于aa,文件tmp1, tmp2, tmp3属于bb, 则若输入myls –u aa, 则显示home1,home2,home3,若输入myls -u bb, 则显示tmp1,tmp2,tmp3。 (6) myls –S: 对文件进行排序,需要输入比较参数。 myls –S 的参数包括: time——按最近修改时间排序 name——按文件名的字典序排序 size——按文件的大小从小到大排序 link——按文件链接数从少到多排序 (7) myls -1: 将当前文件夹下的文件按照一行一个的方式显示。 (8) myls –s: 在各个文件开头显示这个文件的文件块大小。 (9) myls /dirname: 显示/dirname下的文件。 编译 gcc main.c -o myls 执行 ./myls 可加若干参数,具体见上描述 程序并不完整,可能会有BUG,希望广大网友指点,交流~

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值