Linux下的ls命令

#include <sys/stat.h>
#include <dirent.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include<shadow.h>
#define LS 1
#define LL 2
#define LA 3

void print(char *str, int tag);            //如果用户执行的ll该怎么进行,将ls的可执行文档复制一份改名为ll?                                    

int main(int argc, char*argv[])
{	
	char buf[256] = {0};
	getcwd(buf, 256);
	if (argc == 1)
	{
		print(buf, LS);
	}
	else
	{
		if (!(strncmp(argv[1], "-l", 2)))            // ls -l
		{
			if (argc > 2)
			{
			//	printf("ls -l out\n");
				print(argv[2], LL); 
			}
			else
			{
			//	printf("ls -l\n");
				print(buf, LL);//ls -l 
			}
		}
		else if (!(strncmp(argv[1], "-a", 2)))
		{
			if (argc > 2)
			{
			//	printf("ls -a out\n"); // ls -a out
				print(argv[2], LA); 
			}
			else
			{
			//	printf("ls -a\n");
				print(buf, LA); //ls -a 
			}
		}
		else
		{
		//	printf("ls out  ");
			print(argv[1], LS); //ls out
		}
	}
}

void print(char *str, int tag)
{
	DIR *dir = opendir(str);
	if (dir == NULL)
	{
		printf("cannot open %s: no such file or directory\n", str);
		exit(0);
	}


	struct dirent *pre = NULL;
	struct stat st;

	if (tag == LS || tag == LA) //ls            ls -a
	{
		while((pre = readdir(dir)) != NULL)
		{
		 	lstat(pre->d_name, &st);
			if (tag == LS)
			{
				if (!(strncmp(pre->d_name, ".", 1)))
				{
					continue;
				}
			}
			if (S_ISDIR(st.st_mode))
			{
				printf("\033[34m%s\033[30m   ", pre->d_name);
			}
			else if (st.st_mode & S_IXOTH || st.st_mode & S_IXGRP || st.st_mode &S_IXUSR)
			{
				printf("\033[32m%s\033[30m   ",pre->d_name);
			}
		}
	printf("\n");
	}
	else// ll
	{
		while ((pre = readdir(dir)) != NULL)
		{
			lstat(pre->d_name, &st);
			
			if (!(strncmp(pre->d_name, ".", 1)))
			{
				continue;
			}
			if (S_ISDIR(st.st_mode))
			{
				char user[127] = {0};
				//long st_uid = st.st_uid;//get user
				//long st_gid = st.st.gid;//get owns
                                printf("\033[32m%ld %ld %ld  %s \033[30m\n", st.st_nlink, st.st_uid, st.st_gid, pre->d_name);
			}		
			else if (st.st_mode & S_IXOTH || st.st_mode & S_IXGRP || st.st_mode &S_IXUSR)
			{
			
                                printf("\033[34m%ld %ld %ld  %s\033[30m\n", st.st_nlink, st.st_uid, st.st_gid, pre->d_name);
			}
		} 
	}
	closedir(dir);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值