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