aboutsummaryrefslogtreecommitdiff
path: root/src/var.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2013-05-21 20:43:02 +0000
committerSergey Poznyakoff <gray@gnu.org.ua>2013-05-21 20:43:02 +0000
commit05fa76dfce5311161abbc64c12acbaf56f298e97 (patch)
tree00c6651278ded58dc53fdf1d50e601c2d469090a /src/var.c
parent7a87cd8c3ca9528bf53cdb06dfb4168450251d79 (diff)
downloadgdbm-05fa76dfce5311161abbc64c12acbaf56f298e97.tar.gz
gdbm-05fa76dfce5311161abbc64c12acbaf56f298e97.tar.bz2
New function gdbm_count.
* configure.ac: Check for unsigned long long, define substitution variable GDBM_COUNT_T. * src/gdbmcount.c: New file. * src/Makefile.am (libgdbm_la_SOURCES): Add gdbmcount.c. * src/bucket.c (_gdbm_read_bucket_at): New function. * src/gdbm.h.in (gdbm_count_t): New typedef. (gdbm_count): New proto. * src/gdbmdefs.h (GDBM_DIR_COUNT): New define. * src/proto.h (_gdbm_read_bucket_at): New proto. * src/var.c: New variable "filemode". * src/gdbmtool.c: Use gdbm_count. Various bugfixes. * NEWS: Update. * doc/gdbm.texinfo: Update.
Diffstat (limited to 'src/var.c')
-rw-r--r--src/var.c53
1 files changed, 35 insertions, 18 deletions
diff --git a/src/var.c b/src/var.c
index 54122eb..fb6ecf0 100644
--- a/src/var.c
+++ b/src/var.c
@@ -17,10 +17,11 @@
17 17
18#include "gdbmtool.h" 18#include "gdbmtool.h"
19 19
20#define VARF_DFL 0x00 20#define VARF_DFL 0x00 /* Default flags -- everything disabled */
21#define VARF_SET 0x01 21#define VARF_SET 0x01 /* Variable is set */
22#define VARF_INIT 0x02 22#define VARF_INIT 0x02 /* Variable is initialized */
23#define VARF_PROT 0x04 23#define VARF_PROT 0x04 /* Variable is protected, i.e. cannot be unset */
24#define VARF_OCTAL 0x08 /* For integer variables -- use octal base */
24 25
25#define VAR_IS_SET(v) ((v)->flags & (VARF_SET|VARF_INIT)) 26#define VAR_IS_SET(v) ((v)->flags & (VARF_SET|VARF_INIT))
26 27
@@ -38,7 +39,6 @@ struct variable
38 int flags; 39 int flags;
39 union value v; 40 union value v;
40 int (*hook) (struct variable *, union value *); 41 int (*hook) (struct variable *, union value *);
41 void *hook_data;
42}; 42};
43 43
44static int open_hook (struct variable *, union value *); 44static int open_hook (struct variable *, union value *);
@@ -59,6 +59,7 @@ static struct variable vartab[] = {
59 { "lock", VART_BOOL, VARF_INIT, { num: 1 } }, 59 { "lock", VART_BOOL, VARF_INIT, { num: 1 } },
60 { "mmap", VART_BOOL, VARF_INIT, { num: 1 } }, 60 { "mmap", VART_BOOL, VARF_INIT, { num: 1 } },
61 { "sync", VART_BOOL, VARF_INIT, { num: 0 } }, 61 { "sync", VART_BOOL, VARF_INIT, { num: 0 } },
62 { "filemode", VART_INT, VARF_INIT|VARF_OCTAL|VARF_PROT, { num: 0644 } },
62 { "pager", VART_STRING, VARF_DFL }, 63 { "pager", VART_STRING, VARF_DFL },
63 { "quiet", VART_BOOL, VARF_DFL }, 64 { "quiet", VART_BOOL, VARF_DFL },
64 { NULL } 65 { NULL }
@@ -105,24 +106,24 @@ varfind (const char *name)
105 return NULL; 106 return NULL;
106} 107}
107 108
108typedef int (*setvar_t) (union value *, void *); 109typedef int (*setvar_t) (union value *, void *, int);
109 110
110static int 111static int
111s2s (union value *vp, void *val) 112s2s (union value *vp, void *val, int flags)
112{ 113{
113 vp->string = estrdup (val); 114 vp->string = estrdup (val);
114 return VAR_OK; 115 return VAR_OK;
115} 116}
116 117
117static int 118static int
118b2s (union value *vp, void *val) 119b2s (union value *vp, void *val, int flags)
119{ 120{
120 vp->string = estrdup (*(int*)val ? "true" : "false"); 121 vp->string = estrdup (*(int*)val ? "true" : "false");
121 return VAR_OK; 122 return VAR_OK;
122} 123}
123 124
124static int 125static int
125i2s (union value *vp, void *val) 126i2s (union value *vp, void *val, int flags)
126{ 127{
127 char buf[128]; 128 char buf[128];
128 snprintf (buf, sizeof buf, "%d", *(int*)val); 129 snprintf (buf, sizeof buf, "%d", *(int*)val);
@@ -131,7 +132,7 @@ i2s (union value *vp, void *val)
131} 132}
132 133
133static int 134static int
134s2b (union value *vp, void *val) 135s2b (union value *vp, void *val, int flags)
135{ 136{
136 static char *trueval[] = { "on", "true", "yes", NULL }; 137 static char *trueval[] = { "on", "true", "yes", NULL };
137 static char *falseval[] = { "off", "false", "no", NULL }; 138 static char *falseval[] = { "off", "false", "no", NULL };
@@ -161,10 +162,10 @@ s2b (union value *vp, void *val)
161} 162}
162 163
163static int 164static int
164s2i (union value *vp, void *val) 165s2i (union value *vp, void *val, int flags)
165{ 166{
166 char *p; 167 char *p;
167 int n = strtoul (val, &p, 0); 168 int n = strtoul (val, &p, (flags & VARF_OCTAL) ? 8 : 10);
168 169
169 if (*p) 170 if (*p)
170 return VAR_ERR_BADTYPE; 171 return VAR_ERR_BADTYPE;
@@ -174,28 +175,28 @@ s2i (union value *vp, void *val)
174} 175}
175 176
176static int 177static int
177b2b (union value *vp, void *val) 178b2b (union value *vp, void *val, int flags)
178{ 179{
179 vp->bool = !!*(int*)val; 180 vp->bool = !!*(int*)val;
180 return VAR_OK; 181 return VAR_OK;
181} 182}
182 183
183static int 184static int
184b2i (union value *vp, void *val) 185b2i (union value *vp, void *val, int flags)
185{ 186{
186 vp->num = *(int*)val; 187 vp->num = *(int*)val;
187 return VAR_OK; 188 return VAR_OK;
188} 189}
189 190
190static int 191static int
191i2i (union value *vp, void *val) 192i2i (union value *vp, void *val, int flags)
192{ 193{
193 vp->num = *(int*)val; 194 vp->num = *(int*)val;
194 return VAR_OK; 195 return VAR_OK;
195} 196}
196 197
197static int 198static int
198i2b (union value *vp, void *val) 199i2b (union value *vp, void *val, int flags)
199{ 200{
200 vp->bool = *(int*)val; 201 vp->bool = *(int*)val;
201 return VAR_OK; 202 return VAR_OK;
@@ -221,7 +222,7 @@ variable_set (const char *name, int type, void *val)
221 if (val) 222 if (val)
222 { 223 {
223 memset (&v, 0, sizeof (v)); 224 memset (&v, 0, sizeof (v));
224 rc = setvar[vp->type][type] (&v, val); 225 rc = setvar[vp->type][type] (&v, val, vp->flags);
225 if (rc) 226 if (rc)
226 return rc; 227 return rc;
227 valp = &v; 228 valp = &v;
@@ -304,11 +305,26 @@ variable_get (const char *name, int type, void **val)
304 return VAR_OK; 305 return VAR_OK;
305} 306}
306 307
308static int
309varcmp (const void *a, const void *b)
310{
311 return strcmp (((struct variable const *)a)->name,
312 ((struct variable const *)b)->name);
313}
314
307void 315void
308variable_print_all (FILE *fp) 316variable_print_all (FILE *fp)
309{ 317{
310 struct variable *vp; 318 struct variable *vp;
311 char *s; 319 char *s;
320 static int sorted;
321
322 if (!sorted)
323 {
324 qsort (vartab, sizeof (vartab) / sizeof (vartab[0]) - 1,
325 sizeof (vartab[0]), varcmp);
326 sorted = 1;
327 }
312 328
313 for (vp = vartab; vp->name; vp++) 329 for (vp = vartab; vp->name; vp++)
314 { 330 {
@@ -321,7 +337,8 @@ variable_print_all (FILE *fp)
321 switch (vp->type) 337 switch (vp->type)
322 { 338 {
323 case VART_INT: 339 case VART_INT:
324 fprintf (fp, "%s=%d", vp->name, vp->v.num); 340 fprintf (fp, (vp->flags & VARF_OCTAL) ? "%s=%03o" : "%s=%d",
341 vp->name, vp->v.num);
325 break; 342 break;
326 343
327 case VART_BOOL: 344 case VART_BOOL:

Return to:

Send suggestions and report system problems to the System administrator.