dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libast / common / dir / opendir.c
blob08bbe06407d0f4ad8ccc2b3cb1472ab663bd81e2
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
24 * opendir, closedir
26 * open|close directory stream
28 * POSIX compatible directory stream access routines:
30 * #include <sys/types.h>
31 * #include <dirent.h>
33 * NOTE: readdir() returns a pointer to struct dirent
36 #include "dirlib.h"
38 #if _dir_ok
40 NoN(opendir)
42 #else
44 static const char id_dir[] = "\n@(#)$Id: directory (AT&T Research) 1993-04-01 $\0\n";
46 static DIR* freedirp; /* always keep one dirp */
48 DIR*
49 opendir(register const char* path)
51 register DIR* dirp = 0;
52 register int fd;
53 struct stat st;
55 if ((fd = open(path, O_RDONLY)) < 0) return(0);
56 if (fstat(fd, &st) < 0 ||
57 !S_ISDIR(st.st_mode) && (errno = ENOTDIR) ||
58 fcntl(fd, F_SETFD, FD_CLOEXEC) ||
59 !(dirp = freedirp ? freedirp :
60 #if defined(_DIR_PRIVATE_) || _ptr_dd_buf
61 newof(0, DIR, 1, DIRBLKSIZ)
62 #else
63 newof(0, DIR, 1, 0)
64 #endif
67 close(fd);
68 if (dirp)
70 if (!freedirp) freedirp = dirp;
71 else free(dirp);
73 return(0);
75 freedirp = 0;
76 dirp->dd_fd = fd;
77 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
78 #if defined(_DIR_PRIVATE_) || _ptr_dd_buf
79 dirp->dd_buf = (void*)((char*)dirp + sizeof(DIR));
80 #endif
81 return(dirp);
84 void
85 closedir(register DIR* dirp)
87 if (dirp)
89 close(dirp->dd_fd);
90 if (!freedirp) freedirp = dirp;
91 else free(dirp);
95 #endif