C语言实现Linux命令系列

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/BluseLIBB/article/details/99814685
  • 1、ls的简单实现,显示./目录下的文件

     /**********************************************
      *
      *			语言实现linux系列 - ls
      *			  Bluseli 2019.8.20
      *
      **********************************************/
     #include <stdio.h>
     #include <sys/types.h>
     #include <dirent.h>
     
     int main(int argc, char* argv[])
     {
     	/*
     	if(argc<2)
     	{
     		printf("Usage: --filedir\n");
     		return -1;
     	}
     	*/
     	
     	DIR *dp=NULL;
     	struct dirent * dt=NULL;
     	dp=opendir("./");
     	if(dp==NULL)
     	{
     		perror("opendir err");
     		return -1;
     	}
     
     	while(1)
     	{
     		dt = readdir(dp);
     		if(dt==NULL)
     		{
     			break;
     		}
     		if(dt->d_name[0] != '.')
     			printf("%s\n", dt->d_name);
     	}
     
     	closedir(dp);
     
     	return 0;
     }
    
  • 2、ls进阶版实现ls -l,显示某个文件或目录下的文件的详细信息

     /**********************************************
      *
      *			C语言实现linux系列 - ls -l
      *				Bluseli 2019.8.20
      *
      **********************************************/
     #include <stdio.h>
     #include <sys/types.h>
     #include <sys/stat.h>
     #include <unistd.h>
     #include <pwd.h>
     #include <time.h>
     #include <dirent.h>
     
     //显示文件详细信息
     int showfile(char *filename)
     {
     	int ret;
     	struct stat st;
     	struct passwd *pd = NULL;
     	struct tm * tt;
     
     	ret = stat(filename,&st);
     	if(ret !=0)
     	{
     		perror("stat err");
     		return -1;
     	}
     
     	//判断文件类型
     	/* 使用宏判定文件类型
     	if(S_ISREG(st.st_mode) )
     	{
     		printf("-");
     	}
     	if(S_ISDIR(st.st_mode))
     	{
     		printf("d");
     	}
     	if(S_ISCHR(st.st_mode))
     	{
     		printf("c");
     	}
     	if(S_ISBLK(st.st_mode))
     	{
     		printf("b");
     	}
     	if(S_ISFIFO(st.st_mode))
     		printf("f");
     	if(S_ISLNK(st.st_mode))
     		printf("l");
     	if(S_ISSOCK(st.st_mode))
     		printf("s");
     	*/
     	int ftype=st.st_mode&S_IFMT;
     	switch(ftype)
     	{
     		case S_IFSOCK:	printf("s");break;
     		case S_IFLNK:	printf("l");break;
     		case S_IFREG:	printf("-");break;
     		case S_IFBLK:	printf("b");break;
     		case S_IFDIR:	printf("d");break;
     		case S_IFCHR:   printf("c");break;
     		case S_IFIFO:	printf("p");break;
     		default :		printf("?");break;
     	}
     
     	//判断权限
     	if(st.st_mode & S_IRUSR)
     		printf("r");
     	else 
     		printf("-");
     	if(st.st_mode & S_IWUSR)
     		printf("w");
     	else 
     		printf("-");
     	if(st.st_mode & S_IXUSR)
     		printf("x");
     	else 
     		printf("-");
     
     	if(st.st_mode & S_IRGRP)
     		printf("r");
     	else 
     		printf("-");
     	if(st.st_mode & S_IRGRP)
     		printf("w");
     	else 
     		printf("-");
     	if(st.st_mode & S_IXGRP)
     		printf("x");
     	else 
     		printf("-");
     
     	if(st.st_mode & S_IROTH)
     		printf("r");
     	else 
     		printf("-");
     	if(st.st_mode & S_IWOTH)
     		printf("w");
     	else 
     		printf("-");
     	if(st.st_mode & S_IXOTH)
     		printf("x");
     	else 
     		printf("-");
     
     
     	//判断链接数
     	printf(" %ld ",st.st_nlink);
     	//判断所有者
     	pd =getpwuid(st.st_uid);
     	printf(" %s ",pd->pw_name);
     	//判断所属组
     	pd =getpwuid(st.st_gid);
     	printf(" %s ",pd->pw_name);
     	//大小
     	printf(" %ld ", st.st_size);
     	//获取时间
     	tt = localtime(&st.st_mtim.tv_sec);
     	printf(" %d月 %d %d:%d ", tt->tm_mon+1,tt->tm_mday\
     			,tt->tm_hour,tt->tm_min);
     
     	printf(" %s\n", filename);
     
     	return 0;
     }
     //显示文件夹详细信息
     int showdir(char *path)
     {
     	DIR *dp=NULL;
     	struct dirent * dt=NULL;
     	dp=opendir(path);
     	if(dp==NULL)
     	{   
     		perror("opendir err");
     		return -1; 
     	}   
     
     	while(1)
     	{   
     		dt = readdir(dp);
     		if(dt==NULL)
     		{
     			break;
     		}
     		if(dt->d_name[0] != '.')
     			showfile(dt->d_name);
     	}   
     
     	closedir(dp);
     }
     
     int main(int argc, char* argv[])
     {
     	//char *filename="./a.out";
     
     	if(argc<2)
     	{
     		printf("Usage: --filepath or filedir\n");
     		return -1;
     	}
     
     	struct stat st;
     	stat(argv[1],&st);
     	//如果参数是文件夹,遍历文件夹内容所有文件
     	if (S_ISDIR(st.st_mode))
     	{
     		showdir(argv[1]);
     	}
     	//如果是文件,显示文件详细信息
     	else
     	{
     		showfile(argv[1]);
     	}
     	return 0;
     }
    

持续CODING。。。

猜你喜欢

转载自blog.csdn.net/BluseLIBB/article/details/99814685