⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 custom_ls.c

📁 c实现的cuntom ls
💻 C
字号:
#include <stdio.h>#include <ctype.h>#include <dirent.h>#include <sys/types.h>#include <sys/stat.h>#include <limits.h>#include <unistd.h>#include <memory.h>#include <pwd.h>#include <grp.h>#ifdef PATH_MAXstatic int pathmax = PATH_MAX;#elsestatic int pathmax = 256;#endif//translate the bitPermision to the char presentationchar* GetPermisionMode(char *buf, mode_t mode){char *psz = buf;memset(buf, 0, sizeof buf);if(S_ISREG(mode)) *psz++ = '-';if(S_ISDIR(mode)) *psz++ = 'd';if(S_IRUSR & mode) *psz++ = 'r';else *psz++ = '-';if(S_IWUSR & mode) *psz++ = 'w';else *psz++ = '-';if(S_IXUSR & mode) *psz++ = 'x';else *psz++ = '-';if(S_IRGRP & mode) *psz++ = 'r';else *psz++ = '-';if(S_IWGRP & mode) *psz++ = 'w';else *psz++ = '-';if(S_IXGRP & mode) *psz++ = 'x';else *psz++ = '-';if(S_IROTH & mode) *psz++ = 'r';else *psz++ = '-';if(S_IWOTH & mode) *psz++ = 'w';else *psz++ = '-';if(S_IXOTH & mode) *psz++ = 'x';else *psz++ = '-';*psz = '\0';return buf;}//back to the upper directorychar* backone(char *dir){char *temp = dir;int  len = strlen(dir);while(*(temp+len) != '/') {	*(temp+len) = '\0';	len--;	}*(temp+len) = '\0';return dir;}/* print the file in a  directory */void PrintContent(DIR *dp){struct 	dirent	*dirCont;while((dirCont = readdir(dp)) != NULL) {if((! strcmp(dirCont -> d_name, ".")) || (! strcmp(dirCont -> d_name, "..")))	continue;fprintf(stderr, "%s\t", dirCont -> d_name);}fprintf(stdout, "\n\n");}/* recursive a directory */void RecursiveDir(DIR *dp, char *psz){DIR 		*pdp;DIR 		*temp;int		len;struct dirent 	*dirpsz;struct stat	statbuffer;char 		fullpath[256];len = strlen(psz);*(psz + len) = '/';*(psz +	len + 1) = '\0';if((temp = opendir(psz)) == NULL) {	fprintf(stderr, "can't open %s", psz);	exit(1);	}/* print the file in the directory */PrintContent(temp);while((dirpsz = readdir(dp)) != NULL) {if(! strcmp(dirpsz -> d_name, ".") || ! strcmp(dirpsz -> d_name, ".."))	continue;memset(fullpath, 0, sizeof fullpath);strcpy(fullpath, psz);strcat(fullpath, dirpsz -> d_name);/* acquire the stat infomation about the file */ if(lstat(fullpath, &statbuffer)) {	fprintf(stderr, "can not acquire the stat infomation\n");	exit(1);	}/* judge the file whether is a directory */if(S_ISDIR(statbuffer.st_mode)) {	fprintf(stdout, "%s/\n", dirpsz -> d_name);	if((pdp = opendir(fullpath)) == NULL) {		fprintf(stderr, "can't open %s\n", dirpsz -> d_name);		exit(1);		}	RecursiveDir(pdp, fullpath);	closedir(pdp);	}}}//main functionint main(int argc, char **argv){DIR  	*dp;int	opt;int 	optsum 	= 0;int	flag 	= 1;int	dflag 	= 0;char 	*pstr;char 	*pmode;char 	psz[pathmax];char 	*pfile;char	mode[12];char	dirName[pathmax];struct 	stat statbuf;struct 	dirent *dirp;struct  passwd *puid;struct  group  *pgid;//get the option compositionif(argc == 1)	optsum = 0;else if(argc == 2 && argv[1][0] != '-')	optsum = 100;else if(argc == 2 && argv[1][0] == '-') {	while((opt = getopt(argc, argv, "liadR")) != -1) {		switch(opt){			case 'l':				optsum += 1;				break;			case 'i':				optsum += 2;				break;			case 'a':				optsum += 4;				break;			case 'R':				optsum += 8;				break;			case 'd':				optsum += 16;				break;			case '?':				fprintf(stderr, "%s", "can not recognize the option\n");                                exit(1);			}		}	}else if(argc == 3 && argv[1][0] == '-') {	flag = 2;	//the flag which indicate whether have the parameter 	while((opt = getopt(argc, argv, "liadR")) != -1) {		switch(opt){			case 'l':				optsum += 1;				break;			case 'i':				optsum += 2;				break;			case 'a':				optsum += 4;				break;			case 'R':				optsum += 8;				break;			case 'd':				optsum += 16;				break;			case '?':				fprintf(stderr, "%s", "can not recognize the option\n");                                exit(1);			}		}	}else {	fprintf(stderr, "%s", "the usage is wrong\n");	exit(1);}memset(dirName, 0, sizeof dirName);pstr = getcwd(dirName, pathmax);   //get the absolute path of the directoryif(flag == 1) {  //have no parameter	dflag = 1;	//the flag indicate whether the parameter is a directory	if((dp = opendir(pstr)) == NULL) {		fprintf(stderr, "can't open %s", pstr);		exit(1);		}	}else if(flag == 2) {	memset(psz, 0, sizeof psz);	pfile = psz;	strcpy(pfile, pstr);	int leng = strlen(pfile);	*(pfile + leng) = '/';		strcat(pfile, argv[2]);//judge the parameter is a absolute path or a relative path	if(lstat(pfile, &statbuf) < 0 && lstat(argv[2], &statbuf) < 0) {		fprintf(stderr, "%s", "Don't have the file or directory\n");		exit(1);		}	if(lstat(pfile, &statbuf) == 0) {		if(S_ISDIR(statbuf.st_mode)) {			dflag = 1;			if((dp = opendir(pfile)) == NULL) {				fprintf(stderr, "can't open %s", pfile);				exit(1);				}			strcpy(pstr, pfile);			}		}	else if(lstat(argv[2], &statbuf) == 0) {		if(S_ISDIR(statbuf.st_mode)) {			dflag = 1;			if((dp = opendir(argv[2])) == NULL) {				fprintf(stderr, "can't open %s", argv[2]);				exit(1);				}			strcpy(pstr, argv[2]);			}		}	}	switch(optsum) {	case 0:		while((dirp = readdir(dp)) != NULL)			if((strcmp(dirp->d_name, ".") != 0) && (strcmp(dirp->d_name, "..")) != 0)				fprintf(stdout, "%s\n", dirp->d_name);		break;	case 1:		if(dflag == 1) {//the parameter is a directory,and indicate the file by reading the directory			while((dirp = readdir(dp)) != NULL) {				memset(psz, 0, sizeof psz);				pfile = psz;				strcpy(pfile, pstr);				if((strcmp(dirp->d_name, "..")) == 0) {					pfile = backone(psz);					}				else if(strcmp(dirp->d_name, ".") != 0) {					int length = strlen(pfile);					*(pfile + length) = '/';						strcat(pfile, dirp->d_name);					}				if(lstat(pfile, &statbuf) < 0) {					fprintf(stderr, "can not acquire the stat infomation\n");					exit(1);					}//get the char format permision mode and user name, group name, then print them				pmode = GetPermisionMode(mode, statbuf.st_mode);				puid = getpwuid(statbuf.st_uid);				pgid = getgrgid(statbuf.st_gid);				fprintf(stdout, "%s\t%d\t%s\t%s\t%d\t%.24s\t%s\n", 						pmode, statbuf.st_nlink, puid -> pw_name, 						pgid -> gr_name, statbuf.st_size, 						ctime(&statbuf.st_atime), 							dirp -> d_name);				}			}//the parameter is a file		else if(dflag == 0) {			pmode = GetPermisionMode(mode, statbuf.st_mode);			puid = getpwuid(statbuf.st_uid);			pgid = getgrgid(statbuf.st_gid);			fprintf(stdout, "%s\t%d\t%s\t%s\t%d\t%.24s\t%s\n", 					pmode, statbuf.st_nlink, puid -> pw_name, 					pgid -> gr_name, statbuf.st_size, 					ctime(&statbuf.st_atime), 						argv[2]);			}		break;	case 2:		if(dflag == 1) {//the parameter is a directory,and indicate the file by reading the directory		while((dirp = readdir(dp)) != NULL) {			memset(psz, 0, sizeof psz);			pfile = psz;			strcpy(pfile, pstr);			if(strcmp(dirp->d_name, "..") == 0 || strcmp(dirp -> d_name, ".") ==0) {				continue;				}			else if(strcmp(dirp->d_name, ".") != 0) {				int length = strlen(pfile);				*(pfile + length) = '/';					strcat(pfile, dirp -> d_name);				}			if(lstat(pfile, &statbuf) < 0) {				fprintf(stderr, "can not acquire the stat infomation\n");				exit(1);				}			fprintf(stdout, "%d  %s\t", statbuf.st_ino, dirp -> d_name);			}		fprintf(stdout, "\n");		}		if(dflag == 0) {			if(lstat(argv[2], &statbuf) < 0) {			fprintf(stderr, "can not acquire the stat infomation\n");			exit(1);			}		fprintf(stdout, "%d  %s\n", statbuf.st_ino, argv[2]);		}			break;	case 4:		if(dflag == 1) {//the parameter is a directory,and indicate the file by reading the directory		while((dirp = readdir(dp)) != NULL) {			fprintf(stdout, "%s\t", dirp -> d_name);			}		fprintf(stdout, "\n");		}		if(dflag == 0) {			fprintf(stdout, "%d  %s\n", statbuf.st_ino, argv[2]);		}					break;	case 8:		if(flag == 1) {			fprintf(stdout, ".:\n");			RecursiveDir(dp, pstr);			break;		}		if(flag == 2 && dflag == 1) { //the command have parameter and the parameter is a 									directory			fprintf(stdout, "%s\n", pstr);			RecursiveDir(dp, pstr);			break;		}		if(flag == 2 && dflag == 0) { //the command have parameter and the parameter is't 									a directory			fprintf(stdout, "%s\n", argv[2]);			break;		}	case 16:		if(flag == 1)			fprintf(stdout, ".\n");		else			fprintf(stdout, "%s\n", argv[2]);		break;	case 100:		fprintf(stdout, "%s\n", argv[1]);		break;	}return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -