-
-
Notifications
You must be signed in to change notification settings - Fork 295
/
dirent.c
179 lines (143 loc) · 3.59 KB
/
dirent.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
178
179
/*!
\file lib/db/dbmi_base/dirent.c
\brief DBMI Library (base) - directory entities management
(C) 1999-2010 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author Joel Jones (CERL/UIUC)
\author Upgraded to GRASS 5.7 by Radim Blazek
*/
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <grass/dbmi.h>
/* NOTE: these should come from <unistd.h> or from <sys/file.h> */
#define R_OK 4
#define W_OK 2
#define X_OK 1
#include <sys/types.h>
#ifdef USE_DIRECT
# include <sys/dir.h>
typedef struct direct dir_entry;
#else
# include <dirent.h>
typedef struct dirent dir_entry;
#endif
extern DIR *opendir();
extern dir_entry *readdir();
static int cmp_dirent(const void *, const void *);
static int get_perm(char *);
static void sort_dirent(dbDirent *, int);
/*!
\brief Read directory and build an array of dbDirent's
Append one entry with name = NULL to mark end of array
\param dirname directory name
\param[out] n number of entities
\return pointer to dbDirent
\return NULL on error
*/
dbDirent *db_dirent(const char *dirname, int *n)
{
DIR *dp;
dir_entry *entry;
dbDirent *dirent;
int i, count;
char *path;
int len, max;
db_clear_error();
*n = 0;
dp = opendir(dirname);
if (dp == NULL) {
db_syserror(dirname);
return (dbDirent *) NULL;
}
/* count the number of entries and get the strlen of the longest name */
count = 0;
max = 0;
while ((entry = readdir(dp))) {
count++;
len = strlen(entry->d_name);
if (len > max)
max = len;
}
rewinddir(dp);
path = db_malloc(strlen(dirname) + max + 2); /* extra 2 for / and NULL */
if (path == NULL) {
closedir(dp);
return (dbDirent *) NULL;
}
dirent = db_alloc_dirent_array(count);
if (dirent == NULL) {
closedir(dp);
return (dbDirent *) NULL;
}
*n = count;
for (i = 0; i < count; i++) {
entry = readdir(dp);
if (entry == NULL) /* this shouldn't happen */
break;
if (DB_OK != db_set_string(&dirent[i].name, entry->d_name))
break;
sprintf(path, "%s/%s", dirname, entry->d_name);
dirent[i].perm = get_perm(path);
dirent[i].isdir = (db_isdir(path) == DB_OK);
}
closedir(dp);
db_free(path);
sort_dirent(dirent, *n);
return dirent;
}
/*!
\brief Free dbDirent
\param dirent pointer to dbDirent
\param count number of entities in the array
*/
void db_free_dirent_array(dbDirent * dirent, int count)
{
int i;
if (dirent) {
for (i = 0; i < count; i++)
db_free_string(&dirent[i].name);
db_free(dirent);
}
}
static int get_perm(char *path)
{
int perm;
perm = 0;
if (access(path, R_OK) == 0)
perm |= DB_PERM_R;
if (access(path, W_OK) == 0)
perm |= DB_PERM_W;
if (access(path, X_OK) == 0)
perm |= DB_PERM_X;
return perm;
}
static int cmp_dirent(const void *aa, const void *bb)
{
const dbDirent *a = aa;
const dbDirent *b = bb;
return strcmp(db_get_string((dbString *) & a->name),
db_get_string((dbString *) & b->name));
}
static void sort_dirent(dbDirent * a, int n)
{
qsort(a, n, sizeof(dbDirent), cmp_dirent);
}
/*!
\brief Allocate dirent array
\param count number of entities in the array
\return pointer to dbDirent array
\return NULL on failure
*/
dbDirent *db_alloc_dirent_array(int count)
{
int i;
dbDirent *dirent;
dirent = (dbDirent *) db_calloc(count, sizeof(dbDirent));
if (dirent == NULL)
return dirent;
for (i = 0; i < count; i++)
db_init_string(&dirent[i].name);
return dirent;
}