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.
28 #include <string.h> /* strcmp */
31 #include <sys/types.h>
50 gchar utf8_buf[FILENAME_MAX*4];
56 * @path: the path to the directory you are interested in. On Unix
57 * in the on-disk encoding. On Windows in UTF-8
58 * @flags: Currently must be set to 0. Reserved for future use.
59 * @error: return location for a #GError, or %NULL.
60 * If non-%NULL, an error will be set if and only if
63 * Opens a directory for reading. The names of the files in the
64 * directory can then be retrieved using g_dir_read_name().
66 * Return value: a newly allocated #GDir on success, %NULL on failure.
67 * If non-%NULL, you must free the result with g_dir_close()
68 * when you are finished with it.
71 g_dir_open (const gchar *path,
80 g_return_val_if_fail (path != NULL, NULL);
83 if (G_WIN32_HAVE_WIDECHAR_API ())
85 wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
90 dir = g_new (GDir, 1);
92 dir->u.wdirp = _wopendir (wpath);
100 gchar *cp_path = g_locale_from_utf8 (path, -1, NULL, NULL, error);
105 dir = g_new (GDir, 1);
107 dir->u.dirp = opendir (cp_path);
119 g_file_error_from_errno (errno),
120 _("Error opening directory '%s': %s"),
121 path, g_strerror (errno));
127 dir = g_new (GDir, 1);
129 dir->u.dirp = opendir (path);
135 utf8_path = g_filename_to_utf8 (path, -1,
139 g_file_error_from_errno (errno),
140 _("Error opening directory '%s': %s"),
141 utf8_path, g_strerror (errno));
152 /* The above function actually is called g_dir_open_utf8, and it's
153 * that what applications compiled with this GLib version will
159 /* Binary compatibility version. Not for newly compiled code. */
162 g_dir_open (const gchar *path,
166 gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
169 if (utf8_path == NULL)
172 retval = g_dir_open_utf8 (utf8_path, flags, error);
182 * @dir: a #GDir* created by g_dir_open()
184 * Retrieves the name of the next entry in the directory. The '.' and
185 * '..' entries are omitted. On Windows, the returned name is in
186 * UTF-8. On Unix, it is in the on-disk encoding.
188 * Return value: The entry's name or %NULL if there are no
189 * more entries. The return value is owned by GLib and
190 * must not be modified or freed.
192 G_CONST_RETURN gchar*
193 g_dir_read_name (GDir *dir)
195 struct dirent *entry;
197 g_return_val_if_fail (dir != NULL, NULL);
200 if (G_WIN32_HAVE_WIDECHAR_API ())
203 struct _wdirent *wentry;
207 wentry = _wreaddir (dir->u.wdirp);
209 && (0 == wcscmp (wentry->d_name, L".") ||
210 0 == wcscmp (wentry->d_name, L"..")))
211 wentry = _wreaddir (dir->u.wdirp);
216 utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
218 if (utf8_name == NULL)
219 continue; /* Huh, impossible? Skip it anyway */
221 strcpy (dir->utf8_buf, utf8_name);
224 return dir->utf8_buf;
233 entry = readdir (dir->u.dirp);
235 && (0 == strcmp (entry->d_name, ".") ||
236 0 == strcmp (entry->d_name, "..")))
237 entry = readdir (dir->u.dirp);
242 utf8_name = g_locale_to_utf8 (entry->d_name, -1, NULL, NULL, NULL);
244 if (utf8_name != NULL)
246 strcpy (dir->utf8_buf, utf8_name);
249 return dir->utf8_buf;
254 entry = readdir (dir->u.dirp);
256 && (0 == strcmp (entry->d_name, ".") ||
257 0 == strcmp (entry->d_name, "..")))
258 entry = readdir (dir->u.dirp);
261 return entry->d_name;
269 /* Ditto for g_dir_read_name */
271 #undef g_dir_read_name
273 /* Binary compatibility version. Not for newly compiled code. */
275 G_CONST_RETURN gchar*
276 g_dir_read_name (GDir *dir)
280 const gchar *utf8_name = g_dir_read_name_utf8 (dir);
283 if (utf8_name == NULL)
286 retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
290 strcpy (dir->utf8_buf, retval);
293 return dir->utf8_buf;
302 * @dir: a #GDir* created by g_dir_open()
304 * Resets the given directory. The next call to g_dir_read_name()
305 * will return the first entry again.
308 g_dir_rewind (GDir *dir)
310 g_return_if_fail (dir != NULL);
313 if (G_WIN32_HAVE_WIDECHAR_API ())
315 _wrewinddir (dir->u.wdirp);
320 rewinddir (dir->u.dirp);
325 * @dir: a #GDir* created by g_dir_open()
327 * Closes the directory and deallocates all related resources.
330 g_dir_close (GDir *dir)
332 g_return_if_fail (dir != NULL);
335 if (G_WIN32_HAVE_WIDECHAR_API ())
337 _wclosedir (dir->u.wdirp);
343 closedir (dir->u.dirp);