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 "gstrfuncs.h"
42 #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"
50 #include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
55 * An opaque structure representing an opened directory.
66 gchar utf8_buf
[FILENAME_MAX
*4];
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.
88 g_dir_open_with_errno (const gchar
*path
,
97 g_return_val_if_fail (path
!= NULL
, NULL
);
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
);
109 if (dir
.wdirp
== NULL
)
112 dir
.dirp
= opendir (path
);
114 if (dir
.dirp
== NULL
)
118 return g_memdup (&dir
, sizeof dir
);
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.
139 g_dir_open (const gchar
*path
,
146 dir
= g_dir_open_with_errno (path
, flags
);
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
));
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
173 /* Binary compatibility version. Not for newly compiled code. */
176 g_dir_open (const gchar
*path
,
180 gchar
*utf8_path
= g_locale_to_utf8 (path
, -1, NULL
, NULL
, error
);
183 if (utf8_path
== NULL
)
186 retval
= g_dir_open_utf8 (utf8_path
, flags
, error
);
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
212 g_dir_new_from_dirp (gpointer dirp
)
217 g_return_val_if_fail (dirp
!= NULL
, NULL
);
219 dir
= g_new (GDir
, 1);
224 g_assert_not_reached ();
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
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.
252 g_dir_read_name (GDir
*dir
)
256 struct _wdirent
*wentry
;
258 struct dirent
*entry
;
261 g_return_val_if_fail (dir
!= NULL
, NULL
);
266 wentry
= _wreaddir (dir
->wdirp
);
268 && (0 == wcscmp (wentry
->d_name
, L
".") ||
269 0 == wcscmp (wentry
->d_name
, L
"..")))
270 wentry
= _wreaddir (dir
->wdirp
);
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
);
283 return dir
->utf8_buf
;
286 entry
= readdir (dir
->dirp
);
288 && (0 == strcmp (entry
->d_name
, ".") ||
289 0 == strcmp (entry
->d_name
, "..")))
290 entry
= readdir (dir
->dirp
);
293 return entry
->d_name
;
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. */
308 g_dir_read_name (GDir
*dir
)
312 const gchar
*utf8_name
= g_dir_read_name_utf8 (dir
);
315 if (utf8_name
== NULL
)
318 retval
= g_locale_from_utf8 (utf8_name
, -1, NULL
, NULL
, NULL
);
322 strcpy (dir
->utf8_buf
, retval
);
325 return dir
->utf8_buf
;
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.
340 g_dir_rewind (GDir
*dir
)
342 g_return_if_fail (dir
!= NULL
);
345 _wrewinddir (dir
->wdirp
);
347 rewinddir (dir
->dirp
);
353 * @dir: a #GDir* created by g_dir_open()
355 * Closes the directory and deallocates all related resources.
358 g_dir_close (GDir
*dir
)
360 g_return_if_fail (dir
!= NULL
);
363 _wclosedir (dir
->wdirp
);
365 closedir (dir
->dirp
);