Blame glib/gdir.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * gdir.c: Simplified wrapper around the DIRENT functions.
Packit ae235b
 *
Packit ae235b
 * Copyright 2001 Hans Breuer
Packit ae235b
 * Copyright 2004 Tor Lillqvist
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <errno.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
Packit ae235b
#ifdef HAVE_DIRENT_H
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <dirent.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gdir.h"
Packit ae235b
Packit ae235b
#include "gconvert.h"
Packit ae235b
#include "gfileutils.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
Packit ae235b
#include "../build/win32/dirent/dirent.h"
Packit ae235b
#include "../build/win32/dirent/wdirent.c"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glib-private.h" /* g_dir_open_with_errno, g_dir_new_from_dirp */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GDir:
Packit ae235b
 *
Packit ae235b
 * An opaque structure representing an opened directory.
Packit ae235b
 */
Packit ae235b
Packit ae235b
struct _GDir
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  _WDIR *wdirp;
Packit ae235b
#else
Packit ae235b
  DIR *dirp;
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  gchar utf8_buf[FILENAME_MAX*4];
Packit ae235b
#endif
Packit ae235b
};
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_dir_open_with_errno:
Packit ae235b
 * @path: the path to the directory you are interested in.
Packit ae235b
 * @flags: Currently must be set to 0. Reserved for future use.
Packit ae235b
 *
Packit ae235b
 * Opens a directory for reading.
Packit ae235b
 *
Packit ae235b
 * This function is equivalent to g_dir_open() except in the error case,
Packit ae235b
 * errno will be set accordingly.
Packit ae235b
 *
Packit ae235b
 * This is useful if you want to construct your own error message.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GDir on success, or %NULL on failure,
Packit ae235b
 *   with errno set accordingly.
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
GDir *
Packit ae235b
g_dir_open_with_errno (const gchar *path,
Packit ae235b
                       guint        flags)
Packit ae235b
{
Packit ae235b
  GDir dir;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  gint saved_errno;
Packit ae235b
  wchar_t *wpath;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_return_val_if_fail (path != NULL, NULL);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
  g_return_val_if_fail (wpath != NULL, NULL);
Packit ae235b
Packit ae235b
  dir.wdirp = _wopendir (wpath);
Packit ae235b
  saved_errno = errno;
Packit ae235b
  g_free (wpath);
Packit ae235b
  errno = saved_errno;
Packit ae235b
Packit ae235b
  if (dir.wdirp == NULL)
Packit ae235b
    return NULL;
Packit ae235b
#else
Packit ae235b
  dir.dirp = opendir (path);
Packit ae235b
Packit ae235b
  if (dir.dirp == NULL)
Packit ae235b
    return NULL;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return g_memdup (&dir, sizeof dir);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dir_open:
Packit ae235b
 * @path: the path to the directory you are interested in. On Unix
Packit ae235b
 *         in the on-disk encoding. On Windows in UTF-8
Packit ae235b
 * @flags: Currently must be set to 0. Reserved for future use.
Packit ae235b
 * @error: return location for a #GError, or %NULL.
Packit ae235b
 *         If non-%NULL, an error will be set if and only if
Packit ae235b
 *         g_dir_open() fails.
Packit ae235b
 *
Packit ae235b
 * Opens a directory for reading. The names of the files in the
Packit ae235b
 * directory can then be retrieved using g_dir_read_name().  Note
Packit ae235b
 * that the ordering is not defined.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GDir on success, %NULL on failure.
Packit ae235b
 *   If non-%NULL, you must free the result with g_dir_close()
Packit ae235b
 *   when you are finished with it.
Packit ae235b
 **/
Packit ae235b
GDir *
Packit ae235b
g_dir_open (const gchar  *path,
Packit ae235b
            guint         flags,
Packit ae235b
            GError      **error)
Packit ae235b
{
Packit ae235b
  gint saved_errno;
Packit ae235b
  GDir *dir;
Packit ae235b
Packit ae235b
  dir = g_dir_open_with_errno (path, flags);
Packit ae235b
Packit ae235b
  if (dir == NULL)
Packit ae235b
    {
Packit ae235b
      gchar *utf8_path;
Packit ae235b
Packit ae235b
      saved_errno = errno;
Packit ae235b
Packit ae235b
      utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (saved_errno),
Packit ae235b
                   _("Error opening directory ā€œ%sā€: %s"), utf8_path, g_strerror (saved_errno));
Packit ae235b
      g_free (utf8_path);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return dir;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*< private >
Packit ae235b
 * g_dir_new_from_dirp:
Packit ae235b
 * @dirp: a #DIR* created by opendir() or fdopendir()
Packit ae235b
 *
Packit ae235b
 * Creates a #GDir object from the DIR object that is created using
Packit ae235b
 * opendir() or fdopendir().  The created #GDir assumes ownership of the
Packit ae235b
 * passed-in #DIR pointer.
Packit ae235b
 *
Packit ae235b
 * @dirp must not be %NULL.
Packit ae235b
 *
Packit ae235b
 * This function never fails.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GDir, which should be closed using
Packit ae235b
 *     g_dir_close().
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 **/
Packit ae235b
GDir *
Packit ae235b
g_dir_new_from_dirp (gpointer dirp)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  GDir *dir;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (dirp != NULL, NULL);
Packit ae235b
Packit ae235b
  dir = g_new (GDir, 1);
Packit ae235b
  dir->dirp = dirp;
Packit ae235b
Packit ae235b
  return dir;
Packit ae235b
#else
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dir_read_name:
Packit ae235b
 * @dir: a #GDir* created by g_dir_open()
Packit ae235b
 *
Packit ae235b
 * Retrieves the name of another entry in the directory, or %NULL.
Packit ae235b
 * The order of entries returned from this function is not defined,
Packit ae235b
 * and may vary by file system or other operating-system dependent
Packit ae235b
 * factors.
Packit ae235b
 *
Packit ae235b
 * %NULL may also be returned in case of errors. On Unix, you can
Packit ae235b
 * check `errno` to find out if %NULL was returned because of an error.
Packit ae235b
 *
Packit ae235b
 * On Unix, the '.' and '..' entries are omitted, and the returned
Packit ae235b
 * name is in the on-disk encoding.
Packit ae235b
 *
Packit ae235b
 * On Windows, as is true of all GLib functions which operate on
Packit ae235b
 * filenames, the returned name is in UTF-8.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): The entry's name or %NULL if there are no
Packit ae235b
 *   more entries. The return value is owned by GLib and
Packit ae235b
 *   must not be modified or freed.
Packit ae235b
 **/
Packit ae235b
const gchar *
Packit ae235b
g_dir_read_name (GDir *dir)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  gchar *utf8_name;
Packit ae235b
  struct _wdirent *wentry;
Packit ae235b
#else
Packit ae235b
  struct dirent *entry;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_return_val_if_fail (dir != NULL, NULL);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      wentry = _wreaddir (dir->wdirp);
Packit ae235b
      while (wentry 
Packit ae235b
	     && (0 == wcscmp (wentry->d_name, L".") ||
Packit ae235b
		 0 == wcscmp (wentry->d_name, L"..")))
Packit ae235b
	wentry = _wreaddir (dir->wdirp);
Packit ae235b
Packit ae235b
      if (wentry == NULL)
Packit ae235b
	return NULL;
Packit ae235b
Packit ae235b
      utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
Packit ae235b
Packit ae235b
      if (utf8_name == NULL)
Packit ae235b
	continue;		/* Huh, impossible? Skip it anyway */
Packit ae235b
Packit ae235b
      strcpy (dir->utf8_buf, utf8_name);
Packit ae235b
      g_free (utf8_name);
Packit ae235b
Packit ae235b
      return dir->utf8_buf;
Packit ae235b
    }
