reading directory under SYSTEM V
Don Steiny
steiny at scc.UUCP
Sun Sep 7 04:25:59 AEST 1986
**
In my source for netnews there were some routines that do the trick.
They say for V7, but I have used them on several system V systems.
-----
: This is a shar archieve. Extract with sh, not csh.
: The rest of this file will extract:
: ndir.c ndir.h
echo extracting - ndir.c
sed 's/^X//' > ndir.c << '~FUNKY STUFF~'
X#include "defs.h"
X#if !defined(BSD4_2) && !defined(BSD4_1C)
X#include <sys/param.h>
X#include "ndir.h"
X
X#ifdef SCCSID
Xstatic char *SccsId = "@(#)ndir.c 1.8 4/26/85";
X#endif /* SCCSID */
X
X/*
X * support for Berkeley directory reading routine on a V7 file system
X */
X
Xextern char *malloc();
X
X/*
X * open a directory.
X */
XDIR *
Xopendir(name)
Xchar *name;
X{
X register DIR *dirp;
X register int fd;
X
X if ((fd = open(name, 0)) == -1)
X return NULL;
X if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
X close (fd);
X return NULL;
X }
X dirp->dd_fd = fd;
X dirp->dd_loc = 0;
X return dirp;
X}
X
X/*
X * read an old style directory entry and present it as a new one
X */
X#ifdef pyr
X/* Pyramid in the AT&T universe */
X#define ODIRSIZ 248
Xstruct olddirect {
X long od_ino;
X short od_fill1, od_fill2;
X char od_name[ODIRSIZ];
X};
X#else /* V7 file system */
X#define ODIRSIZ 14
X
Xstruct olddirect {
X short od_ino;
X char od_name[ODIRSIZ];
X};
X#endif /* V7 */
X
X/*
X * get next entry in a directory.
X */
Xstruct direct *
Xreaddir(dirp)
Xregister DIR *dirp;
X{
X register struct olddirect *dp;
X static struct direct dir;
X
X for (;;) {
X if (dirp->dd_loc == 0) {
X dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
X DIRBLKSIZ);
X if (dirp->dd_size <= 0)
X return NULL;
X }
X if (dirp->dd_loc >= dirp->dd_size) {
X dirp->dd_loc = 0;
X continue;
X }
X dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
X dirp->dd_loc += sizeof(struct olddirect);
X if (dp->od_ino == 0)
X continue;
X dir.d_ino = dp->od_ino;
X strncpy(dir.d_name, dp->od_name, ODIRSIZ);
X dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
X dir.d_namlen = strlen(dir.d_name);
X dir.d_reclen = DIRSIZ(&dir);
X return (&dir);
X }
X}
X
X/*
X * close a directory.
X */
Xvoid
Xclosedir(dirp)
Xregister DIR *dirp;
X{
X close(dirp->dd_fd);
X dirp->dd_fd = -1;
X dirp->dd_loc = 0;
X free((char *)dirp);
X}
X#endif /* !BSD4_2 && !BSD4_1C */
~FUNKY STUFF~
echo extracting - ndir.h
sed 's/^X//' > ndir.h << '~FUNKY STUFF~'
X/* @(#)ndir.h 1.4 4/16/85 */
X#ifndef DEV_BSIZE
X#define DEV_BSIZE 512
X#endif
X#define DIRBLKSIZ DEV_BSIZE
X#define MAXNAMLEN 255
X
Xstruct direct {
X long d_ino; /* inode number of entry */
X short d_reclen; /* length of this record */
X short d_namlen; /* length of string in d_name */
X char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */
X};
X
X/*
X * The DIRSIZ macro gives the minimum record length which will hold
X * the directory entry. This requires the amount of space in struct direct
X * without the d_name field, plus enough space for the name with a terminating
X * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
X */
X
X#ifdef DIRSIZ
X#undef DIRSIZ
X#endif /* DIRSIZ */
X#define DIRSIZ(dp) \
X ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
X
X/*
X * Definitions for library routines operating on directories.
X */
Xtypedef struct _dirdesc {
X int dd_fd;
X long dd_loc;
X long dd_size;
X char dd_buf[DIRBLKSIZ];
X} DIR;
X#ifndef NULL
X#define NULL 0
X#endif
Xextern DIR *opendir();
Xextern struct direct *readdir();
Xextern void closedir();
~FUNKY STUFF~
exit 0;
--
scc!steiny
Don Steiny @ Don Steiny Software
109 Torrey Pine Terrace
Santa Cruz, Calif. 95060
(408) 425-0382
More information about the Comp.lang.c
mailing list