VirtualBox

source: kBuild/vendor/grep/2.12/lib/opendir.c

Last change on this file was 2595, checked in by bird, 12 years ago

gnu grep version 2.12 (grep-2.12.tar.xz, md5sum=8d2f0346d08b13c18afb81f0e8aa1e2f)

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1/* Start reading the entries of a directory.
2 Copyright (C) 2006-2012 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17#include <config.h>
18
19/* Specification. */
20#include <dirent.h>
21
22#include <errno.h>
23#include <stddef.h>
24
25#if HAVE_OPENDIR
26
27/* Override opendir(), to keep track of the open file descriptors.
28 Needed because there is a function dirfd(). */
29
30#else
31
32# include <stdlib.h>
33
34# include "dirent-private.h"
35# include "filename.h"
36
37#endif
38
39#if REPLACE_FCHDIR
40# include <unistd.h>
41#endif
42
43DIR *
44opendir (const char *dir_name)
45{
46#if HAVE_OPENDIR
47# undef opendir
48 DIR *dirp;
49
50 dirp = opendir (dir_name);
51 if (dirp == NULL)
52 return NULL;
53
54#else
55
56 char dir_name_mask[MAX_PATH + 1 + 1 + 1];
57 int status;
58 HANDLE current;
59 WIN32_FIND_DATA entry;
60 struct gl_directory *dirp;
61
62 if (dir_name[0] == '\0')
63 {
64 errno = ENOENT;
65 return NULL;
66 }
67
68 /* Make the dir_name absolute, so that we continue reading the same
69 directory if the current directory changed between this opendir()
70 call and a subsequent rewinddir() call. */
71 if (!GetFullPathName (dir_name, MAX_PATH, dir_name_mask, NULL))
72 {
73 errno = EINVAL;
74 return NULL;
75 }
76
77 /* Append the mask.
78 "*" and "*.*" appear to be equivalent. */
79 {
80 char *p;
81
82 p = dir_name_mask + strlen (dir_name_mask);
83 if (p > dir_name_mask && !ISSLASH (p[-1]))
84 *p++ = '\\';
85 *p++ = '*';
86 *p = '\0';
87 }
88
89 /* Start searching the directory. */
90 status = -1;
91 current = FindFirstFile (dir_name_mask, &entry);
92 if (current == INVALID_HANDLE_VALUE)
93 {
94 switch (GetLastError ())
95 {
96 case ERROR_FILE_NOT_FOUND:
97 status = -2;
98 break;
99 case ERROR_PATH_NOT_FOUND:
100 errno = ENOENT;
101 return NULL;
102 case ERROR_DIRECTORY:
103 errno = ENOTDIR;
104 return NULL;
105 case ERROR_ACCESS_DENIED:
106 errno = EACCES;
107 return NULL;
108 default:
109 errno = EIO;
110 return NULL;
111 }
112 }
113
114 /* Allocate the result. */
115 dirp =
116 (struct gl_directory *)
117 malloc (offsetof (struct gl_directory, dir_name_mask[0])
118 + strlen (dir_name_mask) + 1);
119 if (dirp == NULL)
120 {
121 if (current != INVALID_HANDLE_VALUE)
122 FindClose (current);
123 errno = ENOMEM;
124 return NULL;
125 }
126 dirp->status = status;
127 dirp->current = current;
128 if (status == -1)
129 memcpy (&dirp->entry, &entry, sizeof (WIN32_FIND_DATA));
130 strcpy (dirp->dir_name_mask, dir_name_mask);
131
132#endif
133
134#if REPLACE_FCHDIR
135 {
136 int fd = dirfd (dirp);
137 if (0 <= fd && _gl_register_fd (fd, dir_name) != fd)
138 {
139 int saved_errno = errno;
140 closedir (dirp);
141 errno = saved_errno;
142 return NULL;
143 }
144 }
145#endif
146
147 return dirp;
148}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette