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.1 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, see <http://www.gnu.org/licenses/>.
31 #include <sys/types.h>
38 #include "gfileutils.h"
39 #include "gstrfuncs.h"
40 #include "gtestutils.h"
43 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
44 #include "../build/win32/dirent/dirent.h"
45 #include "../build/win32/dirent/wdirent.c"
48 #include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
53 * An opaque structure representing an opened directory.
64 gchar utf8_buf
[FILENAME_MAX
*4];
69 * g_dir_open_with_errno:
70 * @path: the path to the directory you are interested in.
71 * @flags: Currently must be set to 0. Reserved for future use.
73 * Opens a directory for reading.
75 * This function is equivalent to g_dir_open() except in the error case,
76 * errno will be set accordingly.
78 * This is useful if you want to construct your own error message.
80 * Returns: a newly allocated #GDir on success, or %NULL on failure,
81 * with errno set accordingly.
86 g_dir_open_with_errno (const gchar
*path
,
95 g_return_val_if_fail (path
!= NULL
, NULL
);
98 wpath
= g_utf8_to_utf16 (path
, -1, NULL
, NULL
, NULL
);
100 g_return_val_if_fail (wpath
!= NULL
, NULL
);
102 dir
.wdirp
= _wopendir (wpath
);
107 if (dir
.wdirp
== NULL
)
110 dir
.dirp
= opendir (path
);
112 if (dir
.dirp
== NULL
)
116 return g_memdup (&dir
, sizeof dir
);
121 * @path: the path to the directory you are interested in. On Unix
122 * in the on-disk encoding. On Windows in UTF-8
123 * @flags: Currently must be set to 0. Reserved for future use.
124 * @error: return location for a #GError, or %NULL.
125 * If non-%NULL, an error will be set if and only if
126 * g_dir_open() fails.
128 * Opens a directory for reading. The names of the files in the
129 * directory can then be retrieved using g_dir_read_name(). Note
130 * that the ordering is not defined.
132 * Returns: a newly allocated #GDir on success, %NULL on failure.
133 * If non-%NULL, you must free the result with g_dir_close()
134 * when you are finished with it.
137 g_dir_open (const gchar
*path
,
144 dir
= g_dir_open_with_errno (path
, flags
);
152 utf8_path
= g_filename_to_utf8 (path
, -1, NULL
, NULL
, NULL
);
154 g_set_error (error
, G_FILE_ERROR
, g_file_error_from_errno (saved_errno
),
155 _("Error opening directory ā%sā: %s"), utf8_path
, g_strerror (saved_errno
));
163 * g_dir_new_from_dirp:
164 * @dirp: a #DIR* created by opendir() or fdopendir()
166 * Creates a #GDir object from the DIR object that is created using
167 * opendir() or fdopendir(). The created #GDir assumes ownership of the
168 * passed-in #DIR pointer.
170 * @dirp must not be %NULL.
172 * This function never fails.
174 * Returns: a newly allocated #GDir, which should be closed using
180 g_dir_new_from_dirp (gpointer dirp
)
185 g_return_val_if_fail (dirp
!= NULL
, NULL
);
187 dir
= g_new (GDir
, 1);
192 g_assert_not_reached ();
198 * @dir: a #GDir* created by g_dir_open()
200 * Retrieves the name of another entry in the directory, or %NULL.
201 * The order of entries returned from this function is not defined,
202 * and may vary by file system or other operating-system dependent
205 * %NULL may also be returned in case of errors. On Unix, you can
206 * check `errno` to find out if %NULL was returned because of an error.
208 * On Unix, the '.' and '..' entries are omitted, and the returned
209 * name is in the on-disk encoding.
211 * On Windows, as is true of all GLib functions which operate on
212 * filenames, the returned name is in UTF-8.
214 * Returns: (type filename): The entry's name or %NULL if there are no
215 * more entries. The return value is owned by GLib and
216 * must not be modified or freed.
219 g_dir_read_name (GDir
*dir
)
223 struct _wdirent
*wentry
;
225 struct dirent
*entry
;
228 g_return_val_if_fail (dir
!= NULL
, NULL
);
233 wentry
= _wreaddir (dir
->wdirp
);
235 && (0 == wcscmp (wentry
->d_name
, L
".") ||
236 0 == wcscmp (wentry
->d_name
, L
"..")))
237 wentry
= _wreaddir (dir
->wdirp
);
242 utf8_name
= g_utf16_to_utf8 (wentry
->d_name
, -1, NULL
, NULL
, NULL
);
244 if (utf8_name
== NULL
)
245 continue; /* Huh, impossible? Skip it anyway */
247 strcpy (dir
->utf8_buf
, utf8_name
);
250 return dir
->utf8_buf
;
253 entry
= readdir (dir
->dirp
);
255 && (0 == strcmp (entry
->d_name
, ".") ||
256 0 == strcmp (entry
->d_name
, "..")))
257 entry
= readdir (dir
->dirp
);
260 return entry
->d_name
;
268 * @dir: a #GDir* created by g_dir_open()
270 * Resets the given directory. The next call to g_dir_read_name()
271 * will return the first entry again.
274 g_dir_rewind (GDir
*dir
)
276 g_return_if_fail (dir
!= NULL
);
279 _wrewinddir (dir
->wdirp
);
281 rewinddir (dir
->dirp
);
287 * @dir: a #GDir* created by g_dir_open()
289 * Closes the directory and deallocates all related resources.
292 g_dir_close (GDir
*dir
)
294 g_return_if_fail (dir
!= NULL
);
297 _wclosedir (dir
->wdirp
);
299 closedir (dir
->dirp
);
306 /* Binary compatibility versions. Not for newly compiled code. */
308 _GLIB_EXTERN GDir
*g_dir_open_utf8 (const gchar
*path
,
311 _GLIB_EXTERN
const gchar
*g_dir_read_name_utf8 (GDir
*dir
);
314 g_dir_open_utf8 (const gchar
*path
,
318 return g_dir_open (path
, flags
, error
);
322 g_dir_read_name_utf8 (GDir
*dir
)
324 return g_dir_read_name (dir
);