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"
46 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
47 #include "../build/win32/dirent/dirent.h"
48 #include "../build/win32/dirent/wdirent.c"
54 * An opaque structure representing an opened directory.
65 gchar utf8_buf
[FILENAME_MAX
*4];
71 * @path: the path to the directory you are interested in. On Unix
72 * in the on-disk encoding. On Windows in UTF-8
73 * @flags: Currently must be set to 0. Reserved for future use.
74 * @error: return location for a #GError, or %NULL.
75 * If non-%NULL, an error will be set if and only if
78 * Opens a directory for reading. The names of the files in the
79 * directory can then be retrieved using g_dir_read_name(). Note
80 * that the ordering is not defined.
82 * Return value: a newly allocated #GDir on success, %NULL on failure.
83 * If non-%NULL, you must free the result with g_dir_close()
84 * when you are finished with it.
87 g_dir_open (const gchar
*path
,
99 g_return_val_if_fail (path
!= NULL
, NULL
);
102 wpath
= g_utf8_to_utf16 (path
, -1, NULL
, NULL
, error
);
107 dir
= g_new (GDir
, 1);
109 dir
->wdirp
= _wopendir (wpath
);
120 g_file_error_from_errno (errsv
),
121 _("Error opening directory '%s': %s"),
122 path
, g_strerror (errsv
));
128 dir
= g_new (GDir
, 1);
130 dir
->dirp
= opendir (path
);
138 utf8_path
= g_filename_to_utf8 (path
, -1,
143 g_file_error_from_errno (errsv
),
144 _("Error opening directory '%s': %s"),
145 utf8_path
, g_strerror (errsv
));
154 #if defined (G_OS_WIN32) && !defined (_WIN64)
156 /* The above function actually is called g_dir_open_utf8, and it's
157 * that what applications compiled with this GLib version will
163 /* Binary compatibility version. Not for newly compiled code. */
166 g_dir_open (const gchar
*path
,
170 gchar
*utf8_path
= g_locale_to_utf8 (path
, -1, NULL
, NULL
, error
);
173 if (utf8_path
== NULL
)
176 retval
= g_dir_open_utf8 (utf8_path
, flags
, error
);
186 * @dir: a #GDir* created by g_dir_open()
188 * Retrieves the name of another entry in the directory, or %NULL.
189 * The order of entries returned from this function is not defined,
190 * and may vary by file system or other operating-system dependent
193 * On Unix, the '.' and '..' entries are omitted, and the returned
194 * name is in the on-disk encoding.
196 * On Windows, as is true of all GLib functions which operate on
197 * filenames, the returned name is in UTF-8.
199 * Return value: The entry's name or %NULL if there are no
200 * more entries. The return value is owned by GLib and
201 * must not be modified or freed.
204 g_dir_read_name (GDir
*dir
)
208 struct _wdirent
*wentry
;
210 struct dirent
*entry
;
213 g_return_val_if_fail (dir
!= NULL
, NULL
);
218 wentry
= _wreaddir (dir
->wdirp
);
220 && (0 == wcscmp (wentry
->d_name
, L
".") ||
221 0 == wcscmp (wentry
->d_name
, L
"..")))
222 wentry
= _wreaddir (dir
->wdirp
);
227 utf8_name
= g_utf16_to_utf8 (wentry
->d_name
, -1, NULL
, NULL
, NULL
);
229 if (utf8_name
== NULL
)
230 continue; /* Huh, impossible? Skip it anyway */
232 strcpy (dir
->utf8_buf
, utf8_name
);
235 return dir
->utf8_buf
;
238 entry
= readdir (dir
->dirp
);
240 && (0 == strcmp (entry
->d_name
, ".") ||
241 0 == strcmp (entry
->d_name
, "..")))
242 entry
= readdir (dir
->dirp
);
245 return entry
->d_name
;
251 #if defined (G_OS_WIN32) && !defined (_WIN64)
253 /* Ditto for g_dir_read_name */
255 #undef g_dir_read_name
257 /* Binary compatibility version. Not for newly compiled code. */
260 g_dir_read_name (GDir
*dir
)
264 const gchar
*utf8_name
= g_dir_read_name_utf8 (dir
);
267 if (utf8_name
== NULL
)
270 retval
= g_locale_from_utf8 (utf8_name
, -1, NULL
, NULL
, NULL
);
274 strcpy (dir
->utf8_buf
, retval
);
277 return dir
->utf8_buf
;
286 * @dir: a #GDir* created by g_dir_open()
288 * Resets the given directory. The next call to g_dir_read_name()
289 * will return the first entry again.
292 g_dir_rewind (GDir
*dir
)
294 g_return_if_fail (dir
!= NULL
);
297 _wrewinddir (dir
->wdirp
);
299 rewinddir (dir
->dirp
);
305 * @dir: a #GDir* created by g_dir_open()
307 * Closes the directory and deallocates all related resources.
310 g_dir_close (GDir
*dir
)
312 g_return_if_fail (dir
!= NULL
);
315 _wclosedir (dir
->wdirp
);
317 closedir (dir
->dirp
);