GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
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 SPDX-FileCopyrightText: 1999-2010 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Joel Jones (CERL/UIUC)
10 \author Upgraded to GRASS 5.7 by Radim Blazek
11 */
12
13#include <string.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <sys/types.h>
17#include <dirent.h>
18
19#include <grass/dbmi.h>
20
21/* NOTE: these should come from <unistd.h> or from <sys/file.h> */
22#if !defined(HAVE_UNISTD_H)
23#ifndef R_OK
24#define R_OK 4
25#endif
26#ifndef W_OK
27#define W_OK 2
28#endif
29#ifndef X_OK
30#define X_OK 1
31#endif
32#endif
33
34static int cmp_dirent(const void *, const void *);
35static int get_perm(char *);
36static void sort_dirent(dbDirent *, int);
37
38/*!
39 \brief Read directory and build an array of dbDirent's
40
41 Append one entry with name = NULL to mark end of array
42
43 \param dirname directory name
44 \param[out] n number of entities
45
46 \return pointer to dbDirent
47 \return NULL on error
48 */
49dbDirent *db_dirent(const char *dirname, int *n)
50{
51 DIR *dp;
52 struct dirent *entry;
54 int i, count;
55 char *path;
56 int len, max;
57
59
60 *n = 0;
62 if (dp == NULL) {
64 return (dbDirent *)NULL;
65 }
66
67 /* count the number of entries and get the strlen of the longest name */
68 count = 0;
69 max = 0;
70 while ((entry = readdir(dp))) {
71 count++;
72 len = strlen(entry->d_name);
73 if (len > max)
74 max = len;
75 }
77
78 size_t path_len = strlen(dirname) + max + 2; // extra 2 for / and NULL
80 if (path == NULL) {
81 closedir(dp);
82 return (dbDirent *)NULL;
83 }
85 if (db_dirent == NULL) {
86 closedir(dp);
87 return (dbDirent *)NULL;
88 }
89 *n = count;
90 for (i = 0; i < count; i++) {
91 entry = readdir(dp);
92 if (entry == NULL) /* this shouldn't happen */
93 break;
94
95 if (DB_OK != db_set_string(&db_dirent[i].name, entry->d_name))
96 break;
97 snprintf(path, path_len, "%s/%s", dirname, entry->d_name);
98 db_dirent[i].perm = get_perm(path);
100 }
101 closedir(dp);
102 db_free(path);
103
104 sort_dirent(db_dirent, *n);
105
106 return db_dirent;
107}
108
109/*!
110 \brief Free dbDirent
111
112 \param db_dirent pointer to dbDirent
113 \param count number of entities in the array
114 */
116{
117 int i;
118
119 if (db_dirent) {
120 for (i = 0; i < count; i++)
123 }
124}
125
126static int get_perm(char *path)
127{
128 int perm;
129
130 perm = 0;
131
132 if (access(path, R_OK) == 0)
133 perm |= DB_PERM_R;
134 if (access(path, W_OK) == 0)
135 perm |= DB_PERM_W;
136 if (access(path, X_OK) == 0)
137 perm |= DB_PERM_X;
138
139 return perm;
140}
141
142static int cmp_dirent(const void *aa, const void *bb)
143{
144 const dbDirent *a = aa;
145 const dbDirent *b = bb;
146
147 return strcmp(db_get_string((dbString *)&a->name),
149}
150
151static void sort_dirent(dbDirent *a, int n)
152{
153 qsort(a, n, sizeof(dbDirent), cmp_dirent);
154}
155
156/*!
157 \brief Allocate dirent array
158
159 \param count number of entities in the array
160
161 \return pointer to dbDirent array
162 \return NULL on failure
163 */
165{
166 int i;
168
170 if (db_dirent == NULL)
171 return db_dirent;
172
173 for (i = 0; i < count; i++)
175
176 return db_dirent;
177}
#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:64
#define DB_PERM_X
Definition dbmi.h:66
#define DB_PERM_W
Definition dbmi.h:65
#define DB_OK
Definition dbmi.h:69
int db_isdir(const char *)
Test if path is a directory.
Definition isdir.c:27
void db_clear_error(void)
Clear error status.
void db_free_string(dbString *)
Free allocated space for dbString.
Definition string.c:148
void * db_calloc(int, int)
Allocate memory.
char * db_get_string(const dbString *)
Get string.
Definition string.c:138
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition string.c:39
void db_free(void *)
Free allocated memory.
void db_init_string(dbString *)
Initialize dbString.
Definition string.c:23
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.
void db_free_dirent_array(dbDirent *db_dirent, int count)
Free dbDirent.
dbDirent * db_alloc_dirent_array(int count)
Allocate dirent array.
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:37
dbString name
Definition dbmi.h:158
int perm
Definition dbmi.h:160
int isdir
Definition dbmi.h:159
Definition path.h:15
#define access
Definition unistd.h:7
#define R_OK
Definition unistd.h:25
#define X_OK
Definition unistd.h:23
#define W_OK
Definition unistd.h:24