Build fixes for the fall-out of the inclusion changes
[platform/upstream/glib.git] / glib / gdir.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gdir.c: Simplified wrapper around the DIRENT functions.
5  *
6  * Copyright 2001 Hans Breuer
7  * Copyright 2004 Tor Lillqvist
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31
32 #ifdef HAVE_DIRENT_H
33 #include <sys/types.h>
34 #include <dirent.h>
35 #endif
36
37 #include "gdir.h"
38
39 #include "gconvert.h"
40 #include "gfileutils.h"
41 #include "gstrfuncs.h"
42 #include "gtestutils.h"
43 #include "glibintl.h"
44
45
46 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
47 #include "../build/win32/dirent/dirent.h"
48 #include "../build/win32/dirent/wdirent.c"
49 #endif
50
51 struct _GDir
52 {
53 #ifdef G_OS_WIN32
54   _WDIR *wdirp;
55 #else
56   DIR *dirp;
57 #endif
58 #ifdef G_OS_WIN32
59   gchar utf8_buf[FILENAME_MAX*4];
60 #endif
61 };
62
63 /**
64  * g_dir_open:
65  * @path: the path to the directory you are interested in. On Unix
66  *         in the on-disk encoding. On Windows in UTF-8
67  * @flags: Currently must be set to 0. Reserved for future use.
68  * @error: return location for a #GError, or %NULL.
69  *         If non-%NULL, an error will be set if and only if
70  *         g_dir_open() fails.
71  *
72  * Opens a directory for reading. The names of the files in the
73  * directory can then be retrieved using g_dir_read_name().
74  *
75  * Return value: a newly allocated #GDir on success, %NULL on failure.
76  *   If non-%NULL, you must free the result with g_dir_close()
77  *   when you are finished with it.
78  **/
79 GDir *
80 g_dir_open (const gchar  *path,
81             guint         flags,
82             GError      **error)
83 {
84   GDir *dir;
85   int errsv;
86 #ifdef G_OS_WIN32
87   wchar_t *wpath;
88 #else
89   gchar *utf8_path;
90 #endif
91
92   g_return_val_if_fail (path != NULL, NULL);
93
94 #ifdef G_OS_WIN32
95   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
96
97   if (wpath == NULL)
98     return NULL;
99
100   dir = g_new (GDir, 1);
101
102   dir->wdirp = _wopendir (wpath);
103   g_free (wpath);
104
105   if (dir->wdirp)
106     return dir;
107
108   /* error case */
109   errsv = errno;
110
111   g_set_error (error,
112                G_FILE_ERROR,
113                g_file_error_from_errno (errsv),
114                _("Error opening directory '%s': %s"),
115                path, g_strerror (errsv));
116   
117   g_free (dir);
118       
119   return NULL;
120 #else
121   dir = g_new (GDir, 1);
122
123   dir->dirp = opendir (path);
124
125   if (dir->dirp)
126     return dir;
127
128   /* error case */
129   errsv = errno;
130
131   utf8_path = g_filename_to_utf8 (path, -1,
132                                   NULL, NULL, NULL);
133
134   g_set_error (error,
135                G_FILE_ERROR,
136                g_file_error_from_errno (errsv),
137                _("Error opening directory '%s': %s"),
138                utf8_path, g_strerror (errsv));
139
140   g_free (utf8_path);
141   g_free (dir);
142
143   return NULL;
144 #endif
145 }
146
147 #if defined (G_OS_WIN32) && !defined (_WIN64)
148
149 /* The above function actually is called g_dir_open_utf8, and it's
150  * that what applications compiled with this GLib version will
151  * use.
152  */
153
154 #undef g_dir_open
155
156 /* Binary compatibility version. Not for newly compiled code. */
157
158 GDir *
159 g_dir_open (const gchar  *path,
160             guint         flags,
161             GError      **error)
162 {
163   gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
164   GDir *retval;
165
166   if (utf8_path == NULL)
167     return NULL;
168
169   retval = g_dir_open_utf8 (utf8_path, flags, error);
170
171   g_free (utf8_path);
172
173   return retval;
174 }
175 #endif
176
177 /**
178  * g_dir_read_name:
179  * @dir: a #GDir* created by g_dir_open()
180  *
181  * Retrieves the name of the next entry in the directory.  The '.' and
182  * '..' entries are omitted. On Windows, the returned name is in
183  * UTF-8. On Unix, it is in the on-disk encoding.
184  *
185  * Return value: The entry's name or %NULL if there are no 
186  *   more entries. The return value is owned by GLib and
187  *   must not be modified or freed.
188  **/
189 G_CONST_RETURN gchar*
190 g_dir_read_name (GDir *dir)
191 {
192 #ifdef G_OS_WIN32
193   gchar *utf8_name;
194   struct _wdirent *wentry;
195 #else
196   struct dirent *entry;
197 #endif
198
199   g_return_val_if_fail (dir != NULL, NULL);
200
201 #ifdef G_OS_WIN32
202   while (1)
203     {
204       wentry = _wreaddir (dir->wdirp);
205       while (wentry 
206              && (0 == wcscmp (wentry->d_name, L".") ||
207                  0 == wcscmp (wentry->d_name, L"..")))
208         wentry = _wreaddir (dir->wdirp);
209
210       if (wentry == NULL)
211         return NULL;
212
213       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
214
215       if (utf8_name == NULL)
216         continue;               /* Huh, impossible? Skip it anyway */
217
218       strcpy (dir->utf8_buf, utf8_name);
219       g_free (utf8_name);
220
221       return dir->utf8_buf;
222     }
223 #else
224   entry = readdir (dir->dirp);
225   while (entry 
226          && (0 == strcmp (entry->d_name, ".") ||
227              0 == strcmp (entry->d_name, "..")))
228     entry = readdir (dir->dirp);
229
230   if (entry)
231     return entry->d_name;
232   else
233     return NULL;
234 #endif
235 }
236
237 #if defined (G_OS_WIN32) && !defined (_WIN64)
238
239 /* Ditto for g_dir_read_name */
240
241 #undef g_dir_read_name
242
243 /* Binary compatibility version. Not for newly compiled code. */
244
245 G_CONST_RETURN gchar*
246 g_dir_read_name (GDir *dir)
247 {
248   while (1)
249     {
250       const gchar *utf8_name = g_dir_read_name_utf8 (dir);
251       gchar *retval;
252       
253       if (utf8_name == NULL)
254         return NULL;
255
256       retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
257
258       if (retval != NULL)
259         {
260           strcpy (dir->utf8_buf, retval);
261           g_free (retval);
262
263           return dir->utf8_buf;
264         }
265     }
266 }
267
268 #endif
269
270 /**
271  * g_dir_rewind:
272  * @dir: a #GDir* created by g_dir_open()
273  *
274  * Resets the given directory. The next call to g_dir_read_name()
275  * will return the first entry again.
276  **/
277 void
278 g_dir_rewind (GDir *dir)
279 {
280   g_return_if_fail (dir != NULL);
281   
282 #ifdef G_OS_WIN32
283   _wrewinddir (dir->wdirp);
284 #else
285   rewinddir (dir->dirp);
286 #endif
287 }
288
289 /**
290  * g_dir_close:
291  * @dir: a #GDir* created by g_dir_open()
292  *
293  * Closes the directory and deallocates all related resources.
294  **/
295 void
296 g_dir_close (GDir *dir)
297 {
298   g_return_if_fail (dir != NULL);
299
300 #ifdef G_OS_WIN32
301   _wclosedir (dir->wdirp);
302 #else
303   closedir (dir->dirp);
304 #endif
305   g_free (dir);
306 }