g_dir_open: added g_return_val_if_fail() to prevent us from calling
[platform/upstream/glib.git] / glib / gdir.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gdir.c: Simplified wrapper around the DIRENT functions.
5  *
6  * Copyright 2001 Hans Breuer
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <string.h> /* strerror, strcmp */
28
29 #ifdef HAVE_DIRENT_H
30 #include <dirent.h>
31 #endif
32
33 #include "glib.h"
34 #include "gdir.h"
35
36 #include "glibintl.h"
37
38 struct _GDir
39 {
40   DIR *dir;
41 };
42
43 /**
44  * g_dir_open:
45  * @path: the path to the directory you are interested in
46  * @flags: Currently must be set to 0. Reserved for future use.
47  * @error: return location for a #GError, or %NULL.
48  *         If non-%NULL, an error will be set if and only if
49  *         g_dir_open_fails.
50  *
51  * Opens a directory for reading. The names of the files
52  * in the directory can then be retrieved using
53  * g_dir_get_name().
54  *
55  * Return value: a newly allocated #GDir on success, %NULL on failure.
56  *   If non-%NULL, you must free the result with g_dir_close()
57  *   when you are finished with it.
58  **/
59 GDir *
60 g_dir_open (const gchar  *path,
61             guint         flags,
62             GError      **error)
63 {
64   GDir *dir;
65
66   g_return_val_if_fail (path != NULL, NULL);
67
68   dir = g_new (GDir, 1);
69
70   dir->dir = opendir (path);
71
72   if (dir->dir)
73     return dir;
74
75   /* error case */
76   g_set_error (error,
77                G_FILE_ERROR,
78                g_file_error_from_errno (errno),
79                _("Error opening directory '%s': %s"),
80                path, strerror (errno));
81
82   g_free (dir);
83   return NULL;
84 }
85
86 /**
87  * g_dir_read_name:
88  * @dir: a #GDir* created by g_dir_open()
89  *
90  * Retrieves the name of the next entry in the directory.
91  * The '.' and '..' entries are omitted.
92  *
93  * Return value: The entries name or %NULL if there are no 
94  *   more entries. The return value is owned by GLib and
95  *   must not be modified or freed.
96  **/
97 G_CONST_RETURN gchar*
98 g_dir_read_name (GDir *dir)
99 {
100   struct dirent *entry;
101
102   g_return_val_if_fail (dir != NULL, NULL);
103
104   entry = readdir (dir->dir);
105   while (entry 
106          && (0 == strcmp (entry->d_name, ".") ||
107              0 == strcmp (entry->d_name, "..")))
108     entry = readdir (dir->dir);
109
110   if (entry)
111     return entry->d_name;
112   else
113     return NULL;
114 }
115
116 /**
117  * g_dir_rewind:
118  * @dir: a #GDir* created by g_dir_open()
119  *
120  * Resets the given directory. The next call to g_dir_read_name()
121  * will return the first entry again.
122  **/
123 void
124 g_dir_rewind (GDir *dir)
125 {
126   g_return_if_fail (dir != NULL);
127   
128   rewinddir (dir->dir);
129 }
130
131 /**
132  * g_dir_close:
133  * @dir: a #GDir* created by g_dir_open()
134  *
135  * Closes the directory and deallocates all related resources.
136  **/
137 void
138 g_dir_close (GDir *dir)
139 {
140   g_return_if_fail (dir != NULL);
141
142   closedir (dir->dir);
143   g_free (dir);
144 }