gdir.c 6.76 KB
Newer Older
1 2 3 4 5 6
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * gdir.c: Simplified wrapper around the DIRENT functions.
 *
 * Copyright 2001 Hans Breuer
7
 * Copyright 2004 Tor Lillqvist
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

Owen Taylor's avatar
Owen Taylor committed
27
#include <errno.h>
28
#include <string.h>
29
#include <stdio.h>
30
#include <sys/stat.h>
Owen Taylor's avatar
Owen Taylor committed
31

32
#ifdef HAVE_DIRENT_H
33
#include <sys/types.h>
34 35 36 37 38
#include <dirent.h>
#endif

#include "gdir.h"

Matthias Clasen's avatar
Matthias Clasen committed
39 40
#include "gconvert.h"
#include "gfileutils.h"
41
#include "gstrfuncs.h"
Matthias Clasen's avatar
Matthias Clasen committed
42
#include "gtestutils.h"
43 44
#include "glibintl.h"

45

46 47 48 49 50
#if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
#include "../build/win32/dirent/dirent.h"
#include "../build/win32/dirent/wdirent.c"
#endif

51 52
struct _GDir
{
53
#ifdef G_OS_WIN32
54 55 56
  _WDIR *wdirp;
#else
  DIR *dirp;
57 58 59 60
#endif
#ifdef G_OS_WIN32
  gchar utf8_buf[FILENAME_MAX*4];
#endif
61 62 63 64
};

/**
 * g_dir_open:
65 66
 * @path: the path to the directory you are interested in. On Unix
 *         in the on-disk encoding. On Windows in UTF-8
67 68 69
 * @flags: Currently must be set to 0. Reserved for future use.
 * @error: return location for a #GError, or %NULL.
 *         If non-%NULL, an error will be set if and only if
Matthias Clasen's avatar
Matthias Clasen committed
70
 *         g_dir_open() fails.
71
 *
72
 * Opens a directory for reading. The names of the files in the
73 74
 * directory can then be retrieved using g_dir_read_name().  Note
 * that the ordering is not defined.
75 76 77 78
 *
 * Return value: a newly allocated #GDir on success, %NULL on failure.
 *   If non-%NULL, you must free the result with g_dir_close()
 *   when you are finished with it.
79 80 81 82 83 84
 **/
GDir *
g_dir_open (const gchar  *path,
            guint         flags,
            GError      **error)
{
85
  GDir *dir;
Christian Persch's avatar
Christian Persch committed
86
  int errsv;
87 88 89
#ifdef G_OS_WIN32
  wchar_t *wpath;
#else
90
  gchar *utf8_path;
91
#endif
92 93 94

  g_return_val_if_fail (path != NULL, NULL);

95
#ifdef G_OS_WIN32
96
  wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
97

98 99
  if (wpath == NULL)
    return NULL;
100

101
  dir = g_new (GDir, 1);
102

103 104
  dir->wdirp = _wopendir (wpath);
  g_free (wpath);
105

106 107
  if (dir->wdirp)
    return dir;
108 109

  /* error case */
Christian Persch's avatar
Christian Persch committed
110
  errsv = errno;
111 112 113

  g_set_error (error,
	       G_FILE_ERROR,
Christian Persch's avatar
Christian Persch committed
114
	       g_file_error_from_errno (errsv),
115
	       _("Error opening directory '%s': %s"),
Christian Persch's avatar
Christian Persch committed
116
	       path, g_strerror (errsv));
117 118 119 120 121
  
  g_free (dir);
      
  return NULL;
#else
122
  dir = g_new (GDir, 1);
123

124
  dir->dirp = opendir (path);
125

126
  if (dir->dirp)
127 128 129
    return dir;

  /* error case */
Christian Persch's avatar
Christian Persch committed
130 131
  errsv = errno;

132 133
  utf8_path = g_filename_to_utf8 (path, -1,
				  NULL, NULL, NULL);
Christian Persch's avatar
Christian Persch committed
134

135 136
  g_set_error (error,
               G_FILE_ERROR,
Christian Persch's avatar
Christian Persch committed
137
               g_file_error_from_errno (errsv),
138
               _("Error opening directory '%s': %s"),
Christian Persch's avatar
Christian Persch committed
139
	       utf8_path, g_strerror (errsv));
140

141
  g_free (utf8_path);
142
  g_free (dir);
143

144
  return NULL;
145
#endif
146 147
}

148
#if defined (G_OS_WIN32) && !defined (_WIN64)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

