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

📄 ls.txt

📁 Linux环境下的简单的shell编程 用C模拟
💻 TXT
字号:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include "ourhdr.h"
#include <time.h>
char p1[10];
char p3[50];
int flag=0;
char p2[100];
char state[10];

void printdir(char *dir, int f)
{
	DIR *dp;
	struct dirent *dirp;
	struct stat statbuf;

	if ( (dp = opendir(dir)) == NULL)
	{
		printf("can't open %s\n", dir);
		exit(0);
	}	

	switch (f)
	{
		case 0:	
			chdir(dir);
			while ( (dirp = readdir(dp)) != NULL)
			{
				lstat(dirp->d_name, &statbuf);
				strcpy(p2, dirp->d_name);
				if (p2[0] == '.')
					continue;
				
				if (S_ISDIR(statbuf.st_mode))
				{					
					printf("%s/\n", dirp->d_name);			
				}		
				else printf("%s\n", dirp->d_name);
			}		
			break;

		case 1:
			chdir(dir);
			while ( (dirp = readdir(dp)) != NULL)
			{
				lstat(dirp->d_name, &statbuf);
				strcpy(p2, dirp->d_name);
				if (p2[0] == '.')
					continue;
				if (S_ISDIR(statbuf.st_mode))
				{					
					printf("%d %s/\n", dirp->d_ino, dirp->d_name);			
				}		
				else printf("%d %s\n", dirp->d_ino, dirp->d_name);
			}		
			break;


		case 2:
			chdir(dir);
			while ( (dirp = readdir(dp)) != NULL)
			{
				lstat(dirp->d_name, &statbuf);
				strcpy(p2, dirp->d_name);
				if (strcmp(".", dirp->d_name) == 0 ||
					   strcmp("..", dirp->d_name) == 0 ||
					    p2[0] == '.')
						continue;
				char state[10] = "----------";
				if (S_ISDIR(statbuf.st_mode))
				{
					state[0] = 'd';
				}
				if (S_IRUSR&statbuf.st_mode)
				{
					state[1] = 'r';
				}
				if (S_IWUSR&statbuf.st_mode)
				{
					state[2] = 'w';
				}
				if (S_IXUSR&statbuf.st_mode)
				{
					state[3] = 'x';
				}
				if (S_IRGRP&statbuf.st_mode)
				{
					state[4] = 'r';
				}
				if (S_IWGRP&statbuf.st_mode)
				{
					state[5] = 'w';
				}
				if (S_IXGRP&statbuf.st_mode)
				{
					state[6] = 'x';
				}
				if (S_IROTH&statbuf.st_mode)
				{
					state[7] = 'r';
				}
				if (S_IWOTH&statbuf.st_mode)
				{
					state[8] = 'w';
				}
				if (S_IXOTH&statbuf.st_mode)
				{
					state[9] = 'x';
				}


				if (S_ISDIR(statbuf.st_mode))
				{					
					printf("%s %d %d %d %d %s/ %s ", state, statbuf.st_nlink, statbuf.st_uid,
 statbuf.st_gid, statbuf.st_size, dirp->d_name, ctime(&statbuf.st_mtime));			

				}		
				else printf("%s %d %d %d %d %s %s ", state, statbuf.st_nlink, statbuf.st_u
id, statbuf.st_gid, statbuf.st_size, dirp->d_name, ctime(&statbuf.st_mtime));


			}
			break;

		case 4:
			chdir(dir);
			while ( (dirp = readdir(dp)) != NULL)
			{
				lstat(dirp->d_name, &statbuf);
				strcpy(p2, dirp->d_name);
				if (S_ISDIR(statbuf.st_mode))
				{
					if (strcmp(".", dirp->d_name) == 0 ||
					   strcmp("..", dirp->d_name) == 0 ||
			  		    p2[0] == '.')
						continue;
		
					printf("%s/\n", dirp->d_name);
					printdir(dirp->d_name, flag);
				}
				else printf("%s\n", dirp->d_name);
			}

			chdir("..");
			break;
		case 8:
			printf("%s\n", dir);
			break;
		case 16:
			chdir(dir);
			while ( (dirp = readdir(dp)) != NULL)
			{
				lstat(dirp->d_name, &statbuf);
				strcpy(p2, dirp->d_name);
				if (S_ISDIR(statbuf.st_mode))
				{
					if (strcmp(".", dirp->d_name) == 0 ||
					   strcmp("..", dirp->d_name) == 0)
						continue;
		
					printf("%s/\n", dirp->d_name);

				}
				else printf("%s\n", dirp->d_name);
			}		

			
			break;
		default:
			break;
	}

	closedir(dp);
}


int main(int argc, char *argv[])
{
	if (argv[1] != NULL)
	{
		strcpy(p1, argv[1]);
		int i;
		for (i = 1; i < 9; i++)
		{
			switch (p1[i])
			{
				case 'i':
					flag = flag+1;
					break;
				case 'l':
					flag = flag+2;
					break;
				case 'R':
					flag = flag+4;
					break;
				case 'd':
					flag = flag+8;
					break;
				case 'a':
					flag = flag+16;
					break;
				default:
					break;
			}
		}
		if (p1[0] != '-')
		{	
			strcpy(p3, p1);
			printdir(p3, flag);
			exit(0);
		}	
		if (argv[2] != NULL)
		{
			strcpy(p3, argv[2]);
			printdir(p3, flag);
			exit(0);
		}
	}	

	printdir("/home/fym", flag);
	exit(0);
}

⌨️ 快捷键说明

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