Minor doc markup fix.
[glib.git] / glib / gdir.c
blob1fa3644b817a928bf66b39f3cbd4f8e6fd6d2ebc
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.
24 #include "config.h"
26 #include <errno.h>
27 #include <string.h> /* strcmp */
29 #ifdef HAVE_DIRENT_H
30 #include <sys/types.h>
31 #include <dirent.h>
32 #endif
34 #include "glib.h"
35 #include "gdir.h"
37 #include "glibintl.h"
39 struct _GDir
41 DIR *dir;
44 /**
45 * g_dir_open:
46 * @path: the path to the directory you are interested in
47 * @flags: Currently must be set to 0. Reserved for future use.
48 * @error: return location for a #GError, or %NULL.
49 * If non-%NULL, an error will be set if and only if
50 * g_dir_open_fails.
52 * Opens a directory for reading. The names of the files
53 * in the directory can then be retrieved using
54 * g_dir_read_name().
56 * Return value: a newly allocated #GDir on success, %NULL on failure.
57 * If non-%NULL, you must free the result with g_dir_close()
58 * when you are finished with it.
59 **/
60 GDir *
61 g_dir_open (const gchar *path,
62 guint flags,
63 GError **error)
65 GDir *dir;
67 g_return_val_if_fail (path != NULL, NULL);
69 dir = g_new (GDir, 1);
71 dir->dir = opendir (path);
73 if (dir->dir)
74 return dir;
76 /* error case */
77 g_set_error (error,
78 G_FILE_ERROR,
79 g_file_error_from_errno (errno),
80 _("Error opening directory '%s': %s"),
81 path, g_strerror (errno));
83 g_free (dir);
84 return NULL;
87 /**
88 * g_dir_read_name:
89 * @dir: a #GDir* created by g_dir_open()
91 * Retrieves the name of the next entry in the directory.
92 * The '.' and '..' entries are omitted.
94 * Return value: The entries name or %NULL if there are no
95 * more entries. The return value is owned by GLib and
96 * must not be modified or freed.
97 **/
98 G_CONST_RETURN gchar*
99 g_dir_read_name (GDir *dir)
101 struct dirent *entry;
103 g_return_val_if_fail (dir != NULL, NULL);
105 entry = readdir (dir->dir);
106 while (entry
107 && (0 == strcmp (entry->d_name, ".") ||
108 0 == strcmp (entry->d_name, "..")))
109 entry = readdir (dir->dir);
111 if (entry)
112 return entry->d_name;
113 else
114 return NULL;
118 * g_dir_rewind:
119 * @dir: a #GDir* created by g_dir_open()
121 * Resets the given directory. The next call to g_dir_read_name()
122 * will return the first entry again.
124 void
125 g_dir_rewind (GDir *dir)
127 g_return_if_fail (dir != NULL);
129 rewinddir (dir->dir);
133 * g_dir_close:
134 * @dir: a #GDir* created by g_dir_open()
136 * Closes the directory and deallocates all related resources.
138 void
139 g_dir_close (GDir *dir)
141 g_return_if_fail (dir != NULL);
143 closedir (dir->dir);
144 g_free (dir);