diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-05-21 20:43:02 +0000 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-05-21 20:43:02 +0000 |
commit | 05fa76dfce5311161abbc64c12acbaf56f298e97 (patch) | |
tree | 00c6651278ded58dc53fdf1d50e601c2d469090a /src/var.c | |
parent | 7a87cd8c3ca9528bf53cdb06dfb4168450251d79 (diff) | |
download | gdbm-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.c | 53 |
1 files changed, 35 insertions, 18 deletions
@@ -14,16 +14,17 @@ You should have received a copy of the GNU General Public License along with GDBM. If not, see <http://www.gnu.org/licenses/>. */ #include "gdbmtool.h" -#define VARF_DFL 0x00 -#define VARF_SET 0x01 -#define VARF_INIT 0x02 -#define VARF_PROT 0x04 +#define VARF_DFL 0x00 /* Default flags -- everything disabled */ +#define VARF_SET 0x01 /* Variable is set */ +#define VARF_INIT 0x02 /* Variable is initialized */ +#define VARF_PROT 0x04 /* Variable is protected, i.e. cannot be unset */ +#define VARF_OCTAL 0x08 /* For integer variables -- use octal base */ #define VAR_IS_SET(v) ((v)->flags & (VARF_SET|VARF_INIT)) union value { char *string; @@ -35,13 +36,12 @@ struct variable { char *name; int type; int flags; union value v; int (*hook) (struct variable *, union value *); - void *hook_data; }; static int open_hook (struct variable *, union value *); static struct variable vartab[] = { /* Top-level prompt */ @@ -56,12 +56,13 @@ static struct variable vartab[] = { { "cachesize", VART_INT, VARF_DFL }, { "blocksize", VART_INT, VARF_DFL }, { "open", VART_STRING, VARF_DFL, { NULL }, open_hook }, { "lock", VART_BOOL, VARF_INIT, { num: 1 } }, { "mmap", VART_BOOL, VARF_INIT, { num: 1 } }, { "sync", VART_BOOL, VARF_INIT, { num: 0 } }, + { "filemode", VART_INT, VARF_INIT|VARF_OCTAL|VARF_PROT, { num: 0644 } }, { "pager", VART_STRING, VARF_DFL }, { "quiet", VART_BOOL, VARF_DFL }, { NULL } }; static int @@ -102,39 +103,39 @@ varfind (const char *name) if (strcmp (vp->name, name) == 0) return vp; return NULL; } -typedef int (*setvar_t) (union value *, void *); +typedef int (*setvar_t) (union value *, void *, int); static int -s2s (union value *vp, void *val) +s2s (union value *vp, void *val, int flags) { vp->string = estrdup (val); return VAR_OK; } static int -b2s (union value *vp, void *val) +b2s (union value *vp, void *val, int flags) { vp->string = estrdup (*(int*)val ? "true" : "false"); return VAR_OK; } static int -i2s (union value *vp, void *val) +i2s (union value *vp, void *val, int flags) { char buf[128]; snprintf (buf, sizeof buf, "%d", *(int*)val); vp->string = estrdup (buf); return VAR_OK; } static int -s2b (union value *vp, void *val) +s2b (union value *vp, void *val, int flags) { static char *trueval[] = { "on", "true", "yes", NULL }; static char *falseval[] = { "off", "false", "no", NULL }; int i; unsigned long n; char *p; @@ -158,47 +159,47 @@ s2b (union value *vp, void *val) return VAR_ERR_BADTYPE; vp->bool = !!n; return VAR_OK; } static int -s2i (union value *vp, void *val) +s2i (union value *vp, void *val, int flags) { char *p; - int n = strtoul (val, &p, 0); + int n = strtoul (val, &p, (flags & VARF_OCTAL) ? 8 : 10); if (*p) return VAR_ERR_BADTYPE; vp->num = n; return VAR_OK; } static int -b2b (union value *vp, void *val) +b2b (union value *vp, void *val, int flags) { vp->bool = !!*(int*)val; return VAR_OK; } static int -b2i (union value *vp, void *val) +b2i (union value *vp, void *val, int flags) { vp->num = *(int*)val; return VAR_OK; } static int -i2i (union value *vp, void *val) +i2i (union value *vp, void *val, int flags) { vp->num = *(int*)val; return VAR_OK; } static int -i2b (union value *vp, void *val) +i2b (union value *vp, void *val, int flags) { vp->bool = *(int*)val; return VAR_OK; } static setvar_t setvar[3][3] = { @@ -218,13 +219,13 @@ variable_set (const char *name, int type, void *val) if (!vp) return VAR_ERR_NOTDEF; if (val) { memset (&v, 0, sizeof (v)); - rc = setvar[vp->type][type] (&v, val); + rc = setvar[vp->type][type] (&v, val, vp->flags); if (rc) return rc; valp = &v; } else { @@ -301,30 +302,46 @@ variable_get (const char *name, int type, void **val) break; } return VAR_OK; } +static int +varcmp (const void *a, const void *b) +{ + return strcmp (((struct variable const *)a)->name, + ((struct variable const *)b)->name); +} + void variable_print_all (FILE *fp) { struct variable *vp; char *s; + static int sorted; + + if (!sorted) + { + qsort (vartab, sizeof (vartab) / sizeof (vartab[0]) - 1, + sizeof (vartab[0]), varcmp); + sorted = 1; + } for (vp = vartab; vp->name; vp++) { if (!VAR_IS_SET (vp)) { fprintf (fp, "# %s is unset", vp->name); } else { switch (vp->type) { case VART_INT: - fprintf (fp, "%s=%d", vp->name, vp->v.num); + fprintf (fp, (vp->flags & VARF_OCTAL) ? "%s=%03o" : "%s=%d", + vp->name, vp->v.num); break; case VART_BOOL: fprintf (fp, "%s%s", vp->v.bool ? "" : "no", vp->name); break; |