[net.sources] ndir

ecn-pa:ecn-pb:rick (10/27/82)

sed 's/^	//' >_ <<\*-*-END-*-*
	sed 's/^	//' >_ <<\*-*-END-*-*
*-*-END-*-*
sed 's/^	//' >closedir.c <<\*-*-END-*-*
	/* Copyright (c) 1982 Regents of the University of California */
	
	static char sccsid[] = "@(#)closedir.c 4.2 3/10/82";
	
	#include <sys/types.h>
	#include <ndir.h>
	
	/*
	 * close a directory.
	 */
	void
	closedir(dirp)
		register DIR *dirp;
	{
		close(dirp->dd_fd);
		dirp->dd_fd = -1;
		dirp->dd_loc = 0;
		free(dirp);
	}
*-*-END-*-*
sed 's/^	//' >ndir.h <<\*-*-END-*-*
	/* Copyright (c) 1982 Regents of the University of California */
	
	/* @(#)ndir.h 4.4 3/30/82 */
	
	/*
	 * This sets the "page size" for directories.
	 * Requirements are DEV_BSIZE <= DIRBLKSIZ <= MINBSIZE with
	 * DIRBLKSIZ a power of two.
	 * Dennis Ritchie feels that directory pages should be atomic
	 * operations to the disk, so we use DEV_BSIZE.
	 */
	#define DIRBLKSIZ 512
	
	/*
	 * This limits the directory name length. Its main constraint
	 * is that it appears twice in the user structure. (u. area)
	 */
	#define MAXNAMLEN 255
	
	struct	direct {
		u_long	d_ino;
		short	d_reclen;
		short	d_namlen;
		char	d_name[MAXNAMLEN + 1];
		/* typically shorter */
	};
	
	struct _dirdesc {
		int	dd_fd;
		long	dd_loc;
		long	dd_size;
		char	dd_buf[DIRBLKSIZ];
	};
	
	/*
	 * useful macros.
	 */
	#undef DIRSIZ
	#define DIRSIZ(dp) \
	    ((sizeof(struct direct) - MAXNAMLEN + (dp)->d_namlen + sizeof(ino_t) - 1) &\
	    ~(sizeof(ino_t) - 1))
	typedef	struct _dirdesc DIR;
	#ifndef	NULL
	#define	NULL	0
	#endif
	
	/*
	 * functions defined on directories
	 */
	extern DIR *opendir();
	extern struct direct *readdir();
	extern long telldir();
	extern void seekdir();
	#define rewinddir(dirp)	seekdir((dirp), 0)
	extern void closedir();
*-*-END-*-*
sed 's/^	//' >opendir.c <<\*-*-END-*-*
	/* Copyright (c) 1982 Regents of the University of California */
	
	static char sccsid[] = "@(#)opendir.c 4.2 3/10/82";
	
	#include <sys/types.h>
	#include <sys/stat.h>
	#include <ndir.h>
	
	/*
	 * open a directory.
	 */
	DIR *
	opendir(name)
		char *name;
	{
		register DIR *dirp;
		struct stat sbuf;
	
		dirp = (DIR *)malloc(sizeof(DIR));
		dirp->dd_fd = open(name, 0);
		if (dirp->dd_fd == -1) {
			free(dirp);
			return NULL;
		}
		fstat(dirp->dd_fd, &sbuf);
		if ((sbuf.st_mode & S_IFDIR) == 0) {
			free(dirp);
			return NULL;
		}
		dirp->dd_loc = 0;
		return dirp;
	}
*-*-END-*-*
sed 's/^	//' >readdir.c <<\*-*-END-*-*
	/* Copyright (c) 1982 Regents of the University of California */
	
	static char sccsid[] = "@(#)readdir.c 4.2 3/12/82";
	
	#include <sys/types.h>
	#include <ndir.h>
	
	/*
	 * read an old stlye directory entry and present it as a new one
	 */
	#define	ODIRSIZ	14
	
	struct	olddirect {
		ino_t	od_ino;
		char	od_name[ODIRSIZ];
	};
	
	/*
	 * get next entry in a directory.
	 */
	struct direct *
	readdir(dirp)
		register DIR *dirp;
	{
		register struct olddirect *dp;
		static struct direct dir;
	
		for (;;) {
			if (dirp->dd_loc == 0) {
				dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 
				    DIRBLKSIZ);
				if (dirp->dd_size <= 0)
					return NULL;
			}
			if (dirp->dd_loc >= dirp->dd_size) {
				dirp->dd_loc = 0;
				continue;
			}
			dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
			dirp->dd_loc += sizeof(struct olddirect);
			if (dp->od_ino == 0)
				continue;
			dir.od_ino = dp->d_ino;
			strncpy(dir.d_name, dp->od_name, ODIRSIZ);
			dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
			dir.d_namlen = strlen(dir.d_name);
			dir.d_reclen = DIRSIZ(&dir);
			return (&dir);
		}
	}
*-*-END-*-*
sed 's/^	//' >seekdir.c <<\*-*-END-*-*
	/* Copyright (c) 1982 Regents of the University of California */
	
	static char sccsid[] = "@(#)seekdir.c 4.3 2/25/82";
	
	#include <sys/types.h>
	#include <ndir.h>
	
	/*
	 * seek to an entry in a directory.
	 * Only values returned by ``telldir'' should be passed to seekdir.
	 */
	void
	seekdir(dirp, loc)
		register DIR *dirp;
		long loc;
	{
		long curloc, base, offset;
		struct direct *dp;
	
		curloc = telldir(dirp);
		if (loc == curloc)
			return;
		base = loc & ~(DIRBLKSIZ - 1);
		offset = loc & (DIRBLKSIZ - 1);
		if (dirp->dd_loc != 0 && (curloc & ~(DIRBLKSIZ - 1)) == base) {
			dirp->dd_loc = offset;
			return;
		}
		lseek(dirp->dd_fd, base, 0);
		dirp->dd_loc = 0;
		while (dirp->dd_loc < offset) {
			dp = readdir(dirp);
			if (dp == NULL)
				return;
		}
	}
*-*-END-*-*
sed 's/^	//' >telldir.c <<\*-*-END-*-*
	/* Copyright (c) 1982 Regents of the University of California */
	
	static char sccsid[] = "@(#)telldir.c 4.1 2/21/82";
	
	#include <sys/types.h>
	#include <ndir.h>
	
	/*
	 * return a pointer into a directory
	 */
	long
	telldir(dirp)
		DIR *dirp;
	{
		return (lseek(dirp->dd_fd, 0L, 1) - dirp->dd_size + dirp->dd_loc);
	}
*-*-END-*-*