dirfstat.c

来自「这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易」· C语言 代码 · 共 38 行

C
38
字号
#include <u.h>#include <libc.h>#include <fcall.h>enum{	DIRSIZE	= STATFIXLEN + 16 * 4		/* enough for encoded stat buf + some reasonable strings */};Dir*dirfstat(int fd){	Dir *d;	uchar *buf;	int n, nd, i;	nd = DIRSIZE;	for(i=0; i<2; i++){	/* should work by the second try */		d = malloc(sizeof(Dir) + BIT16SZ + nd);		if(d == nil)			return nil;		buf = (uchar*)&d[1];		n = fstat(fd, buf, BIT16SZ+nd);		if(n < BIT16SZ){			free(d);			return nil;		}		nd = GBIT16(buf);	/* upper bound on size of Dir + strings */		if(nd <= n){			convM2D(buf, n, d, (char*)&d[1]);			return d;		}		/* else sizeof(Dir)+BIT16SZ+nd is plenty */		free(d);	}	return nil;}

⌨️ 快捷键说明

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