GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
lib/db/dbmi_base/dirent.c
Go to the documentation of this file.
1/*!
2 \file lib/db/dbmi_base/dirent.c
3
4 \brief DBMI Library (base) - directory entities management
5
6 (C) 1999-2010 by the GRASS Development Team
7
8 This program is free software under the GNU General Public License
9 (>=v2). Read the file COPYING that comes with GRASS for details.
10
11 \author Joel Jones (CERL/UIUC)
12 \author Upgraded to GRASS 5.7 by Radim Blazek
13 */
14
15#include <string.h>
16#include <stdlib.h>
17#include <unistd.h>
18#include <sys/types.h>
19#include <dirent.h>
20
21#include <grass/dbmi.h>
22
23/* NOTE: these should come from <unistd.h> or from <sys/file.h> */
24#ifndef R_OK
25#if !defined(HAVE_UNISTD_H)
26#define R_OK 4
27#endif
28#ifndef W_OK
29#define W_OK 2
30#endif
31#ifndef X_OK
32#define X_OK 1
33#endif
34#endif
35
36static int cmp_dirent(const void *, const void *);
37static int get_perm(char *);
38static void sort_dirent(dbDirent *, int);
39
40/*!
41 \brief Read directory and build an array of dbDirent's
42
43 Append one entry with name = NULL to mark end of array
44
45 \param dirname directory name
46 \param[out] n number of entities
47
48 \return pointer to dbDirent
49 \return NULL on error
50 */
51dbDirent *db_dirent(const char *dirname, int *n)
52{
53 DIR *dp;
54 struct dirent *entry;
56 int i, count;
57 char *path;
58 int len, max;
59
61
62 *n = 0;
64 if (dp == NULL) {
66 return (dbDirent *)NULL;
67 }
68
69 /* count the number of entries and get the strlen of the longest name */
70 count = 0;
71 max = 0;
72 while ((entry = readdir(dp))) {
73 count++;
74 len = strlen(entry->d_name);
75 if (len > max)
76 max = len;
77 }
79
80 size_t path_len = strlen(dirname) + max + 2; // extra 2 for / and NULL
82 if (path == NULL) {
83 closedir(dp);
84 return (dbDirent *)NULL;
85 }
87 if (db_dirent == NULL) {
88 closedir(dp);
89 return (dbDirent *)NULL;
90 }
91 *n = count;
92 for (i = 0; i < count; i++) {
93 entry = readdir(dp);
94 if (entry == NULL) /* this shouldn't happen */
95 break;
96
97 if (DB_OK != db_set_string(&db_dirent[i].name, entry->d_name))
98 break;
99 snprintf(path, path_len, "%s/%s", dirname, entry->d_name);
100 db_dirent[i].perm = get_perm(path);
101 db_dirent[i].isdir = (db_isdir(path) == DB_OK);
102 }
103 closedir(dp);
104 db_free(path);
105
106 sort_dirent(db_dirent, *n);
107
108 return db_dirent;
109}
110
111/*!
112 \brief Free dbDirent
113
114 \param db_dirent pointer to dbDirent
115 \param count number of entities in the array
116 */
118{
119 int i;
120
121 if (db_dirent) {
122 for (i = 0; i < count; i++)
125 }
126}
127
128static int get_perm(char *path)
129{
130 int perm;
131
132 perm = 0;
133
134 if (access(path, R_OK) == 0)
135 perm |= DB_PERM_R;
136 if (access(path, W_OK) == 0)
137 perm |= DB_PERM_W;
138 if (access(path, X_OK) == 0)
139 perm |= DB_PERM_X;
140
141 return perm;
142}
143
144static int cmp_dirent(const void *aa, const void *bb)
145{
146 const dbDirent *a = aa;
147 const dbDirent *b = bb;
148
149 return strcmp(db_get_string((dbString *)&a->name),
151}
152
153static void sort_dirent(dbDirent *a, int n)
154{
155 qsort(a, n, sizeof(dbDirent), cmp_dirent);
156}
157
158/*!
159 \brief Allocate dirent array
160
161 \param count number of entities in the array
162
163 \return pointer to dbDirent array
164 \return NULL on failure
165 */
167{
168 int i;
170
172 if (db_dirent == NULL)
173 return db_dirent;
174
175 for (i = 0; i < count; i++)
177
178 return db_dirent;
179}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
Main header of GRASS DataBase Management Interface.
#define DB_PERM_R
Definition dbmi.h:66
#define DB_PERM_X
Definition dbmi.h:68
#define DB_PERM_W
Definition dbmi.h:67
#define DB_OK
Definition dbmi.h:71
int db_isdir(const char *)
Test if path is a directory.
Definition isdir.c:29
void db_clear_error(void)
Clear error status.
void db_free_string(dbString *)
Free allocated space for dbString.
Definition string.c:150
void * db_calloc(int, int)
Allocate memory.
char * db_get_string(const dbString *)
Get string.
Definition string.c:140
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition string.c:41
void db_free(void *)
Free allocated memory.
void db_init_string(dbString *)
Initialize dbString.
Definition string.c:25
void db_syserror(const char *)
Report system error.
void * db_malloc(int)
Allocate memory.
struct DIR DIR
Definition dirent.h:18
#define max(x, y)
Definition draw2.c:30
int count
dbDirent * db_dirent(const char *dirname, int *n)
Read directory and build an array of dbDirent's.
#define X_OK
void db_free_dirent_array(dbDirent *db_dirent, int count)
Free dbDirent.
dbDirent * db_alloc_dirent_array(int count)
Allocate dirent array.
#define W_OK
DIR * opendir(const char *name)
Definition msvc/dirent.c:15
void rewinddir(DIR *dir)
Definition msvc/dirent.c:92
struct dirent * readdir(DIR *dir)
Definition msvc/dirent.c:75
int closedir(DIR *dir)
Definition msvc/dirent.c:54
const char * name
Definition named_colr.c:6
double b
Definition r_raster.c:39
dbString name
Definition dbmi.h:160
int perm
Definition dbmi.h:162
int isdir
Definition dbmi.h:161
Definition path.h:15
#define access
Definition unistd.h:7
#define R_OK
Definition unistd.h:25