/* This file is part of GDBM, the GNU data base manager.
Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 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 quiet_option = 0; /* Omit the usual welcome banner at startup */
#define SIZE_T_MAX ((size_t)-1)
unsigned input_line;
void
terror (int code, const char *fmt, ...)
{
va_list ap;
if (!interactive)
fprintf (stderr, "%s: ", progname);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fputc ('\n', stderr);
if (code)
exit (code);
}
char *
mkfilename (const char *dir, const char *file, const char *suf)
{
char *tmp;
size_t dirlen = strlen (dir);
size_t suflen = suf ? strlen (suf) : 0;
size_t fillen = strlen (file);
size_t len;
while (dirlen > 0 && dir[dirlen-1] == '/')
dirlen--;
len = dirlen + (dir[0] ? 1 : 0) + fillen + suflen;
tmp = emalloc (len + 1);
memcpy (tmp, dir, dirlen);
if (dir[0])
tmp[dirlen++] = '/';
memcpy (tmp + dirlen, file, fillen);
if (suf)
memcpy (tmp + dirlen + fillen, suf, suflen);
tmp[len] = 0;
return tmp;
}
char *
tildexpand (char *s)
{
if (s[0] == '~')
{
char *p = s + 1;
size_t len = strcspn (p, "/");
struct passwd *pw;
if (len == 0)
pw = getpwuid (getuid ());
else
{
char *user = emalloc (len + 1);
memcpy (user, p, len);
user[len] = 0;
pw = getpwnam (user);
free (user);
}
if (pw)
return mkfilename (pw->pw_dir, p + len + 1, NULL);
}
return estrdup (s);
}
static int
opendb (char *dbname)
{
int cache_size;
int block_size;
int flags = 0;
GDBM_FILE db;
if (variable_get ("cachesize", VART_INT, (void**) &cache_size))
abort ();
if (variable_get ("blocksize", VART_INT, (void**) &block_size))
abort ();
if (!variable_is_set ("lock"))
flags |= GDBM_NOLOCK;
if (!variable_is_set ("mmap"))
flags |= GDBM_NOMMAP;
if (variable_is_set ("sync"))
flags |= GDBM_SYNC;
if (variable_is_set ("readonly"))
flags = GDBM_READER;
else if (variable_is_set ("newdb"))
flags |= GDBM_NEWDB;
else
flags |= GDBM_WRCREAT;
db = gdbm_open (dbname, block_size, flags, 00664, NULL);
if (db == NULL)
{
syntax_error (_("cannot open database %s: %s"), dbname,
gdbm_strerror (gdbm_errno));
return 1;
}
if (gdbm_setopt (db, GDBM_CACHESIZE, &cache_size, sizeof (int)) ==
-1)
syntax_error (_("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);
syntax_error (_("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_
|