gsettings test: fix srcdir != builddir
[glib.git] / glib / gdir.c
blob4f17981580e95f8666d3a7c5729d48f87d893bb7
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gdir.c: Simplified wrapper around the DIRENT functions.
6 * Copyright 2001 Hans Breuer
7 * Copyright 2004 Tor Lillqvist
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.
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.
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.
25 #include "config.h"
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
32 #ifdef HAVE_DIRENT_H
33 #include <sys/types.h>
34 #include <dirent.h>
35 #endif
37 #include "gdir.h"
39 #include "gconvert.h"
40 #include "gfileutils.h"
41 #include "gstrfuncs.h"
42 #include "gtestutils.h"
43 #include "glibintl.h"
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
50 #include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
52 /**
53 * GDir:
55 * An opaque structure representing an opened directory.
58 struct _GDir
60 #ifdef G_OS_WIN32
61 _WDIR *wdirp;
62 #else
63 DIR *dirp;
64 #endif
65 #ifdef G_OS_WIN32
66 gchar utf8_buf[FILENAME_MAX*4];
67 #endif
70 /*< private >
71 * g_dir_open_with_errno:
72 * @path: the path to the directory you are interested in.
73 * @flags: Currently must be set to 0. Reserved for future use.
75 * Opens a directory for reading.
77 * This function is equivalent to g_dir_open() except in the error case,
78 * errno will be set accordingly.
80 * This is useful if you want to construct your own error message.
82 * Returns: a newly allocated #GDir on success, or %NULL on failure,
83 * with errno set accordingly.
85 * Since: 2.38
87 GDir *
88 g_dir_open_with_errno (const gchar *path,
89 guint flags)
91 GDir dir;
92 #ifdef G_OS_WIN32
93 gint saved_errno;
94 wchar_t *wpath;
95 #endif
97 g_return_val_if_fail (path != NULL, NULL);
99 #ifdef G_OS_WIN32
100 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
102 g_return_val_if_fail (wpath != NULL, NULL);
104 dir.wdirp = _wopendir (wpath);
105 saved_errno = errno;
106 g_free (wpath);
107 errno = saved_errno;
109 if (dir.wdirp == NULL)
110 return NULL;
111 #else
112 dir.dirp = opendir (path);
114 if (dir.dirp == NULL)
115 return NULL;
116 #endif
118 return g_memdup (&dir, sizeof dir);
122 * g_dir_open:
123 * @path: the path to the directory you are interested in. On Unix
124 * in the on-disk encoding. On Windows in UTF-8
125 * @flags: Currently must be set to 0. Reserved for future use.
126 * @error: return location for a #GError, or %NULL.
127 * If non-%NULL, an error will be set if and only if
128 * g_dir_open() fails.
130 * Opens a directory for reading. The names of the files in the
131 * directory can then be retrieved using g_dir_read_name(). Note
132 * that the ordering is not defined.
134 * Return value: a newly allocated #GDir on success, %NULL on failure.
135 * If non-%NULL, you must free the result with g_dir_close()
136 * when you are finished with it.
138 GDir *
139 g_dir_open (const gchar *path,
140 guint flags,
141 GError **error)
143 gint saved_errno;
144 GDir *dir;
146 dir = g_dir_open_with_errno (path, flags);
148 if (dir == NULL)
150 gchar *utf8_path;
152 saved_errno = errno;
154 utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
156 g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
157 _("Error opening directory '%s': %s"), utf8_path, g_strerror (saved_errno));
158 g_free (utf8_path);
161 return dir;
164 #if defined (G_OS_WIN32) && !defined (_WIN64)
166 /* The above function actually is called g_dir_open_utf8, and it's
167 * that what applications compiled with this GLib version will
168 * use.
171 #undef g_dir_open
173 /* Binary compatibility version. Not for newly compiled code. */
175 GDir *
176 g_dir_open (const gchar *path,
177 guint flags,
178 GError **error)
180 gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
181 GDir *retval;
183 if (utf8_path == NULL)
184 return NULL;
186 retval = g_dir_open_utf8 (utf8_path, flags, error);
188 g_free (utf8_path);
190 return retval;
192 #endif
194 /*< private >
195 * g_dir_new_from_dirp:
196 * @dirp: a #DIR* created by opendir() or fdopendir()
198 * Creates a #GDir object from the DIR object that is created using
199 * opendir() or fdopendir(). The created #GDir assumes ownership of the
200 * passed-in #DIR pointer.
202 * @dirp must not be %NULL.
204 * This function never fails.
206 * Returns: a newly allocated #GDir, which should be closed using
207 * g_dir_close().
209 * Since: 2.38
211 GDir *
212 g_dir_new_from_dirp (gpointer dirp)
214 #ifdef G_OS_UNIX
215 GDir *dir;
217 g_return_val_if_fail (dirp != NULL, NULL);
219 dir = g_new (GDir, 1);
220 dir->dirp = dirp;
222 return dir;
223 #else
224 g_assert_not_reached ();
225 #endif
229 * g_dir_read_name:
230 * @dir: a #GDir* created by g_dir_open()
232 * Retrieves the name of another entry in the directory, or %NULL.
233 * The order of entries returned from this function is not defined,
234 * and may vary by file system or other operating-system dependent
235 * factors.
237 * %NULL may also be returned in case of errors. On Unix, you can
238 * check <literal>errno</literal> to find out if %NULL was returned
239 * because of an error.
241 * On Unix, the '.' and '..' entries are omitted, and the returned
242 * name is in the on-disk encoding.
244 * On Windows, as is true of all GLib functions which operate on
245 * filenames, the returned name is in UTF-8.
247 * Return value: The entry's name or %NULL if there are no
248 * more entries. The return value is owned by GLib and
249 * must not be modified or freed.
251 const gchar *
252 g_dir_read_name (GDir *dir)
254 #ifdef G_OS_WIN32
255 gchar *utf8_name;
256 struct _wdirent *wentry;
257 #else
258 struct dirent *entry;
259 #endif
261 g_return_val_if_fail (dir != NULL, NULL);
263 #ifdef G_OS_WIN32
264 while (1)
266 wentry = _wreaddir (dir->wdirp);
267 while (wentry
268 && (0 == wcscmp (wentry->d_name, L".") ||
269 0 == wcscmp (wentry->d_name, L"..")))
270 wentry = _wreaddir (dir->wdirp);
272 if (wentry == NULL)
273 return NULL;
275 utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
277 if (utf8_name == NULL)
278 continue; /* Huh, impossible? Skip it anyway */
280 strcpy (dir->utf8_buf, utf8_name);
281 g_free (utf8_name);
283 return dir->utf8_buf;
285 #else
286 entry = readdir (dir->dirp);
287 while (entry
288 && (0 == strcmp (entry->d_name, ".") ||
289 0 == strcmp (entry->d_name, "..")))
290 entry = readdir (dir->dirp);
292 if (entry)
293 return entry->d_name;
294 else
295 return NULL;
296 #endif
299 #if defined (G_OS_WIN32) && !defined (_WIN64)
301 /* Ditto for g_dir_read_name */
303 #undef g_dir_read_name
305 /* Binary compatibility version. Not for newly compiled code. */
307 const gchar *
308 g_dir_read_name (GDir *dir)
310 while (1)
312 const gchar *utf8_name = g_dir_read_name_utf8 (dir);
313 gchar *retval;
315 if (utf8_name == NULL)
316 return NULL;
318 retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
320 if (retval != NULL)
322 strcpy (dir->utf8_buf, retval);
323 g_free (retval);
325 return dir->utf8_buf;
330 #endif
333 * g_dir_rewind:
334 * @dir: a #GDir* created by g_dir_open()
336 * Resets the given directory. The next call to g_dir_read_name()
337 * will return the first entry again.
339 void
340 g_dir_rewind (GDir *dir)
342 g_return_if_fail (dir != NULL);
344 #ifdef G_OS_WIN32
345 _wrewinddir (dir->wdirp);
346 #else
347 rewinddir (dir->dirp);
348 #endif
352 * g_dir_close:
353 * @dir: a #GDir* created by g_dir_open()
355 * Closes the directory and deallocates all related resources.
357 void
358 g_dir_close (GDir *dir)
360 g_return_if_fail (dir != NULL);
362 #ifdef G_OS_WIN32
363 _wclosedir (dir->wdirp);
364 #else
365 closedir (dir->dirp);
366 #endif
367 g_free (dir);