【Linux C】lstat获取文件属性

int lstat(const char *path, struct stat *buf);

lstat函数获取文件的所有属性,从stat结构体中获取相应信息。

函数具体相关,可使用 man lstat  查看

示例用到lstat函数,将模仿ls命令。遍历输出文件类型、文件权限(用户权限、用户组权限、其他用户权限)、访问时间、文件名。

编辑

myls.c

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, const char *argv[])
{
	struct stat buf;
	int n;
	struct tm *tp;

	if (argc < 2)
	{
		printf(" Usage:%s <file>\n",argv[0]);
		return -1;
	}

	if (lstat(argv[1],&buf) < 0)
	{
		 perror("lstat");
		 return -1;
	}

	switch (buf.st_mode & S_IFMT)
	{
		case S_IFREG:
			printf("-");
			break;
			
		case S_IFDIR:
			printf("d");
			break;
	}

	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("%lu",buf.st_size);
	tp = localtime(&buf.st_mtime);
	printf(" %d-%02d-%02d", tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday);
	printf(" %s\n",argv[1]);

	return 0;
}

编译

gcc -o myls myls.c

输出

./myls myls.c

猜你喜欢

转载自blog.csdn.net/feit2417/article/details/82107259