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"
53 * An opaque structure representing an opened directory.
64 gchar utf8_buf
[FILENAME_MAX
*4];
70 * @path: the path to the directory you are interested in. On Unix
71 * in the on-disk encoding. On Windows in UTF-8
72 * @flags: Currently must be set to 0. Reserved for future use.
73 * @error: return location for a #GError, or %NULL.
74 * If non-%NULL, an error will be set if and only if
77 * Opens a directory for reading. The names of the files in the
78 * directory can then be retrieved using g_dir_read_name(). Note
79 * that the ordering is not defined.
81 * Return value: a newly allocated #GDir on success, %NULL on failure.
82 * If non-%NULL, you must free the result with g_dir_close()
83 * when you are finished with it.
86 g_dir_open (const gchar
*path
,
98 g_return_val_if_fail (path
!= NULL
, NULL
);
101 wpath
= g_utf8_to_utf16 (path
, -1, NULL
, NULL
, error
);
106 dir
= g_new (GDir
, 1);
108 dir
->wdirp
= _wopendir (wpath
);
119 g_file_error_from_errno (errsv
),
120 _("Error opening directory '%s': %s"),
121 path
, g_strerror (errsv
));
127 dir
= g_new (GDir
, 1);
129 dir
->dirp
= opendir (path
);
137 utf8_path
= g_filename_to_utf8 (path
, -1,
142 g_file_error_from_errno (errsv
),
143 _("Error opening directory '%s': %s"),
144 utf8_path
, g_strerror (errsv
));
153 #if defined (G_OS_WIN32) && !defined (_WIN64)
155 /* The above function actually is called g_dir_open_utf8, and it's
156 * that what applications compiled with this GLib version will
162 /* Binary compatibility version. Not for newly compiled code. */
165 g_dir_open (const gchar
*path
,
169 gchar
*utf8_path
= g_locale_to_utf8 (path
, -1, NULL
, NULL
, error
);
172 if (utf8_path
== NULL
)
175 retval
= g_dir_open_utf8 (utf8_path
, flags
, error
);
185 * @dir: a #GDir* created by g_dir_open()
187 * Retrieves the name of another entry in the directory, or %NULL.
188 * The order of entries returned from this function is not defined,
189 * and may vary by file system or other operating-system dependent
192 * %NULL may also be returned in case of errors. On Unix, you can
193 * check <literal>errno</literal> to find out if %NULL was returned
194 * because of an error.
196 * On Unix, the '.' and '..' entries are omitted, and the returned
197 * name is in the on-disk encoding.
199 * On Windows, as is true of all GLib functions which operate on
200 * filenames, the returned name is in UTF-8.
202 * Return value: The entry's name or %NULL if there are no
203 * more entries. The return value is owned by GLib and
204 * must not be modified or freed.
207 g_dir_read_name (GDir
*dir
)
211 struct _wdirent
*wentry
;
213 struct dirent
*entry
;
216 g_return_val_if_fail (dir
!= NULL
, NULL
);
221 wentry
= _wreaddir (dir
->wdirp
);
223 && (0 == wcscmp (wentry
->d_name
, L
".") ||
224 0 == wcscmp (wentry
->d_name
, L
"..")))
225 wentry
= _wreaddir (dir
->wdirp
);
230 utf8_name
= g_utf16_to_utf8 (wentry
->d_name
, -1, NULL
, NULL
, NULL
);
232 if (utf8_name
== NULL
)
233 continue; /* Huh, impossible? Skip it anyway */
235 strcpy (dir
->utf8_buf
, utf8_name
);
238 return dir
->utf8_buf
;
241 entry
= readdir (dir
->dirp
);
243 && (0 == strcmp (entry
->d_name
, ".") ||
244 0 == strcmp (entry
->d_name
, "..")))
245 entry
= readdir (dir
->dirp
);
248 return entry
->d_name
;
254 #if defined (G_OS_WIN32) && !defined (_WIN64)
256 /* Ditto for g_dir_read_name */
258 #undef g_dir_read_name
260 /* Binary compatibility version. Not for newly compiled code. */
263 g_dir_read_name (GDir
*dir
)
267 const gchar
*utf8_name
= g_dir_read_name_utf8 (dir
);
270 if (utf8_name
== NULL
)
273 retval
= g_locale_from_utf8 (utf8_name
, -1, NULL
, NULL
, NULL
);
277 strcpy (dir
->utf8_buf
, retval
);
280 return dir
->utf8_buf
;
289 * @dir: a #GDir* created by g_dir_open()
291 * Resets the given directory. The next call to g_dir_read_name()
292 * will return the first entry again.
295 g_dir_rewind (GDir
*dir
)
297 g_return_if_fail (dir
!= NULL
);
300 _wrewinddir (dir
->wdirp
);
302 rewinddir (dir
->dirp
);
308 * @dir: a #GDir* created by g_dir_open()
310 * Closes the directory and deallocates all related resources.
313 g_dir_close (GDir
*dir
)
315 g_return_if_fail (dir
!= NULL
);
318 _wclosedir (dir
->wdirp
);
320 closedir (dir
->dirp
);