5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2008 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program 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
16 GNU General Public License for more details.
20 #include "silcruntime.h"
22 /************************** Types and definitions ***************************/
24 /* Directory entry context */
25 struct SilcDirEntryStruct {
26 struct dirent *entry; /* Entry */
27 SilcFileStatStruct status; /* Status */
30 /* The directory context */
31 struct SilcDirStruct {
32 DIR *dir; /* Directory */
33 char *name; /* Directory name */
34 struct SilcDirEntryStruct entry; /* Current entry */
37 /****************************** SILC Dir API ********************************/
41 SilcDir silc_dir_open(const char *name)
45 if (!name || !strlen(name)) {
46 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
50 SILC_LOG_DEBUG(("Open directory '%s'", name));
52 dir = silc_calloc(1, sizeof(*dir));
56 dir->name = silc_strdup(name);
62 if (dir->name[strlen(dir->name) - 1] == '/')
63 dir->name[strlen(dir->name) - 1] = '\0';
65 dir->dir = opendir(name);
67 silc_set_errno_posix(errno);
78 void silc_dir_close(SilcDir dir)
83 SILC_LOG_DEBUG(("Close directory '%s'", dir->name));
90 /* Read next entry in the directory */
92 SilcDirEntry silc_dir_read(SilcDir dir, SilcFileStat status)
95 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
99 SILC_LOG_DEBUG(("Read directory '%s'", dir->name));
101 dir->entry.entry = readdir(dir->dir);
102 if (!dir->entry.entry) {
104 silc_set_errno_posix(errno);
110 silc_asprintf(&name, "%s/%s", dir->name, dir->entry.entry->d_name);
112 silc_file_stat(name, TRUE, status);
116 return (SilcDirEntry)&dir->entry;
119 /* Rewind directory */
121 void silc_dir_rewind(SilcDir dir)
126 SILC_LOG_DEBUG(("Rewind directory '%s'", dir->name));
131 /* Return directory name */
133 const char *silc_dir_name(SilcDir dir)
136 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
143 /* Return entry name */
145 const char *silc_dir_entry_name(SilcDirEntry entry)
148 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
152 return (const char *)entry->entry->d_name;