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
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include <string.h> /* strerror, strcmp */
44 * @path: the path to the directory you are interested in
45 * @flags: for future binary compatible extensions
46 * @error: return location for a #GError
48 * Return value: a #GDir* on success, NULL if error is set
51 g_dir_open (const gchar *path,
55 GDir *dir = g_new (GDir, 1);
57 dir->dir = opendir (path);
65 g_file_error_from_errno (errno),
66 _("Error opening dir '%s': %s"),
67 path, strerror (errno));
75 * @dir: a #GDir* created by g_dir_open()
77 * Iterator which delivers the next directory entries name
78 * with each call. The '.' and '..' entries are omitted.
80 * BTW: using these functions will simplify porting of
81 * your app, at least to Windows.
83 * Return value: The entries name or NULL if there are no
84 * more entries. Don't free this value!
87 g_dir_read_name (GDir *dir)
91 g_return_val_if_fail (dir != NULL, NULL);
93 entry = readdir (dir->dir);
95 && ( 0 == strcmp (entry->d_name, ".")
96 || 0 == strcmp (entry->d_name, "..")))
97 entry = readdir (dir->dir);
104 * @dir: a #GDir* created by g_dir_open()
106 * Resets the given directory. The next call to g_dir_read_name()
107 * will return the first entry again.
110 g_dir_rewind (GDir *dir)
112 g_return_if_fail (dir != NULL);
114 rewinddir (dir->dir);
119 * @dir: a #GDir* created by g_dir_open()
121 * Closes the directory and deallocates all related resources.
123 * Return value: TRUE on success, FALSE otherwise.
126 g_dir_close (GDir *dir)
130 g_return_val_if_fail (dir != NULL, FALSE);
132 ret = closedir (dir->dir);