1987-03-31 10:45:53 +00:00
|
|
|
/* $Header$ */
|
1987-01-27 15:57:55 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/dir.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open a directory.
|
|
|
|
*/
|
|
|
|
DIR *opendir(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
register DIR *dirp;
|
|
|
|
register int fd;
|
|
|
|
struct stat stbuf;
|
1987-04-06 13:03:05 +00:00
|
|
|
long siz;
|
1987-01-27 15:57:55 +00:00
|
|
|
extern char *malloc();
|
|
|
|
|
1987-04-07 08:46:15 +00:00
|
|
|
#ifdef __BSD4_2
|
|
|
|
siz = stbuf.st_blksize;
|
|
|
|
#else
|
|
|
|
siz = DIRBLKSIZ;
|
|
|
|
#endif
|
1987-01-27 15:57:55 +00:00
|
|
|
if ((fd = open(name, 0)) == -1)
|
|
|
|
return NULL;
|
|
|
|
fstat(fd, &stbuf);
|
|
|
|
if (((stbuf.st_mode & S_IFDIR) == 0) ||
|
|
|
|
((dirp = (DIR *)malloc(sizeof (DIR))) == NULL)) {
|
|
|
|
close (fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
1987-04-06 13:03:05 +00:00
|
|
|
if (stbuf.st_size > siz) siz = stbuf.st_size;
|
|
|
|
if ((unsigned) siz == siz &&
|
|
|
|
(dirp->dd_buf = malloc((unsigned) siz))) {
|
|
|
|
dirp->dd_bsize = siz;
|
|
|
|
#ifdef sun
|
|
|
|
dirp->dd_size = getdirentries(fd,
|
|
|
|
(char *) dirp->dd_buf,
|
|
|
|
(int) siz,
|
|
|
|
&siz);
|
|
|
|
#else
|
1987-04-06 12:33:28 +00:00
|
|
|
dirp->dd_size = read(fd, dirp->dd_buf, dirp->dd_bsize);
|
1987-04-06 13:03:05 +00:00
|
|
|
#endif
|
1987-03-27 15:11:06 +00:00
|
|
|
close(fd);
|
|
|
|
dirp->dd_fd = -2;
|
|
|
|
dirp->dd_loc = 0;
|
|
|
|
return dirp;
|
1987-02-24 13:10:59 +00:00
|
|
|
}
|
1987-04-06 13:03:05 +00:00
|
|
|
#ifndef sun
|
1987-02-24 13:10:59 +00:00
|
|
|
else if (dirp->dd_buf = malloc(8*DIRBLKSIZ)) {
|
|
|
|
dirp->dd_bsize = 8 * DIRBLKSIZ;
|
|
|
|
}
|
|
|
|
else if (dirp->dd_buf = malloc(DIRBLKSIZ)) {
|
|
|
|
dirp->dd_bsize = DIRBLKSIZ;
|
|
|
|
}
|
1987-04-06 13:03:05 +00:00
|
|
|
#endif
|
1987-02-24 13:10:59 +00:00
|
|
|
else {
|
|
|
|
close(fd);
|
|
|
|
free((char *) dirp);
|
|
|
|
return NULL;
|
|
|
|
}
|
1987-01-27 15:57:55 +00:00
|
|
|
dirp->dd_fd = fd;
|
1987-03-27 15:11:06 +00:00
|
|
|
dirp->dd_loc = -1;
|
1987-01-27 15:57:55 +00:00
|
|
|
return dirp;
|
|
|
|
}
|