1 /* gdbmcount.c - get number of items in a gdbm file. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 1993, 1994, 2007, 2011, 2013 Free Software Foundation, Inc.
6 GDBM is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GDBM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
19 /* Include system configuration before all else. */
24 compoff (const void *a, const void *b)
26 if (*(off_t*)a < *(off_t*)b)
28 if (*(off_t*)a > *(off_t*)b)
34 gdbm_count (GDBM_FILE dbf, gdbm_count_t *pcount)
37 int nbuckets = GDBM_DIR_COUNT (dbf);
39 gdbm_count_t count = 0;
42 sdir = malloc (dbf->header->dir_size);
45 gdbm_errno = GDBM_MALLOC_ERROR;
49 memcpy (sdir, dbf->dir, dbf->header->dir_size);
50 qsort (sdir, nbuckets, sizeof (off_t), compoff);
52 for (i = last = 0; i < nbuckets; i++)
54 if (i == 0 || sdir[i] != sdir[last])
56 if (_gdbm_read_bucket_at (dbf, sdir[i], &bucket, sizeof bucket))
58 count += bucket.count;