/* The above function actually is called g_dir_open_utf8, and it's
 * that what applications compiled with this GLib version will
 * use.
 */

#undef g_dir_open

/* Binary compatibility version. Not for newly compiled code. */

GDir *
g_dir_open (const gchar  *path,
            guint         flags,
            GError      **error)
{
  gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
  GDir *retval;

  if (utf8_path == NULL)
    return NULL;

  retval = g_dir_open_utf8 (utf8_path, flags, error);

  g_free (utf8_path);

  return retval;
}
#endif

178 179 180 181
/**
 * g_dir_read_name:
 * @dir: a #GDir* created by g_dir_open()
 *
182 183 184 185 186 187 188 189 190 191
 * Retrieves the name of another entry in the directory, or %NULL.
 * The order of entries returned from this function is not defined,
 * and may vary by file system or other operating-system dependent
 * factors. 
 *
 * On Unix, the '.' and '..' entries are omitted, and the returned
 * name is in the on-disk encoding.
 *
 * On Windows, as is true of all GLib functions which operate on
 * filenames, the returned name is in UTF-8.
192
 *
193
 * Return value: The entry's name or %NULL if there are no 
194 195
 *   more entries. The return value is owned by GLib and
 *   must not be modified or freed.
196
 **/
197
const gchar *
198
g_dir_read_name (GDir *dir)
199
{
200 201 202 203
#ifdef G_OS_WIN32
  gchar *utf8_name;
  struct _wdirent *wentry;
#else
204
  struct dirent *entry;
205
#endif
206 207 208

  g_return_val_if_fail (dir != NULL, NULL);

209
#ifdef G_OS_WIN32
210
  while (1)
211
    {
212 213 214 215 216
      wentry = _wreaddir (dir->wdirp);
      while (wentry 
	     && (0 == wcscmp (wentry->d_name, L".") ||
		 0 == wcscmp (wentry->d_name, L"..")))
	wentry = _wreaddir (dir->wdirp);
217

218 219 220 221 222 223 224 225 226 227 228 229
      if (wentry == NULL)
	return NULL;

      utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);

      if (utf8_name == NULL)
	continue;		/* Huh, impossible? Skip it anyway */

      strcpy (dir->utf8_buf, utf8_name);
      g_free (utf8_name);

      return dir->utf8_buf;
230 231
    }
#else
232
  entry = readdir (dir->dirp);
233
  while (entry 
234 235
         && (0 == strcmp (entry->d_name, ".") ||
             0 == strcmp (entry->d_name, "..")))
236
    entry = readdir (dir->dirp);
237

238 239 240 241
  if (entry)
    return entry->d_name;
  else
    return NULL;
242 243 244
#endif
}

245
#if defined (G_OS_WIN32) && !defined (_WIN64)
246 247 248 249 250 251 252

/* Ditto for g_dir_read_name */

#undef g_dir_read_name

/* Binary compatibility version. Not for newly compiled code. */

253
const gchar *
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
g_dir_read_name (GDir *dir)
{
  while (1)
    {
      const gchar *utf8_name = g_dir_read_name_utf8 (dir);
      gchar *retval;
      
      if (utf8_name == NULL)
	return NULL;

      retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);

      if (retval != NULL)
	{
	  strcpy (dir->utf8_buf, retval);
	  g_free (retval);

	  return dir->utf8_buf;
	}
    }
274 275
}

276 277
#endif

278 279 280 281 282 283 284 285 286 287 288 289
/**
 * g_dir_rewind:
 * @dir: a #GDir* created by g_dir_open()
 *
 * Resets the given directory. The next call to g_dir_read_name()
 * will return the first entry again.
 **/
void
g_dir_rewind (GDir *dir)
{
  g_return_if_fail (dir != NULL);
  
290
#ifdef G_OS_WIN32
291 292 293
  _wrewinddir (dir->wdirp);
#else
  rewinddir (dir->dirp);
294
#endif
295 296 297 298 299 300 301 302
}

/**
 * g_dir_close:
 * @dir: a #GDir* created by g_dir_open()
 *
 * Closes the directory and deallocates all related resources.
 **/
303
void
304 305
g_dir_close (GDir *dir)
{
Owen Taylor's avatar
Owen Taylor committed
306
  g_return_if_fail (dir != NULL);
307

308
#ifdef G_OS_WIN32
309 310 311
  _wclosedir (dir->wdirp);
#else
  closedir (dir->dirp);
312
#endif
313 314
  g_free (dir);
}