Packit ae235b
#else
Packit ae235b
  entry = readdir (dir->dirp);
Packit ae235b
  while (entry 
Packit ae235b
         && (0 == strcmp (entry->d_name, ".") ||
Packit ae235b
             0 == strcmp (entry->d_name, "..")))
Packit ae235b
    entry = readdir (dir->dirp);
Packit ae235b
Packit ae235b
  if (entry)
Packit ae235b
    return entry->d_name;
Packit ae235b
  else
Packit ae235b
    return NULL;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dir_rewind:
Packit ae235b
 * @dir: a #GDir* created by g_dir_open()
Packit ae235b
 *
Packit ae235b
 * Resets the given directory. The next call to g_dir_read_name()
Packit ae235b
 * will return the first entry again.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_dir_rewind (GDir *dir)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (dir != NULL);
Packit ae235b
  
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  _wrewinddir (dir->wdirp);
Packit ae235b
#else
Packit ae235b
  rewinddir (dir->dirp);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dir_close:
Packit ae235b
 * @dir: a #GDir* created by g_dir_open()
Packit ae235b
 *
Packit ae235b
 * Closes the directory and deallocates all related resources.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_dir_close (GDir *dir)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (dir != NULL);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  _wclosedir (dir->wdirp);
Packit ae235b
#else
Packit ae235b
  closedir (dir->dirp);
Packit ae235b
#endif
Packit ae235b
  g_free (dir);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/* Binary compatibility versions. Not for newly compiled code. */
Packit ae235b
Packit ae235b
_GLIB_EXTERN GDir        *g_dir_open_utf8      (const gchar  *path,
Packit ae235b
                                                guint         flags,
Packit ae235b
                                                GError      **error);
Packit ae235b
_GLIB_EXTERN const gchar *g_dir_read_name_utf8 (GDir         *dir);
Packit ae235b
Packit ae235b
GDir *
Packit ae235b
g_dir_open_utf8 (const gchar  *path,
Packit ae235b
                 guint         flags,
Packit ae235b
                 GError      **error)
Packit ae235b
{
Packit ae235b
  return g_dir_open (path, flags, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
const gchar *
Packit ae235b
g_dir_read_name_utf8 (GDir *dir)
Packit ae235b
{
Packit ae235b
  return g_dir_read_name (dir);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif