简单实现ls命令

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

int aflag, lflag;

typedef struct _node_
{
	char fname[256];
	struct _node_ *next;
} linknode, *linklist;

void display_file(char *path, char *fname)
{
	struct stat buf;
	struct tm *tp;

	if ( ! lflag )
	{
		printf("%s  ", fname);
		return;
	}

	if (lstat(path, &buf) < 0)
	{
		perror("fail to lstat");
		return ;
	}

	switch (buf.st_mode & S_IFMT)
	{
	case S_IFSOCK :
		printf("s");
		break;
	case S_IFLNK :
		printf("l");
		break;
	case S_IFREG :
		printf("-");
		break;
	case S_IFBLK :
		printf("b");
		break;
	case S_IFDIR :
		printf("d");
		break;
	case S_IFCHR :
		printf("c");
		break;
	case S_IFIFO :
		printf("p");
		break;
	}

	int n;
	for (n=8; n>=0; n--)
	{
		if (buf.st_mode & (1 << n))
		{
			switch (n % 3)
			{
			case 2 :
				printf("r");
				break;
			case 1 :
				printf("w");
				break;
			case 0 :
				printf("x");
				break;
			}
		}
		else
		{
			printf("-");
		}
	}

	printf("%3d", buf.st_nlink);
	printf(" %s", getpwuid(buf.st_uid)->pw_name);
	printf(" %s", getgrgid(buf.st_gid)->gr_name);
	printf("%8ld", buf.st_size);
	tp = localtime(&buf.st_mtime);
	printf(" %d", tp->tm_year+1900);
	if (tp->tm_mon < 9) printf("-0%d", tp->tm_mon+1);
	else printf("-%d", tp->tm_mon+1);
	if (tp->tm_mday < 10) printf("-0%d ", tp->tm_mday);
	else printf("-%d ", tp->tm_mday);
	if (tp->tm_min < 10) printf("0%d", tp->tm_min);
	else printf("%d", tp->tm_min);
	if (tp->tm_sec < 10) printf(":0%d ", tp->tm_sec);
	else printf(":%d ", tp->tm_sec);

	printf("%s\n", fname);

	return;
}

void display_dir(char *dirname)
{
	DIR *mydir;
	struct dirent *myitem;
	char path[256];
	linklist h, p, q;

	h = (linklist)malloc(sizeof(linknode));
	h->next = NULL;
	mydir = opendir(dirname);
	while((myitem = readdir(mydir)) != NULL)
	{
		if ((myitem->d_name[0] == '.') && !aflag)  continue;
		p = h;
		while ((p->next != NULL) && (strcmp(p->next->fname, myitem->d_name) < 0)) p = p->next;
		q = (linklist)malloc(sizeof(linknode));
		strcpy(q->fname, myitem->d_name);
		q->next = p->next;
		p->next = q;
	}
	p = h->next;
	while ( p )
	{
		sprintf(path, "%s/%s", dirname, p->fname);
		display_file(path, p->fname);
		p = p->next;
	}

	return;
}

int main(int argc, char *argv[])
{
	struct stat buf;
	int i, ch;

	opterr = 0;
	while ((ch = getopt(argc, argv, "la")) != EOF)
	{
		switch ( ch )
		{
			case 'a' : aflag = 1; break;
			case 'l' : lflag = 1; break;
			default  : printf("wrong option %d is set\n", optopt); return -1;
		}
	}

	if (optind == argc)
	{
		display_dir(".");
		if ( ! lflag ) printf("\n");
		return 0;
	}

	for (i=optind; i<argc; i++)
	{
		if (lstat(argv[i], &buf) < 0)
		{
			perror("fail to lstat");
			return -1;
		}
	
		if ( S_ISDIR(buf.st_mode) )
		{
			printf("%s:\n", argv[i]);
			display_dir(argv[i]);
		}
		else
		{
			display_file(argv[i], argv[i]);
		}
	}

	return 0;
}

运行:./a.out -la   ./a.out / 

shell命令其实也是调用系统函数。这些系统函数调用好的话你也可以实现shell命令。。敬请参考《UNIX环境高级编程2》,他可以让你深刻了解linux系统。随手可以自己去写shell命令。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值