simplified wrapper around dirent functions to improve portability of
[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 <string.h> /* strerror, strcmp */
27 #ifdef HAVE_DIRENT_H
28 #include <dirent.h>
29 #endif
30
31 #include "glib.h"
32 #include "gdir.h"
33
34 #include "glibintl.h"
35
36 struct _GDir
37 {
38   /*< private >*/
39   DIR *dir;
40 };
41
42 /**
43  * g_dir_open:
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
47  *
48  * Return value: a #GDir* on success, NULL if error is set
49  **/
50 GDir *
51 g_dir_open (const gchar  *path,
52             guint         flags,
53             GError      **error)
54 {
55   GDir *dir = g_new (GDir, 1);
56
57   dir->dir = opendir (path);
58
59   if (dir->dir)
60     return dir;
61
62   /* error case */
63   g_set_error (error,
64                G_FILE_ERROR,
65                g_file_error_from_errno (errno),
66                _("Error opening dir '%s': %s"),
67                  path, strerror (errno));
68
69   g_free (dir);
70   return NULL;
71 }
72
73 /**
74  * g_dir_read_name:
75  * @dir: a #GDir* created by g_dir_open()
76  *
77  * Iterator which delivers the next directory entries name
78  * with each call. The '.' and '..' entries are omitted.
79  *
80  * BTW: using these functions will simplify porting of
81  * your app, at least to Windows.
82  *
83  * Return value: The entries name or NULL if there are no 
84  * more entries. Don't free this value!
85  **/
86 G_CONST_RETURN gchar*
87 g_dir_read_name (GDir    *dir)
88 {
89   struct dirent *entry;
90
91   g_return_val_if_fail (dir != NULL, NULL);
92
93   entry = readdir (dir->dir);
94   while (entry 
95          && (   0 == strcmp (entry->d_name, ".") 
96              || 0 == strcmp (entry->d_name, "..")))
97     entry = readdir (dir->dir);
98
99   return entry->d_name;
100 }
101
102 /**
103  * g_dir_rewind:
104  * @dir: a #GDir* created by g_dir_open()
105  *
106  * Resets the given directory. The next call to g_dir_read_name()
107  * will return the first entry again.
108  **/
109 void
110 g_dir_rewind (GDir *dir)
111 {
112   g_return_if_fail (dir != NULL);
113   
114   rewinddir (dir->dir);
115 }
116
117 /**
118  * g_dir_close:
119  * @dir: a #GDir* created by g_dir_open()
120  *
121  * Closes the directory and deallocates all related resources.
122  *
123  * Return value: TRUE on success, FALSE otherwise.
124  **/
125 gboolean
126 g_dir_close (GDir *dir)
127 {
128   int ret = 0;
129
130   g_return_val_if_fail (dir != NULL, FALSE);
131
132   ret = closedir (dir->dir);
133   g_free (dir);
134
135   return !ret; 
136 }