/* This file is part of GDBM, the GNU data base manager.
Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013,
2016 Free Software Foundation, Inc.
GDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GDBM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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"
#include "gdbm.h"
#include "gram.h"
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <pwd.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_TERMIOS_H
# include <sys/termios.h>
#endif
#include <stdarg.h>
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
char *file_name = NULL; /* Database file name */
GDBM_FILE gdbm_file = NULL; /* Database to operate upon */
datum key_data; /* Current key */
datum return_data; /* Current data */
int open_mode; /* Default open mode */
#define SIZE_T_MAX ((size_t)-1)
unsigned input_line;
static int
opendb (char *dbname)
{
int cache_size = 0;
int block_size = 0;
int flags = 0;
int filemode;
GDBM_FILE db;
switch (variable_get ("cachesize", VART_INT, (void**) &cache_size))
{
case VAR_OK:
case VAR_ERR_NOTSET:
break;
default:
abort ();
}
switch (variable_get ("blocksize", VART_INT, (void**) &block_size))
{
case VAR_OK:
case VAR_ERR_NOTSET:
break;
default:
abort ();
}
if (!variable_is_true ("lock"))
flags |= GDBM_NOLOCK;
if (!variable_is_true ("mmap"))
flags |= GDBM_NOMMAP;
if (variable_is_true ("sync"))
flags |= GDBM_SYNC;
if (open_mode == GDBM_NEWDB)
{
if (interactive && variable_is_true ("confirm") &&
access (dbname, F_OK) == 0)
{
if (!getyn (_("database %s already exists; overwrite"), dbname))
return 1;
}
}
if (variable_get ("filemode", VART_INT, (void**) &filemode))
abort ();
db = gdbm_open (dbname, block_size, open_mode | flags, filemode, NULL);
if (db == NULL)
{
terror (_("cannot open database %s: %s"), dbname,
gdbm_strerror (gdbm_errno));
return 1;
}
if (cache_size &&
gdbm_setopt (db, GDBM_CACHESIZE, &cache_size, sizeof (int)) == -1)
terror (_("gdbm_setopt failed: %s"), gdbm_strerror (gdbm_errno));
if (gdbm_file)
gdbm_close (gdbm_file);
gdbm_file = db;
return 0;
}
static int
checkdb ()
{
if (!gdbm_file)
{
if (!file_name)
{
file_name = estrdup (GDBMTOOL_DEFFILE);
terror (_("warning: using default database file %s"),
file_name);
}
return opendb (file_name);
}
return 0;
}
size_t
bucket_print_lines (hash_bucket *bucket)
{
return 6 + gdbm_file->header->bucket_elems + 3 + bucket->av_count;
}
/* Debug procedure to print the contents of the current hash bucket. */
void
print_bucket (FILE *fp, hash_bucket *bucket, const char *mesg)
{
int index;
fprintf (fp,
_("******* %s **********\n\nbits = %d\ncount= %d\nHash Table:\n"),
mesg, bucket->bucket_bits, bucket->count);
fprintf (fp,
_(" # hash value key size data size data adr home\n"));
for (index = 0; index < gdbm_file->header->bucket_elems; index++)
fprintf (fp, " %4d %12x %11d %11d %11lu %5d\n", index,
bucket->h_table[index].hash_value,
bucket->h_table[index].key_size,
bucket->h_table[index].data_size,
(unsigned long) bucket->h_table[index].data_pointer,
bucket->h_table[index].hash_value %
gdbm_file->header->bucket_elems);
fprintf (fp, _("\nAvail count = %1d\n"), bucket->av_count);
fprintf (fp, _("Avail adr size\n"));
for (index = 0; index < bucket->av_count; index++)
fprintf (fp, "%9lu%9d\n",
(unsigned long) bucket->bucket_avail[index].av_adr,
bucket->bucket_avail[index].av_size);
}
size_t
_gdbm_avail_list_size (GDBM_FILE dbf, size_t min_size)
{
int temp;
int size;
avail_block *av_stk;
size_t lines;
int rc;
lines = 4 + dbf->header->avail.count;
if (lines > min_size)
return lines;
/* Initialize the variables for a pass throught the avail stack. */
temp = dbf->header->avail.next_block;
size = (((dbf->header->avail.size * sizeof (avail_elem)) >> 1)
+ sizeof (avail_block));
av_stk = emalloc (size);
/* Traverse the stack. */
while (temp)
{
if (__ls
|