shithub: npe

ref: 10d8d4ff5dad3dc3a62eefba9d6abfa8243fb348
dir: /libnpe/opendir.c/

View raw version
#include <dirent.h>
#include "_dirent.h"

DIR *
opendir(char *name)
{
	Dir *dir;
	DIR *d;
	int fd;

	dir = nil;
	d = nil;
	if((fd = open(name, OREAD|OCEXEC)) >= 0 &&
	    (dir = dirfstat(fd)) != nil &&
	    (dir->qid.type & QTDIR) != 0 &&
	    (d = calloc(1, sizeof(*d))) != nil){
		d->fd = fd;
	}

	free(dir);
	if(d == nil)
		close(fd);

	return d;
}