周作业:仿制 ls ,命令一部分

-i:inode号
-t:访问时间,修改时间
-p:权限
-f:指定文件

#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<time.h>
#include<dirent.h>
#define MAX_SIZE 255

void quanxian(char *dest, struct stat buf);

int main(int argc, char *argv[]){
	int i = 0;
	int ch = 0;
	
	//显示当前目录下的文件
	DIR *dir;
	struct dirent *dir_name;

	//时间
	struct stat buf;
	struct tm *atime = (struct tm*)malloc(sizeof(struct tm));
	struct tm *mtime = (struct tm*)malloc(sizeof(struct tm));
	
	//权限
	char dest[10];
	for(i = 1; i < 10; i++){
		dest[i] = '-';
	}
	
	//没有参数
	if(argc == 1){
		if((dir=opendir(".")) == NULL){
			printf("opening dir error");
			exit(0);
		}
		while((dir_name=readdir(dir)) != NULL){
			printf("%s\n",dir_name->d_name);
		}
		closedir(dir);
	}else if(argv[1][1] == 'f' || argv[1][2] == 'f' || argv[1][3] == 'f' || argv[1][4] == 'f'){
		if(lstat(argv[2], &buf) < 0){
			printf("lstat error\n");
		}
		while((ch=getopt(argc, argv,"f:tip")) != -1){
			switch(ch){
				case 'f':
					printf("自定义文件夹\n");
					break;
				case 't':
					atime = localtime(&buf.st_atime);
					mtime = localtime(&buf.st_mtime);
					printf("文件的访问时间: %d-%02d-%02d %02d:%02d\n",atime->tm_year+1900, atime->tm_mon+1, atime->tm_mday, atime->tm_hour, atime->tm_min);
					printf("文件的修改时间:%d-%02d-%02d %02d:%02d\n",mtime->tm_year+1900, mtime->tm_mon+1, mtime->tm_mday, mtime->tm_hour, mtime->tm_min);
					break;
				case 'i':
					printf("文件的inode号:%ld\n",buf.st_ino);
					break;
				case 'p'://加入目标文件路径
					quanxian(dest,buf);
					break;
			}
		}

	}else{
		while((ch=getopt(argc, argv, "f:tip")) != -1){
			switch(ch){
				case 't':
					if((dir = opendir(".")) == NULL){
						printf("opening dir error\n");
						exit(0);
					}
					while((dir_name=readdir(dir)) != NULL){
						if(lstat(dir_name->d_name,&buf) < 0){
							printf("lstat dir error\n");
							exit(0);
						}
						printf("%s\t\t\t",dir_name->d_name);
						atime = localtime(&buf.st_atime);
						mtime = localtime(&buf.st_mtime);
						printf("文件的访问时间: %d-%02d-%02d %02d:%02d\tt",atime->tm_year+1900, atime->tm_mon+1, atime->tm_mday, atime->tm_hour, atime->tm_min);
						printf("文件的修改时间:%d-%02d-%02d %02d:%02d\n",mtime->tm_year+1900, mtime->tm_mon+1, mtime->tm_mday, mtime->tm_hour, mtime->tm_min);
					}
					closedir(dir);
					break;
				case 'i':
					if((dir = opendir(".")) == NULL){
						printf("opening dir error\n");
						exit(0);
					}
					while((dir_name=readdir(dir)) != NULL){
						if(lstat(dir_name->d_name,&buf) < 0){
							printf("lstat dir error\n");
							exit(0);
						}
						printf("%s\t\t\t",dir_name->d_name);
						printf("文件的inode号:%ld\n",buf.st_ino);
					}
					closedir(dir);
					break;
				case 'p':
					if((dir = opendir(".")) == NULL){
						printf("opening dir error\n");
						exit(0);
					}
					while((dir_name=readdir(dir)) != NULL){
						if(lstat(dir_name->d_name,&buf) < 0){
							printf("lstat dir error\n");
							exit(0);
						}
						printf("%s\t\t\t",dir_name->d_name);
						quanxian(dest,buf);
					}
					closedir(dir);
					break;
			}
		}
	}

	exit(0);
}
void quanxian(char *dest, struct stat buf){
		int i = 0;
		
		//开启超级无敌九轮if神功
		if(S_IRUSR & buf.st_mode){
			dest[1] = 'r';
		}
		if(S_IWUSR & buf.st_mode){
			dest[2] = 'w';
		}
		if(S_IXUSR & buf.st_mode){
			dest[3] = 'x';
		}
		if(S_IRGRP & buf.st_mode){
			dest[4] = 'r';
		}
		if(S_IWGRP & buf.st_mode){
			dest[5] = 'w';
		}
		if(S_IXGRP & buf.st_mode){
			dest[6] = 'x';
		}
		if(S_IROTH & buf.st_mode){
			dest[7] = 'r';
		}
		if(S_IWOTH & buf.st_mode){
			dest[8] = 'w';
		}
		if(S_IXOTH & buf.st_mode){
			dest[9] = 'x';
		}
		printf("文件的权限:");
		for(i = 1; i < 10; i++){
			printf("%c",dest[i]);
		}
		printf("\n");

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值