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

61 lines
1.1 KiB
C
Raw Normal View History

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();
if ((fd = open(name, 0)) == -1)
return NULL;
fstat(fd, &stbuf);
1987-04-06 13:03:05 +00:00
siz = stbuf.st_blksize;
1987-01-27 15:57:55 +00:00
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
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;
dirp->dd_loc = -1;
1987-01-27 15:57:55 +00:00
return dirp;
}