my_ls

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

int a_flag = 0;
int l_flag = 0;

int display_file(const char *path)
{
	struct stat f_info;
	struct tm *ptm;

	if(lstat(path,&f_info) < 0)
	{
		perror("Fail to stat");
		return -1;
	}
	
	//文件类型
	switch(f_info.st_mode & S_IFMT)
	{
	case S_IFSOCK:
		putchar('s');
		break;

	case S_IFLNK:
		putchar('l');
		break;

	case S_IFREG:
		putchar('-');
		break;

	case S_IFBLK:
		putchar('b');
		break;

	case S_IFDIR:
		putchar('d');
		break;

	case S_IFCHR:
		putchar('c');
		break;

	case S_IFIFO:
		putchar('p');
	}

	//权限
	if(f_info.st_mode & S_IRUSR)
	{
		putchar('r');
	}else{
		putchar('-');
	}
	
	if(f_info.st_mode & S_IWUSR)
	{
		putchar('w');
	}else{
		putchar('-');
	}

	if(f_info.st_mode & S_IXUSR)
	{
		putchar('x');
	}else{
		putchar('-');
	}
	
	if(f_info.st_mode & S_IRGRP)
	{
		putchar('r');
	}else{
		putchar('-');
	}

	if(f_info.st_mode & S_IWGRP)
	{
		putchar('w');
	}else{
		putchar('-');
	}

	if(f_info.st_mode & S_IXGRP)
	{
		putchar('x');
	}else{
		putchar('-');
	}

	if(f_info.st_mode & S_IROTH)
	{
		putchar('r');
	}else{
		putchar('-');
	}

	if(f_info.st_mode & S_IWOTH)
	{
		putchar('w');
	}else{
		putchar('-');
	}
	if(f_info.st_mode & S_IXOTH)
	{
		putchar('x');
	}else{
		putchar('-');
	}
	
	putchar(' ');

	printf("%d",f_info.st_nlink);

	putchar(' ');

	printf("%s",getpwuid(f_info.st_uid)->pw_name);

	putchar(' ');

	printf("%s",getgrgid(f_info.st_gid)->gr_name);
	
	putchar(' ');

	printf("%ld",f_info.st_size);

	printf("  ");

	ptm = localtime(&f_info.st_mtime);
	
	fprintf(stdout,"%d-%d-%d  %d:%d:%d  ",ptm->tm_year + 1900,ptm->tm_mon + 1,\
				ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
	
	///home/linux/test.c
	//test.c
	char *p = (char *)(path + strlen(path) - 1);
	while(*p != '/' && p != path) p --;
	
   if(p == path)
	   printf("%s\n",path);
   else
	   printf("%s\n",p + 1);

	return 0;
}

// /home/linux/ 
int display_dir(const char *name)
{
	DIR *pdir;
	struct dirent *pdirent;
	int file_count = 0,dir_count = 0;
	char path_name[1024];
	
	if((pdir = opendir(name)) == NULL)
	{
		fprintf(stderr,"Fail to opendir %s : %s.\n",name,strerror(errno));
		return -1;
	}

	while( pdirent = readdir(pdir) )
	{
		if(pdirent->d_name[0] == '.' && a_flag != 1)
			continue;
		
		if(name[strlen(name) - 1] == '/')
		{
			sprintf(path_name,"%s%s",name,pdirent->d_name);
		}else{
			sprintf(path_name,"%s/%s",name,pdirent->d_name);
		}
		
		if(l_flag == 1)
		{
			display_file(path_name);
		}else{
			printf("%s ",pdirent->d_name);
		}
	}

	return 0;
}

//./a.out -a
//./a.out -l
//./a.out -l file
//./a.out -l dir 
int main(int argc, char  * const argv[])
{
	int i = 0;
	int ch;
	struct stat f_info;
	
	/*处理命令行参数*/
	while((ch = getopt(argc,argv,"al")) != -1)
	{
		switch(ch)
		{
		case 'a':
			a_flag = 1;
			break;

		case 'l':
			l_flag = 1;
			break;

		default:
			printf("Invalid option character.\n");
			return -1;
		}
	}

	if(argc == optind)
	{
		display_dir(".");
	
	}else{
	
		for(i = optind;i < argc;i++)
		{
			if(stat(argv[i],&f_info) < 0)
			{
				perror("Fail to stat");
				return -1;
			}

			if(S_ISDIR(f_info.st_mode))
			{
				display_dir(argv[i]);
			}else{
				if(l_flag == 1)
					display_file(argv[i]);
				else
					printf("%s ",argv[i]);
			}
		}
	}

	printf("\n");
		
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int a_flag = 0;
int l_flag = 0;

//./a.out -a -l -g
int main(int argc,  char * const argv[])
{
	int ch;

	while( (ch = getopt(argc,argv,"al")) != -1)
	{
		switch(ch)
		{
		case 'a':
			a_flag = 1;
			break;
		case 'l':
			l_flag = 1;
			break;

		default:
			printf("%c.\n",ch);
			break;
		}
	}

	printf("argc = %d optind = %d.\n",argc,optind);
	
	return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静思心远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值