2001-11-05 02:15:38 +01:00
|
|
|
/* 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
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2001-11-05 02:47:31 +01:00
|
|
|
#include <errno.h>
|
2001-11-05 02:15:38 +01:00
|
|
|
#include <string.h> /* strerror, strcmp */
|
2001-11-05 02:47:31 +01:00
|
|
|
|
2001-11-05 02:15:38 +01:00
|
|
|
#ifdef HAVE_DIRENT_H
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "glib.h"
|
|
|
|
#include "gdir.h"
|
|
|
|
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
|
|
|
struct _GDir
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dir_open:
|
|
|
|
* @path: the path to the directory you are interested in
|
2001-11-05 02:35:30 +01:00
|
|
|
* @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
|
|
|
|
* g_dir_open_fails.
|
2001-11-05 02:15:38 +01:00
|
|
|
*
|
2001-11-05 02:35:30 +01:00
|
|
|
* Opens a directory for reading. The names of the files
|
|
|
|
* in the directory can then be retrieved using
|
|
|
|
* g_dir_get_name().
|
|
|
|
*
|
|
|
|
* 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.
|
2001-11-05 02:15:38 +01:00
|
|
|
**/
|
|
|
|
GDir *
|
|
|
|
g_dir_open (const gchar *path,
|
|
|
|
guint flags,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GDir *dir = g_new (GDir, 1);
|
|
|
|
|
|
|
|
dir->dir = opendir (path);
|
|
|
|
|
|
|
|
if (dir->dir)
|
|
|
|
return dir;
|
|
|
|
|
|
|
|
/* error case */
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
2001-11-05 02:35:30 +01:00
|
|
|
_("Error opening directory '%s': %s"),
|
|
|
|
path, strerror (errno));
|
2001-11-05 02:15:38 +01:00
|
|
|
|
|
|
|
g_free (dir);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dir_read_name:
|
|
|
|
* @dir: a #GDir* created by g_dir_open()
|
|
|
|
*
|
2001-11-05 02:35:30 +01:00
|
|
|
* Retrieves the name of the next entry in the directory.
|
|
|
|
* The '.' and '..' entries are omitted.
|
2001-11-05 02:15:38 +01:00
|
|
|
*
|
2001-11-05 02:35:30 +01:00
|
|
|
* Return value: The entries name or %NULL if there are no
|
|
|
|
* more entries. The return value is owned by GLib and
|
|
|
|
* must not be modified or freed.
|
2001-11-05 02:15:38 +01:00
|
|
|
**/
|
|
|
|
G_CONST_RETURN gchar*
|
2001-11-05 02:35:30 +01:00
|
|
|
g_dir_read_name (GDir *dir)
|
2001-11-05 02:15:38 +01:00
|
|
|
{
|
|
|
|
struct dirent *entry;
|
|
|
|
|
|
|
|
g_return_val_if_fail (dir != NULL, NULL);
|
|
|
|
|
|
|
|
entry = readdir (dir->dir);
|
|
|
|
while (entry
|
2001-11-05 02:35:30 +01:00
|
|
|
&& (0 == strcmp (entry->d_name, ".") ||
|
|
|
|
0 == strcmp (entry->d_name, "..")))
|
2001-11-05 02:15:38 +01:00
|
|
|
entry = readdir (dir->dir);
|
|
|
|
|
|
|
|
return entry->d_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
rewinddir (dir->dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_dir_close:
|
|
|
|
* @dir: a #GDir* created by g_dir_open()
|
|
|
|
*
|
|
|
|
* Closes the directory and deallocates all related resources.
|
|
|
|
**/
|
2001-11-05 02:35:30 +01:00
|
|
|
void
|
2001-11-05 02:15:38 +01:00
|
|
|
g_dir_close (GDir *dir)
|
|
|
|
{
|
2001-11-05 02:47:31 +01:00
|
|
|
g_return_if_fail (dir != NULL);
|
2001-11-05 02:15:38 +01:00
|
|
|
|
2001-11-05 02:35:30 +01:00
|
|
|
closedir (dir->dir);
|
2001-11-05 02:15:38 +01:00
|
|
|
g_free (dir);
|
|
|
|
}
|