ack/lang/cem/libcc/gen/readdir.c

66 lines
1.4 KiB
C
Raw Normal View History

1987-01-27 15:57:55 +00:00
#include <sys/types.h>
#include <sys/dir.h>
#ifndef __BSD4_2
1987-01-27 15:57:55 +00:00
/*
* 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];
};
#else
#define olddirect direct
#endif
/*
* 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 == -1) {
1987-01-27 15:57:55 +00:00
dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
1987-02-24 13:10:59 +00:00
dirp->dd_bsize);
1987-01-27 15:57:55 +00:00
if (dirp->dd_size <= 0) {
dirp->dd_size = 0;
return NULL;
}
dirp->dd_loc = 0;
#ifdef __BSD4_2
1987-01-27 15:57:55 +00:00
if (! ((struct direct *) dirp->dd_buf)->d_ino) {
dirp->dd_loc = ((struct direct *)dirp->dd_buf)->d_reclen;
1987-01-27 15:57:55 +00:00
}
#endif
}
if (dirp->dd_loc >= dirp->dd_size) {
dirp->dd_loc = -1;
1987-01-27 15:57:55 +00:00
continue;
}
dp = (struct olddirect *) (dirp->dd_buf + dirp->dd_loc);
#ifndef __BSD4_2
1987-01-27 15:57:55 +00:00
dirp->dd_loc += sizeof (struct olddirect);
if (dp->od_ino == 0)
continue;
dir.d_ino = dp->od_ino;
strncpy(dir.d_name, dp->od_name, ODIRSIZ);
dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
dir.d_reclen = DIRSIZ(&dir);
dir.d_namlen = strlen(dir.d_name);
#else
dirp->dd_loc += dp->d_reclen;
dir.d_ino = dp->d_ino;
strcpy(dir.d_name, dp->d_name);
dir.d_reclen = dp->d_reclen;
dir.d_namlen = dp->d_namlen;
#endif
return &dir;
}
}