C - Modify the fsize program to print the other information contained in the inode entry

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

/*
 * dirent.h - by FreeMan
 */

#define NAME_MAX 14 /* longest filename component; */

/* system-dependent */
typedef struct { /* portable directory entry */
	long ino; /* inode number */
	char name[NAME_MAX + 1]; /* name + '\0' terminator */
} Dirent;

typedef struct { /* minimal DIR: no buffering, etc. */
	int fd; /* file descriptor for the directory */
	Dirent d; /* the directory entry */
} DIR;

DIR *opendir(char *dirname);
Dirent *readdir(DIR *dfd);
void closedir(DIR *dfd);

/*
 * Modify the fsize program to print the other information contained in the inode entry.
 *
 * FSize.c - by FreeMan
 */

#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "dirent.h"
#include <string.h>
#include <time.h>

void fsize(char *);

int main(int argc, char *argv[])
{
	if (argc == 1)
	{
		fsize(".");
	}
	else
	{
		while (--argc > 0)
		{
			fsize(*++argv);
		}
	}
	return 0;
}

void dirwalk(char *, void (*fcn)(char *));

void fsize(char *name)
{
	struct stat stbuf;

	if (stat(name, &stbuf) == -1)
	{
		fprintf(stderr, "fsize: can't access %s\n", name);
		return;
	}
	if ((stbuf.st_mode & _S_IFMT) == _S_IFDIR)
	{
		dirwalk(name, fsize);
	}
	printf("%8ld %s\n", stbuf.st_size, name);
}

#define MAX_PATH 1024

void dirwalk(char *dir, void (*fcn)(char *))
{
	char name[MAX_PATH];
	Dirent *dp;
	DIR *dfd;

	if ((dfd = opendir(dir)) == NULL)
	{
		fprintf(stderr, "dirwalk: can't open %s\n", dir);
		return;
	}
	while ((dp = readdir(dfd)) != NULL)
	{
		if (strcmp(dp->name, ".") == 0 || strcmp(dp->name, "..") == 0)
		{
			continue;
		}
		if (strlen(dir) + strlen(dp->name) + 2 > sizeof(name))
		{
			fprintf(stderr, "dirwalkd: name %s/%s too long\n", dir, dp->name);
		}
		else
		{
			sprintf(name, "%s/%s", dir, dp->name);
			(*fcn)(name);
		}
	}
	closedir(dfd);
}

#ifndef DIRSIZ
#define DIRSIZ 14
#endif

struct direct
{
	ino_t d_ino;
	char d_name[DIRSIZ];
};

int fstat(int fd, struct stat *);

DIR *opendir(char *dirname)
{
	int fd;
	struct stat stbuf;
	DIR *dp;

	if ((fd = open(dirname, O_RDONLY, 0)) == -1 ||
		fstat(fd, &stbuf) == -1 ||
		(stbuf.st_mode & _S_IFMT) != _S_IFDIR ||
		(dp = (DIR *)malloc(sizeof(DIR))) == NULL)
	{
		return NULL;
	}
	dp->fd = fd;
	return dp;
}

void closedir(DIR *dp)
{
	if (dp)
	{
		close(dp->fd);
		free(dp);
	}
}

Dirent *readdir(DIR *dp)
{
	struct direct dirbuf;
	static Dirent d;

	while (read(dp->fd, (char *)&dirbuf, sizeof(dirbuf)) == sizeof(dirbuf))
	{
		if (dirbuf.d_ino == 0)
		{
			continue;
		}
		d.ino = dirbuf.d_ino;
		strncpy(d.name, dirbuf.d_name, DIRSIZ);
		d.name[DIRSIZ] = '\0';
		return &d;
	}
	return NULL;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值