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