/*
|
/*
|
* opendir.c --
|
* opendir.c --
|
*
|
*
|
* This file provides dirent-style directory-reading procedures
|
* This file provides dirent-style directory-reading procedures
|
* for V7 Unix systems that don't have such procedures. The
|
* for V7 Unix systems that don't have such procedures. The
|
* origin of this code is unclear, but it seems to have come
|
* origin of this code is unclear, but it seems to have come
|
* originally from Larry Wall.
|
* originally from Larry Wall.
|
*
|
*
|
*
|
*
|
* RCS: @(#) $Id: opendir.c,v 1.1.1.1 2002-01-16 10:25:23 markom Exp $
|
* RCS: @(#) $Id: opendir.c,v 1.1.1.1 2002-01-16 10:25:23 markom Exp $
|
*/
|
*/
|
|
|
#include "tclInt.h"
|
#include "tclInt.h"
|
#include "tclPort.h"
|
#include "tclPort.h"
|
|
|
#undef DIRSIZ
|
#undef DIRSIZ
|
#define DIRSIZ(dp) \
|
#define DIRSIZ(dp) \
|
((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
|
((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
|
|
|
/*
|
/*
|
* open a directory.
|
* open a directory.
|
*/
|
*/
|
DIR *
|
DIR *
|
opendir(name)
|
opendir(name)
|
char *name;
|
char *name;
|
{
|
{
|
register DIR *dirp;
|
register DIR *dirp;
|
register int fd;
|
register int fd;
|
char *myname;
|
char *myname;
|
|
|
myname = ((*name == '\0') ? "." : name);
|
myname = ((*name == '\0') ? "." : name);
|
if ((fd = open(myname, 0, 0)) == -1)
|
if ((fd = open(myname, 0, 0)) == -1)
|
return NULL;
|
return NULL;
|
if ((dirp = (DIR *)ckalloc(sizeof(DIR))) == NULL) {
|
if ((dirp = (DIR *)ckalloc(sizeof(DIR))) == NULL) {
|
close (fd);
|
close (fd);
|
return NULL;
|
return NULL;
|
}
|
}
|
dirp->dd_fd = fd;
|
dirp->dd_fd = fd;
|
dirp->dd_loc = 0;
|
dirp->dd_loc = 0;
|
return dirp;
|
return dirp;
|
}
|
}
|
|
|
/*
|
/*
|
* read an old style directory entry and present it as a new one
|
* read an old style directory entry and present it as a new one
|
*/
|
*/
|
#ifndef pyr
|
#ifndef pyr
|
#define ODIRSIZ 14
|
#define ODIRSIZ 14
|
|
|
struct olddirect {
|
struct olddirect {
|
ino_t od_ino;
|
ino_t od_ino;
|
char od_name[ODIRSIZ];
|
char od_name[ODIRSIZ];
|
};
|
};
|
#else /* a Pyramid in the ATT universe */
|
#else /* a Pyramid in the ATT universe */
|
#define ODIRSIZ 248
|
#define ODIRSIZ 248
|
|
|
struct olddirect {
|
struct olddirect {
|
long od_ino;
|
long od_ino;
|
short od_fill1, od_fill2;
|
short od_fill1, od_fill2;
|
char od_name[ODIRSIZ];
|
char od_name[ODIRSIZ];
|
};
|
};
|
#endif
|
#endif
|
|
|
/*
|
/*
|
* get next entry in a directory.
|
* get next entry in a directory.
|
*/
|
*/
|
struct dirent *
|
struct dirent *
|
readdir(dirp)
|
readdir(dirp)
|
register DIR *dirp;
|
register DIR *dirp;
|
{
|
{
|
register struct olddirect *dp;
|
register struct olddirect *dp;
|
static struct dirent dir;
|
static struct dirent dir;
|
|
|
for (;;) {
|
for (;;) {
|
if (dirp->dd_loc == 0) {
|
if (dirp->dd_loc == 0) {
|
dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
|
dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
|
DIRBLKSIZ);
|
DIRBLKSIZ);
|
if (dirp->dd_size <= 0)
|
if (dirp->dd_size <= 0)
|
return NULL;
|
return NULL;
|
}
|
}
|
if (dirp->dd_loc >= dirp->dd_size) {
|
if (dirp->dd_loc >= dirp->dd_size) {
|
dirp->dd_loc = 0;
|
dirp->dd_loc = 0;
|
continue;
|
continue;
|
}
|
}
|
dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
|
dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
|
dirp->dd_loc += sizeof(struct olddirect);
|
dirp->dd_loc += sizeof(struct olddirect);
|
if (dp->od_ino == 0)
|
if (dp->od_ino == 0)
|
continue;
|
continue;
|
dir.d_ino = dp->od_ino;
|
dir.d_ino = dp->od_ino;
|
strncpy(dir.d_name, dp->od_name, ODIRSIZ);
|
strncpy(dir.d_name, dp->od_name, ODIRSIZ);
|
dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
|
dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
|
dir.d_namlen = strlen(dir.d_name);
|
dir.d_namlen = strlen(dir.d_name);
|
dir.d_reclen = DIRSIZ(&dir);
|
dir.d_reclen = DIRSIZ(&dir);
|
return (&dir);
|
return (&dir);
|
}
|
}
|
}
|
}
|
|
|
/*
|
/*
|
* close a directory.
|
* close a directory.
|
*/
|
*/
|
void
|
void
|
closedir(dirp)
|
closedir(dirp)
|
register DIR *dirp;
|
register DIR *dirp;
|
{
|
{
|
close(dirp->dd_fd);
|
close(dirp->dd_fd);
|
dirp->dd_fd = -1;
|
dirp->dd_fd = -1;
|
dirp->dd_loc = 0;
|
dirp->dd_loc = 0;
|
ckfree((char *) dirp);
|
ckfree((char *) dirp);
|
}
|
}
|
|
|