GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dirent.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <grass/dbmi.h>
5 /* NOTE: these should come from <unistd.h> or from <sys/file.h> */
6 #define R_OK 4
7 #define W_OK 2
8 #define X_OK 1
9 
10 #include <sys/types.h>
11 #ifdef USE_DIRECT
12 # include <sys/dir.h>
13 typedef struct direct dir_entry;
14 #else
15 # include <dirent.h>
16 typedef struct dirent dir_entry;
17 #endif
18 
19 extern DIR *opendir();
20 extern dir_entry *readdir();
21 
22 static int get_perm();
23 static void sort_dirent();
24 
25 
32 /* read directory and build an array of dbDirent's */
33 /* append one entry with name = NULL to mark end of array */
34 dbDirent *db_dirent(const char *dirname, int *n)
35 {
36  DIR *dp;
37  dir_entry *entry;
38  dbDirent *dirent;
39  int i, count;
40  char *path;
41  int len, max;
42 
44 
45  *n = 0;
46  dp = opendir(dirname);
47  if (dp == NULL) {
48  db_syserror(dirname);
49  return (dbDirent *) NULL;
50  }
51 
52 
53  /* count the number of entries and get the strlen of the longest name */
54  count = 0;
55  max = 0;
56  while ((entry = readdir(dp))) {
57  count++;
58  len = strlen(entry->d_name);
59  if (len > max)
60  max = len;
61  }
62  rewinddir(dp);
63 
64  path = db_malloc(strlen(dirname) + max + 2); /* extra 2 for / and NULL */
65  if (path == NULL) {
66  closedir(dp);
67  return (dbDirent *) NULL;
68  }
69  dirent = db_alloc_dirent_array(count);
70  if (dirent == NULL) {
71  closedir(dp);
72  return (dbDirent *) NULL;
73  }
74  *n = count;
75  for (i = 0; i < count; i++) {
76  entry = readdir(dp);
77  if (entry == NULL) /* this shouldn't happen */
78  break;
79 
80  if (DB_OK != db_set_string(&dirent[i].name, entry->d_name))
81  break;
82  sprintf(path, "%s/%s", dirname, entry->d_name);
83  dirent[i].perm = get_perm(path);
84  dirent[i].isdir = (db_isdir(path) == DB_OK);
85  }
86  closedir(dp);
87  db_free(path);
88 
89  sort_dirent(dirent, *n);
90 
91  return dirent;
92 }
93 
100 void db_free_dirent_array(dbDirent * dirent, int count)
101 {
102  int i;
103 
104  if (dirent) {
105  for (i = 0; i < count; i++)
106  db_free_string(&dirent[i].name);
107  db_free(dirent);
108  }
109 }
110 
111 static int get_perm(char *path)
112 {
113  int perm;
114 
115  perm = 0;
116 
117  if (access(path, R_OK) == 0)
118  perm |= DB_PERM_R;
119  if (access(path, W_OK) == 0)
120  perm |= DB_PERM_W;
121  if (access(path, X_OK) == 0)
122  perm |= DB_PERM_X;
123 
124  return perm;
125 }
126 
127 static int cmp_dirent(const void *aa, const void *bb)
128 {
129  const dbDirent *a = aa;
130  const dbDirent *b = bb;
131 
132  return strcmp(db_get_string((dbString *) & a->name),
133  db_get_string((dbString *) & b->name));
134 }
135 
136 static void sort_dirent(dbDirent * a, int n)
137 {
138  qsort(a, n, sizeof(dbDirent), cmp_dirent);
139 }
140 
148 {
149  int i;
150  dbDirent *dirent;
151 
152  dirent = (dbDirent *) db_calloc(count, sizeof(dbDirent));
153  if (dirent == NULL)
154  return dirent;
155 
156  for (i = 0; i < count; i++)
157  db_init_string(&dirent[i].name);
158 
159  return dirent;
160 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
DIR * opendir()
float b
Definition: named_colr.c:8
string name
Definition: render.py:1314
void * db_calloc(int n, int m)
int count
dbDirent * db_dirent(const char *dirname, int *n)
Definition: dirent.c:34
#define R_OK
Definition: dirent.c:6
#define W_OK
Definition: dirent.c:7
#define max(x, y)
Definition: draw2.c:69
dir_entry * readdir()
void db_free_dirent_array(dbDirent *dirent, int count)
Definition: dirent.c:100
void db_syserror(const char *s)
void db_clear_error(void)
void * db_malloc(int n)
#define X_OK
Definition: dirent.c:8
return NULL
Definition: dbfopen.c:1394
dbDirent * db_alloc_dirent_array(int count)
Definition: dirent.c:147
char * db_get_string(dbString *x)
Definition: string.c:131
int db_set_string(dbString *x, const char *s)
Definition: string.c:33
int db_isdir(const char *path)
Definition: isdir.c:12
void db_free_string(dbString *x)
Definition: string.c:142
struct dirent dir_entry
Definition: dirent.c:16
int n
Definition: dataquad.c:291
void db_init_string(dbString *x)
Definition: string.c:11
void db_free(void *s)