[net.sources] new flexable length directory access routines

ARPAVAX:mckusick (08/02/82)

The purpose of this library is to simulate the new flexable length
directory names on top of the old directory structure. It allows
programs to be converted to the new directory access interface, so
that they need only be relinked when 4.2bsd becomes available.

The following files should be placed in /usr/src/lib/libndir; then run
``make'' followed by ``make install''. The manual page is available as
``man directory''. The library is accessed by specifying "-lndir" as the
last argument to the compile line. (eg ``cc -o foo foo.c -lndir'')
Report problems to mckusick@berkeley or ucbvax!mckusick.
::::::::::::::::::::
Makefile
::::::::::::::::::::
# @(#)Makefile	4.2 (Berkeley) 8/2/82

DESTDIR	=
CFLAGS=	-O

OBJS=	closedir.o opendir.o readdir.o seekdir.o telldir.o

.c.o:
	${CC} ${CFLAGS} -c $*.c
	-ld -x -r $*.o
	mv a.out $*.o

libndir: ${DESTDIR}/usr/include/ndir.h ${OBJS}
	ar cru libndir ${OBJS}
	ranlib libndir

${DESTDIR}/usr/include/ndir.h: ndir.h
	cp ndir.h ${DESTDIR}/usr/include/ndir.h

install: libndir
	cp libndir ${DESTDIR}/usr/lib/libndir.a
	ranlib ${DESTDIR}/usr/lib/libndir.a
	cp directory.3 /usr/man/man3/directory.3

clean:
	rm -f libndir ${OBJS}
::::::::::::::::::::
ndir.h
::::::::::::::::::::
/* 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();
::::::::::::::::::::
closedir.c
::::::::::::::::::::
/* 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);
}
::::::::::::::::::::
opendir.c
::::::::::::::::::::
/* 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;
}
::::::::::::::::::::
readdir.c
::::::::::::::::::::
/* 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	d_ino;
	char	d_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->d_ino == 0)
			continue;
		dir.d_ino = dp->d_ino;
		strncpy(dir.d_name, dp->d_name, ODIRSIZ);
		dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
		dir.d_namlen = strlen(dir.d_name);
		dir.d_reclen = DIRSIZ(&dir);
		return (&dir);
	}
}
::::::::::::::::::::
seekdir.c
::::::::::::::::::::
/* 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;
	}
}
::::::::::::::::::::
telldir.c
::::::::::::::::::::
/* 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);
}
::::::::::::::::::::
directory.3
::::::::::::::::::::
.TH DIRECTORY 3  3/1/82
.UC 4
.SH NAME
opendir, readdir, telldir, seekdir, rewinddir, closedir \- directory operations
.SH SYNOPSIS
.B #include <ndir.h>
.PP
.SM
.B DIR
.B *opendir(filename)
.br
.B char *filename;
.PP
.SM
.B struct direct
.B *readdir(dirp)
.br
.B DIR *dirp;
.PP
.SM
.B long
.B telldir(dirp)
.br
.B DIR *dirp;
.PP
.SM
.B seekdir(dirp, loc)
.br
.B DIR *dirp;
.br
.B long loc;
.PP
.SM
.B rewinddir(dirp)
.br
.B DIR *dirp;
.PP
.SM
.B closedir(dirp)
.br
.B DIR *dirp;
.SH DESCRIPTION
.I Opendir
opens the directory named by
.I filename
and associates a
.I directory stream
with it.
.I Opendir
returns a pointer to be used to identify the
.I directory stream
in subsequent operations.
The pointer
.SM
.B NULL
is returned if
.I filename
cannot be accessed or is not a directory.
.PP
.I Readdir
returns a pointer to the next directory entry.
It returns
.B NULL
upon reaching the end of the directory or detecting
an invalid
.I seekdir
operation.
.PP
.I Telldir
returns the current location associated with the named
.I directory stream.
.PP
.I Seekdir
sets the position of the next
.I readdir
operation on the
.I directory stream.
The new position reverts to the one associated with the
.I directory stream
when the
.I telldir
operation was performed.
Values returned by
.I telldir
are good only for the lifetime of the DIR pointer from 
which they are derived.
If the directory is closed and then reopened, 
the 
.I telldir
value may be invalidated
due to undetected directory compaction.
It is safe to use a previous
.I telldir
value immediately after a call to
.I opendir
and before any calls to
.I readdir.
.PP
.I Rewinddir
resets the position of the named
.I directory stream
to the beginning of the directory.
.PP
.I Closedir
causes the named
.I directory stream
to be closed,
and the structure associated with the DIR pointer to be freed.
.PP
The preferred way to search the current directory for entry ``name'' is:
.br
	len = strlen(name);
.br
	dirp = opendir(".");
.br
	for (dp = readdir(dirp); dp != NULL; dp = readdir(dir))
.br
		if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
.br
			closedir(dirp);
.br
			return FOUND;
.br
		}
.br
	closedir(dirp);
.br
	return NOT_FOUND;
.SH "SEE ALSO"
open(2),
close(2),
read(2),
lseek(2)
::::::::::::::::::::
end-of-files
::::::::::::::::::::