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.
33 #include <sys/types.h>
40 #include "gfileutils.h"
41 #include "gtestutils.h"
45 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
46 #include "../build/win32/dirent/dirent.h"
47 #include "../build/win32/dirent/wdirent.c"
58 gchar utf8_buf[FILENAME_MAX*4];
64 * @path: the path to the directory you are interested in. On Unix
65 * in the on-disk encoding. On Windows in UTF-8
66 * @flags: Currently must be set to 0. Reserved for future use.
67 * @error: return location for a #GError, or %NULL.
68 * If non-%NULL, an error will be set if and only if
71 * Opens a directory for reading. The names of the files in the
72 * directory can then be retrieved using g_dir_read_name().
74 * Return value: a newly allocated #GDir on success, %NULL on failure.
75 * If non-%NULL, you must free the result with g_dir_close()
76 * when you are finished with it.
79 g_dir_open (const gchar *path,
91 g_return_val_if_fail (path != NULL, NULL);
94 wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
99 dir = g_new (GDir, 1);
101 dir->wdirp = _wopendir (wpath);
112 g_file_error_from_errno (errsv),
113 _("Error opening directory '%s': %s"),
114 path, g_strerror (errsv));
120 dir = g_new (GDir, 1);
122 dir->dirp = opendir (path);
130 utf8_path = g_filename_to_utf8 (path, -1,
135 g_file_error_from_errno (errsv),
136 _("Error opening directory '%s': %s"),
137 utf8_path, g_strerror (errsv));
146 #if defined (G_OS_WIN32) && !defined (_WIN64)
148 /* The above function actually is called g_dir_open_utf8, and it's
149 * that what applications compiled with this GLib version will
155 /* Binary compatibility version. Not for newly compiled code. */
158 g_dir_open (const gchar *path,
162 gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
165 if (utf8_path == NULL)
168 retval = g_dir_open_utf8 (utf8_path, flags, error);
178 * @dir: a #GDir* created by g_dir_open()
180 * Retrieves the name of the next entry in the directory. The '.' and
181 * '..' entries are omitted. On Windows, the returned name is in
182 * UTF-8. On Unix, it is in the on-disk encoding.
184 * Return value: The entry's name or %NULL if there are no
185 * more entries. The return value is owned by GLib and
186 * must not be modified or freed.
188 G_CONST_RETURN gchar*
189 g_dir_read_name (GDir *dir)
193 struct _wdirent *wentry;
195 struct dirent *entry;
198 g_return_val_if_fail (dir != NULL, NULL);
203 wentry = _wreaddir (dir->wdirp);
205 && (0 == wcscmp (wentry->d_name, L".") ||
206 0 == wcscmp (wentry->d_name, L"..")))
207 wentry = _wreaddir (dir->wdirp);
212 utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
214 if (utf8_name == NULL)
215 continue; /* Huh, impossible? Skip it anyway */
217 strcpy (dir->utf8_buf, utf8_name);
220 return dir->utf8_buf;
223 entry = readdir (dir->dirp);
225 && (0 == strcmp (entry->d_name, ".") ||
226 0 == strcmp (entry->d_name, "..")))
227 entry = readdir (dir->dirp);
230 return entry->d_name;
236 #if defined (G_OS_WIN32) && !defined (_WIN64)
238 /* Ditto for g_dir_read_name */
240 #undef g_dir_read_name
242 /* Binary compatibility version. Not for newly compiled code. */
244 G_CONST_RETURN gchar*
245 g_dir_read_name (GDir *dir)
249 const gchar *utf8_name = g_dir_read_name_utf8 (dir);
252 if (utf8_name == NULL)
255 retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
259 strcpy (dir->utf8_buf, retval);
262 return dir->utf8_buf;
271 * @dir: a #GDir* created by g_dir_open()
273 * Resets the given directory. The next call to g_dir_read_name()
274 * will return the first entry again.
277 g_dir_rewind (GDir *dir)
279 g_return_if_fail (dir != NULL);
282 _wrewinddir (dir->wdirp);
284 rewinddir (dir->dirp);
290 * @dir: a #GDir* created by g_dir_open()
292 * Closes the directory and deallocates all related resources.
295 g_dir_close (GDir *dir)
297 g_return_if_fail (dir != NULL);
300 _wclosedir (dir->wdirp);
302 closedir (dir->dirp);