myls程序实现

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

#define BUFSIZE 1024

static int cnt;

void name_color(const char *name);
int mycmp(const char *p);
int myls(char *p, char *d_name, int mod);
int myls_dir(char *p, int mod);
int main (int argc, char **argv)
{            
	char *optstring = "-laih";//选项字符串
		//a选项字符 -a选项参数//带-需要识别非选项字符
	int c;
	int mod = 0;
	int sum = 0;
	char *p = NULL;

	if(argc < 2)
		return 1;

	while(1)	{
		c = getopt(argc, argv, optstring);
		if(c == -1)		
			break;
		switch(c)	{
			case 'i':
				mod |= 1 << 2;
				break;
			case 'l':
				mod |= 1 << 1;
				break;
			case 'a':
				mod |= 1;
			case 'h':
				break;
			case '?':
				printf("含有未识别标识符\n");
				break;
			case 1:
				p = argv[optind-1];
			default:
				break;
		}
	}
	sum = myls_dir(p, mod);
	if(mod & 2)
		printf("总用量 %d\n", sum);
}

int myls_dir(char *p, int mod)
{
	struct stat mystat;
	DIR *dp = NULL;
	struct dirent *entry = NULL;
	char buf[BUFSIZE] = {};
	dp = opendir(p);
	int sum = 0;
	if(stat(p, &mystat) == -1)	{
		perror("stat");
		return -1;
	}
	
	if((mystat.st_mode & S_IFMT) != S_IFDIR)	{	//非目录
		sum += myls(NULL, p, mod);
		return 0;
	}
	//为目录
	if(NULL == dp)	{
		perror("opendir()");
		return -1;
	}

	while(1)	{
		entry = readdir(dp);
		if(NULL == entry)	{
			if(errno)	{
				perror("readdir()");
				closedir(dp);
				return -1;
			}
			break;
		}
	/*	memset(buf, '\0', BUFSIZE);
		strcpy(buf, p);
		strcat(buf, "/");
		strcat(buf, entry->d_name);
	*/	if((mod & 1) || mycmp(entry->d_name))
			sum += myls(p, entry->d_name, mod);
	}
	if(cnt != 0)
		printf("\n");
	return sum/2;	

}

int mycmp(const char *p)
{
	return *p != '.';
}

int myls(char *p, char *d_name, int mod)
{
	struct stat mystat;
	char buf[BUFSIZE] = {};
	memset(buf, '\0', BUFSIZE);
	strcpy(buf, p);
	strcat(buf, "/");
	strcat(buf, d_name);
	if(stat(buf, &mystat) == -1)	{
		perror("stat");
		return -1;
	}
	if(mod & 4)	{					//显示ID
		printf("%ld ", mystat.st_ino);
	}
	if(mod & 2)	{					//显示长格式
		//类型
		switch(mystat.st_mode & S_IFMT)	{
			case S_IFREG:
				printf("-");
				break;
			case S_IFDIR:
				printf("d");
				break;
			case S_IFCHR:
				printf("c");
				break;
			case S_IFBLK:
				printf("b");
				break;
			case S_IFIFO:
				printf("p");
				break;
			case S_IFLNK:
				printf("l");
				break;
			case S_IFSOCK:
				printf("s");
			default:
				break;
		}
		//读写执行权限
		if (mystat.st_mode & S_IRUSR)
			putchar('r');
		else
			putchar('-');
		if (mystat.st_mode & S_IWUSR)
			putchar('w');
		else
			putchar('-');
		if (mystat.st_mode & S_IXUSR)	{
			if(mystat.st_mode & S_ISUID)
				putchar('s');
			else
				putchar('x');
		}
		else
			putchar('-');
		if (mystat.st_mode & S_IRGRP)
			putchar('r');
		else
			putchar('-');
		if (mystat.st_mode & S_IWGRP)
			putchar('w');
		else
			putchar('-');
		if (mystat.st_mode & S_IXGRP)
			putchar('x');
		else
			putchar('-');
		if (mystat.st_mode & S_IROTH)
			putchar('r');
		else
			putchar('-');
		if (mystat.st_mode & S_IWOTH)
			putchar('w');
		else
			putchar('-');
		if (mystat.st_mode & S_IXOTH)	{
			if(mystat.st_mode & S_ISVTX)
				putchar('t');
			else
				putchar('x');
		}
		else
			putchar('-');
		printf(" %ld ", mystat.st_nlink);
		struct passwd *pwd = NULL;
		struct group *gid = NULL;
		//所属用户和组
		pwd = getpwuid(mystat.st_uid);
		printf("%s ", pwd->pw_name);
		gid = getgrgid(mystat.st_gid);
		printf("%s", gid->gr_name);
		//总字节个数
		printf("%6ld ", mystat.st_size);
		struct tm *tm_p = NULL;
		tm_p = localtime(&mystat.st_mtim.tv_sec);
		char buf[BUFSIZE] = {};
		//最后修改日期
		strftime(buf, BUFSIZE, "%m月  %d %H:%M", tm_p);
		if(*buf == '0')
			printf("%s ", buf+1);
		else
			printf("%s ", buf);
	}
	//显示文件名
	name_color(d_name);
	printf("\n");
#if 0		//格式调整	
	if(mod & 2)			//长格式直接换行
		printf("\n");
	else if(mod & 4)	{	//显示id每行显示4项
		if(cnt == 3)	{
			printf("\n");
			cnt = 0;
		}
		else
			cnt++;
	}
	else if(cnt == 5)	{	//通常显示6项
		printf("\n");
		cnt = 0;
	}
	else
		cnt++;
#endif
	return mystat.st_blocks;
}

void name_color(const char *name)
{
	char *p = NULL;
	p = strrchr(name, '.');
	if(p != NULL)	{
		if(*name == '.')					//以.开头为隐藏文件设置高亮
			printf("\033[34m\033[1m%-10s\033[0m ", name);
		else if(strcmp(p, ".out") == 0)	 //.out结尾为可执行文件设置高亮
			printf("\033[32m\033[1m%-10s\033[0m ", name);
		else
			printf("%-10s ", name);			//通常带点文件
	}
	else
		printf("%-10s ", name);				//通常不带点文件
}

 

转载于:https://www.cnblogs.com/764120305l/p/10536382.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值