purely cosmetic commit: edited the introductory comments of several files to uniform...
[reactos.git] / posix / lib / psxdll / dirent / opendir.c
1 /* $Id: opendir.c,v 1.2 2002/02/20 09:17:56 hyperion Exp $
2 */
3 /*
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS POSIX+ Subsystem
6 * FILE: subsys/psx/lib/psxdll/dirent/opendir.c
7 * PURPOSE: Open a directory
8 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
9 * UPDATE HISTORY:
10 * 27/01/2002: Created
11 * 13/02/2002: KJK::Hyperion: modified to use file descriptors
12 */
13
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <dirent.h>
17 #include <wchar.h>
18 #include <errno.h>
19 #include <psx/debug.h>
20 #include <psx/stdlib.h>
21 #include <psx/dirent.h>
22 #include <psx/safeobj.h>
23
24 DIR *opendir(const char *dirname)
25 {
26 ANSI_STRING strDirName;
27 UNICODE_STRING wstrDirName;
28 DIR *pdData;
29
30 RtlInitAnsiString(&strDirName, (PCSZ)dirname);
31 RtlAnsiStringToUnicodeString(&wstrDirName, &strDirName, TRUE);
32
33 pdData = (DIR *)_Wopendir(wstrDirName.Buffer);
34
35 RtlFreeUnicodeString(&wstrDirName);
36
37 return (pdData);
38
39 }
40
41 DIR *_Wopendir(const wchar_t *dirname)
42 {
43 struct __internal_DIR *pidData;
44 int nFileNo;
45
46 /* allocate internal object */
47 pidData = __malloc(sizeof(*pidData));
48
49 /* allocation failed */
50 if(pidData == 0)
51 {
52 errno = ENOMEM;
53 return (0);
54 }
55
56 /* open the directory */
57 nFileNo = _Wopen(dirname, O_RDONLY | _O_DIRFILE);
58
59 /* failure */
60 if(nFileNo < 0)
61 {
62 __free(pidData);
63 return (0);
64 }
65
66 /* directory file descriptors must be closed on exec() */
67 if(fcntl(nFileNo, F_SETFD, FD_CLOEXEC) == -1)
68 WARN
69 (
70 "couldn't set FD_CLOEXEC flag on file number %u, errno %u",
71 nFileNo,
72 errno
73 );
74
75 /* associate the internal data to the file descriptor */
76 if(fcntl(nFileNo, F_SETXP, pidData) == -1)
77 WARN
78 (
79 "couldn't associate the object at 0x%X to the file number %u, errno %u",
80 pidData,
81 nFileNo,
82 errno
83 );
84
85 if(fcntl(nFileNo, F_SETXS, sizeof(*pidData)) == -1)
86 WARN
87 (
88 "couldn't set the extra data size of the file number %u, errno %u",
89 nFileNo,
90 errno
91 );
92
93 pidData->signature = __IDIR_MAGIC;
94
95 /* success */
96 return ((DIR *)pidData);
97 }
98
99 /* EOF */
100