Remove support for Windows 9x/ME, as will be done also in Pango and GTK+.
[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  * Copyright 2004 Tor Lillqvist
8  *
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 of the License, or (at your option) any later version.
13  *
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.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31 #ifdef HAVE_DIRENT_H
32 #include <sys/types.h>
33 #include <dirent.h>
34 #endif
35
36 #include "glib.h"
37 #include "gdir.h"
38
39 #include "glibintl.h"
40
41 #include "galias.h"
42
43 struct _GDir
44 {
45 #ifdef G_OS_WIN32
46   _WDIR *wdirp;
47 #else
48   DIR *dirp;
49 #endif
50 #ifdef G_OS_WIN32
51   gchar utf8_buf[FILENAME_MAX*4];
52 #endif
53 };
54
55 /**
56  * g_dir_open:
57  * @path: the path to the directory you are interested in. On Unix
58  *         in the on-disk encoding. On Windows in UTF-8
59  * @flags: Currently must be set to 0. Reserved for future use.
60  * @error: return location for a #GError, or %NULL.
61  *         If non-%NULL, an error will be set if and only if
62  *         g_dir_open() fails.
63  *
64  * Opens a directory for reading. The names of the files in the
65  * directory can then be retrieved using g_dir_read_name().
66  *
67  * Return value: a newly allocated #GDir on success, %NULL on failure.
68  *   If non-%NULL, you must free the result with g_dir_close()
69  *   when you are finished with it.
70  **/
71 GDir *
72 g_dir_open (const gchar  *path,
73             guint         flags,
74             GError      **error)
75 {
76   GDir *dir;
77 #ifdef G_OS_WIN32
78   wchar_t *wpath;
79 #else
80   gchar *utf8_path;
81 #endif
82
83   g_return_val_if_fail (path != NULL, NULL);
84
85 #ifdef G_OS_WIN32
86   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
87
88   if (wpath == NULL)
89     return NULL;
90
91   dir = g_new (GDir, 1);
92
93   dir->wdirp = _wopendir (wpath);
94   g_free (wpath);
95
96   if (dir->wdirp)
97     return dir;
98
99   /* error case */
100
101   g_set_error (error,
102                G_FILE_ERROR,
103                g_file_error_from_errno (errno),
104                _("Error opening directory '%s': %s"),
105                path, g_strerror (errno));
106   
107   g_free (dir);
108       
109   return NULL;
110 #else
111   dir = g_new (GDir, 1);
112
113   dir->dirp = opendir (path);
114
115   if (dir->dirp)
116     return dir;
117
118   /* error case */
119   utf8_path = g_filename_to_utf8 (path, -1,
120                                   NULL, NULL, NULL);
121   g_set_error (error,
122                G_FILE_ERROR,
123                g_file_error_from_errno (errno),
124                _("Error opening directory '%s': %s"),
125                utf8_path, g_strerror (errno));
126
127   g_free (utf8_path);
128   g_free (dir);
129
130   return NULL;
131 #endif
132 }
133
134 #ifdef G_OS_WIN32
135
136 /* The above function actually is called g_dir_open_utf8, and it's
137  * that what applications compiled with this GLib version will
138  * use.
139  */
140
141 #undef g_dir_open
142
143 /* Binary compatibility version. Not for newly compiled code. */
144
145 GDir *
146 g_dir_open (const gchar  *path,
147             guint         flags,
148             GError      **error)
149 {
150   gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
151   GDir *retval;
152
153   if (utf8_path == NULL)
154     return NULL;
155
156   retval = g_dir_open_utf8 (utf8_path, flags, error);
157
158   g_free (utf8_path);
159
160   return retval;
161 }
162 #endif
163
164 /**
165  * g_dir_read_name:
166  * @dir: a #GDir* created by g_dir_open()
167  *
168  * Retrieves the name of the next entry in the directory.  The '.' and
169  * '..' entries are omitted. On Windows, the returned name is in
170  * UTF-8. On Unix, it is in the on-disk encoding.
171  *
172  * Return value: The entry's name or %NULL if there are no 
173  *   more entries. The return value is owned by GLib and
174  *   must not be modified or freed.
175  **/
176 G_CONST_RETURN gchar*
177 g_dir_read_name (GDir *dir)
178 {
179 #ifdef G_OS_WIN32
180   gchar *utf8_name;
181   struct _wdirent *wentry;
182 #else
183   struct dirent *entry;
184 #endif
185
186   g_return_val_if_fail (dir != NULL, NULL);
187
188 #ifdef G_OS_WIN32
189   while (1)
190     {
191       wentry = _wreaddir (dir->wdirp);
192       while (wentry 
193              && (0 == wcscmp (wentry->d_name, L".") ||
194                  0 == wcscmp (wentry->d_name, L"..")))
195         wentry = _wreaddir (dir->wdirp);
196
197       if (wentry == NULL)
198         return NULL;
199
200       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
201
202       if (utf8_name == NULL)
203         continue;               /* Huh, impossible? Skip it anyway */
204
205       strcpy (dir->utf8_buf, utf8_name);
206       g_free (utf8_name);
207
208       return dir->utf8_buf;
209     }
210 #else
211   entry = readdir (dir->dirp);
212   while (entry 
213          && (0 == strcmp (entry->d_name, ".") ||
214              0 == strcmp (entry->d_name, "..")))
215     entry = readdir (dir->dirp);
216
217   if (entry)
218     return entry->d_name;
219   else
220     return NULL;
221 #endif
222 }
223
224 #ifdef G_OS_WIN32
225
226 /* Ditto for g_dir_read_name */
227
228 #undef g_dir_read_name
229
230 /* Binary compatibility version. Not for newly compiled code. */
231
232 G_CONST_RETURN gchar*
233 g_dir_read_name (GDir *dir)
234 {
235   while (1)
236     {
237       const gchar *utf8_name = g_dir_read_name_utf8 (dir);
238       gchar *retval;
239       
240       if (utf8_name == NULL)
241         return NULL;
242
243       retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
244
245       if (retval != NULL)
246         {
247           strcpy (dir->utf8_buf, retval);
248           g_free (retval);
249
250           return dir->utf8_buf;
251         }
252     }
253 }
254
255 #endif
256
257 /**
258  * g_dir_rewind:
259  * @dir: a #GDir* created by g_dir_open()
260  *
261  * Resets the given directory. The next call to g_dir_read_name()
262  * will return the first entry again.
263  **/
264 void
265 g_dir_rewind (GDir *dir)
266 {
267   g_return_if_fail (dir != NULL);
268   
269 #ifdef G_OS_WIN32
270   _wrewinddir (dir->wdirp);
271 #else
272   rewinddir (dir->dirp);
273 #endif
274 }
275
276 /**
277  * g_dir_close:
278  * @dir: a #GDir* created by g_dir_open()
279  *
280  * Closes the directory and deallocates all related resources.
281  **/
282 void
283 g_dir_close (GDir *dir)
284 {
285   g_return_if_fail (dir != NULL);
286
287 #ifdef G_OS_WIN32
288   _wclosedir (dir->wdirp);
289 #else
290   closedir (dir->dirp);
291 #endif
292   g_free (dir);
293 }
294
295 #define __G_DIR_C__
296 #include "galiasdef.c"