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