Merge remote branch 'gvdb/master'
[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 "glib.h"
38 #include "gdir.h"
39
40 #include "glibintl.h"
41
42 #include "galias.h"
43
44 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
45 #include "../build/win32/dirent/dirent.h"
46 #include "../build/win32/dirent/wdirent.c"
47 #endif
48
49 struct _GDir
50 {
51 #ifdef G_OS_WIN32
52   _WDIR *wdirp;
53 #else
54   DIR *dirp;
55 #endif
56 #ifdef G_OS_WIN32
57   gchar utf8_buf[FILENAME_MAX*4];
58 #endif
59 };
60
61 /**
62  * g_dir_open:
63  * @path: the path to the directory you are interested in. On Unix
64  *         in the on-disk encoding. On Windows in UTF-8
65  * @flags: Currently must be set to 0. Reserved for future use.
66  * @error: return location for a #GError, or %NULL.
67  *         If non-%NULL, an error will be set if and only if
68  *         g_dir_open() fails.
69  *
70  * Opens a directory for reading. The names of the files in the
71  * directory can then be retrieved using g_dir_read_name().
72  *
73  * Return value: a newly allocated #GDir on success, %NULL on failure.
74  *   If non-%NULL, you must free the result with g_dir_close()
75  *   when you are finished with it.
76  **/
77 GDir *
78 g_dir_open (const gchar  *path,
79             guint         flags,
80             GError      **error)
81 {
82   GDir *dir;
83   int errsv;
84 #ifdef G_OS_WIN32
85   wchar_t *wpath;
86 #else
87   gchar *utf8_path;
88 #endif
89
90   g_return_val_if_fail (path != NULL, NULL);
91
92 #ifdef G_OS_WIN32
93   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
94
95   if (wpath == NULL)
96     return NULL;
97
98   dir = g_new (GDir, 1);
99
100   dir->wdirp = _wopendir (wpath);
101   g_free (wpath);
102
103   if (dir->wdirp)
104     return dir;
105
106   /* error case */
107   errsv = errno;
108
109   g_set_error (error,
110                G_FILE_ERROR,
111                g_file_error_from_errno (errsv),
112                _("Error opening directory '%s': %s"),
113                path, g_strerror (errsv));
114   
115   g_free (dir);
116       
117   return NULL;
118 #else
119   dir = g_new (GDir, 1);
120
121   dir->dirp = opendir (path);
122
123   if (dir->dirp)
124     return dir;
125
126   /* error case */
127   errsv = errno;
128
129   utf8_path = g_filename_to_utf8 (path, -1,
130                                   NULL, NULL, NULL);
131
132   g_set_error (error,
133                G_FILE_ERROR,
134                g_file_error_from_errno (errsv),
135                _("Error opening directory '%s': %s"),
136                utf8_path, g_strerror (errsv));
137
138   g_free (utf8_path);
139   g_free (dir);
140
141   return NULL;
142 #endif
143 }
144
145 #if defined (G_OS_WIN32) && !defined (_WIN64)
146
147 /* The above function actually is called g_dir_open_utf8, and it's
148  * that what applications compiled with this GLib version will
149  * use.
150  */
151
152 #undef g_dir_open
153
154 /* Binary compatibility version. Not for newly compiled code. */
155
156 GDir *
157 g_dir_open (const gchar  *path,
158             guint         flags,
159             GError      **error)
160 {
161   gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
162   GDir *retval;
163
164   if (utf8_path == NULL)
165     return NULL;
166
167   retval = g_dir_open_utf8 (utf8_path, flags, error);
168
169   g_free (utf8_path);
170
171   return retval;
172 }
173 #endif
174
175 /**
176  * g_dir_read_name:
177  * @dir: a #GDir* created by g_dir_open()
178  *
179  * Retrieves the name of the next entry in the directory.  The '.' and
180  * '..' entries are omitted. On Windows, the returned name is in
181  * UTF-8. On Unix, it is in the on-disk encoding.
182  *
183  * Return value: The entry's name or %NULL if there are no 
184  *   more entries. The return value is owned by GLib and
185  *   must not be modified or freed.
186  **/
187 G_CONST_RETURN gchar*
188 g_dir_read_name (GDir *dir)
189 {
190 #ifdef G_OS_WIN32
191   gchar *utf8_name;
192   struct _wdirent *wentry;
193 #else
194   struct dirent *entry;
195 #endif
196
197   g_return_val_if_fail (dir != NULL, NULL);
198
199 #ifdef G_OS_WIN32
200   while (1)
201     {
202       wentry = _wreaddir (dir->wdirp);
203       while (wentry 
204              && (0 == wcscmp (wentry->d_name, L".") ||
205                  0 == wcscmp (wentry->d_name, L"..")))
206         wentry = _wreaddir (dir->wdirp);
207
208       if (wentry == NULL)
209         return NULL;
210
211       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
212
213       if (utf8_name == NULL)
214         continue;               /* Huh, impossible? Skip it anyway */
215
216       strcpy (dir->utf8_buf, utf8_name);
217       g_free (utf8_name);
218
219       return dir->utf8_buf;
220     }
221 #else
222   entry = readdir (dir->dirp);
223   while (entry 
224          && (0 == strcmp (entry->d_name, ".") ||
225              0 == strcmp (entry->d_name, "..")))
226     entry = readdir (dir->dirp);
227
228   if (entry)
229     return entry->d_name;
230   else
231     return NULL;
232 #endif
233 }
234
235 #if defined (G_OS_WIN32) && !defined (_WIN64)
236
237 /* Ditto for g_dir_read_name */
238
239 #undef g_dir_read_name
240
241 /* Binary compatibility version. Not for newly compiled code. */
242
243 G_CONST_RETURN gchar*
244 g_dir_read_name (GDir *dir)
245 {
246   while (1)
247     {
248       const gchar *utf8_name = g_dir_read_name_utf8 (dir);
249       gchar *retval;
250       
251       if (utf8_name == NULL)
252         return NULL;
253
254       retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
255
256       if (retval != NULL)
257         {
258           strcpy (dir->utf8_buf, retval);
259           g_free (retval);
260
261           return dir->utf8_buf;
262         }
263     }
264 }
265
266 #endif
267
268 /**
269  * g_dir_rewind:
270  * @dir: a #GDir* created by g_dir_open()
271  *
272  * Resets the given directory. The next call to g_dir_read_name()
273  * will return the first entry again.
274  **/
275 void
276 g_dir_rewind (GDir *dir)
277 {
278   g_return_if_fail (dir != NULL);
279   
280 #ifdef G_OS_WIN32
281   _wrewinddir (dir->wdirp);
282 #else
283   rewinddir (dir->dirp);
284 #endif
285 }
286
287 /**
288  * g_dir_close:
289  * @dir: a #GDir* created by g_dir_open()
290  *
291  * Closes the directory and deallocates all related resources.
292  **/
293 void
294 g_dir_close (GDir *dir)
295 {
296   g_return_if_fail (dir != NULL);
297
298 #ifdef G_OS_WIN32
299   _wclosedir (dir->wdirp);
300 #else
301   closedir (dir->dirp);
302 #endif
303   g_free (dir);
304 }
305
306 #define __G_DIR_C__
307 #include "galiasdef.c"