2001-08-07 23:21:45 +00:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
2001-08-07 23:21:45 +00:00
Samba database functions
Copyright ( C ) Andrew Tridgell 1999 - 2000
Copyright ( C ) Paul ` Rusty ' Russell 2000
Copyright ( C ) Jeremy Allison 2000
Copyright ( C ) Andrew Esh 2001
This program 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 2 of the License , or
( at your option ) any later version .
This program 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 this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
2001-08-04 18:17:30 +00:00
# include <errno.h>
1999-12-21 03:04:37 +00:00
# include <stdlib.h>
# include <stdio.h>
# include <fcntl.h>
# include <unistd.h>
# include <string.h>
# include <fcntl.h>
2001-12-04 12:44:10 +00:00
# include <time.h>
1999-12-21 03:04:37 +00:00
# include <sys/mman.h>
# include <sys/stat.h>
# include <sys/time.h>
2000-04-24 14:36:44 +00:00
# include <ctype.h>
2002-09-25 15:19:00 +00:00
# include <signal.h>
1999-12-21 03:04:37 +00:00
# include "tdb.h"
2005-05-06 01:28:02 +00:00
# include "pstring.h"
static int do_command ( void ) ;
char * cmdname , * arg1 , * arg2 ;
size_t arg1len , arg2len ;
int do_connections ;
int bIterate = 0 ;
char * line ;
TDB_DATA iterate_kbuf ;
char cmdline [ 1024 ] ;
enum commands {
CMD_CREATE_TDB ,
CMD_OPEN_TDB ,
CMD_ERASE ,
CMD_DUMP ,
CMD_CDUMP ,
CMD_INSERT ,
CMD_MOVE ,
CMD_STORE ,
CMD_SHOW ,
CMD_KEYS ,
CMD_HEXKEYS ,
CMD_DELETE ,
CMD_LIST_HASH_FREE ,
CMD_LIST_FREE ,
CMD_INFO ,
CMD_FIRST ,
CMD_NEXT ,
CMD_SYSTEM ,
CMD_QUIT ,
CMD_HELP
} ;
typedef struct {
const char * name ;
enum commands cmd ;
} COMMAND_TABLE ;
COMMAND_TABLE cmd_table [ ] = {
{ " create " , CMD_CREATE_TDB } ,
{ " open " , CMD_OPEN_TDB } ,
{ " erase " , CMD_ERASE } ,
{ " dump " , CMD_DUMP } ,
{ " cdump " , CMD_CDUMP } ,
{ " insert " , CMD_INSERT } ,
{ " move " , CMD_MOVE } ,
{ " store " , CMD_STORE } ,
{ " show " , CMD_SHOW } ,
{ " keys " , CMD_KEYS } ,
{ " hexkeys " , CMD_HEXKEYS } ,
{ " delete " , CMD_DELETE } ,
{ " list " , CMD_LIST_HASH_FREE } ,
{ " free " , CMD_LIST_FREE } ,
{ " info " , CMD_INFO } ,
{ " first " , CMD_FIRST } ,
{ " 1 " , CMD_FIRST } ,
{ " next " , CMD_NEXT } ,
{ " n " , CMD_NEXT } ,
{ " quit " , CMD_QUIT } ,
{ " q " , CMD_QUIT } ,
{ " ! " , CMD_SYSTEM } ,
{ NULL , CMD_HELP }
} ;
1999-12-21 03:04:37 +00:00
/* a tdb tool for manipulating a tdb database */
2005-05-06 01:28:02 +00:00
/* these are taken from smb.h - make sure they are in sync */
2001-01-29 21:34:08 +00:00
typedef struct connections_key {
pid_t pid ;
int cnum ;
fstring name ;
} connections_key ;
typedef struct connections_data {
int magic ;
pid_t pid ;
int cnum ;
uid_t uid ;
gid_t gid ;
char name [ 24 ] ;
char addr [ 24 ] ;
2005-05-06 01:28:02 +00:00
char machine [ FSTRING_LEN ] ;
2001-01-29 21:34:08 +00:00
time_t start ;
2005-05-06 01:28:02 +00:00
unsigned bcast_msg_flags ;
2001-01-29 21:34:08 +00:00
} connections_data ;
1999-12-21 03:04:37 +00:00
static TDB_CONTEXT * tdb ;
2005-05-06 01:28:02 +00:00
static int print_crec ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state ) ;
static int print_arec ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state ) ;
2001-12-11 08:24:36 +00:00
static int print_rec ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state ) ;
2005-05-06 01:28:02 +00:00
static int print_key ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state ) ;
static int print_hexkey ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state ) ;
2000-04-24 14:36:44 +00:00
2005-10-18 03:24:00 +00:00
static void print_asc ( const char * buf , int len )
2000-04-24 14:36:44 +00:00
{
int i ;
2001-12-03 00:23:14 +00:00
/* We're probably printing ASCII strings so don't try to display
the trailing NULL character . */
if ( buf [ len - 1 ] = = 0 )
len - - ;
2000-04-24 14:36:44 +00:00
for ( i = 0 ; i < len ; i + + )
printf ( " %c " , isprint ( buf [ i ] ) ? buf [ i ] : ' . ' ) ;
}
2005-10-18 03:24:00 +00:00
static void print_data ( const char * buf , int len )
2000-04-24 14:36:44 +00:00
{
int i = 0 ;
if ( len < = 0 ) return ;
printf ( " [%03X] " , i ) ;
for ( i = 0 ; i < len ; ) {
printf ( " %02X " , ( int ) buf [ i ] ) ;
i + + ;
if ( i % 8 = = 0 ) printf ( " " ) ;
if ( i % 16 = = 0 ) {
print_asc ( & buf [ i - 16 ] , 8 ) ; printf ( " " ) ;
print_asc ( & buf [ i - 8 ] , 8 ) ; printf ( " \n " ) ;
if ( i < len ) printf ( " [%03X] " , i ) ;
}
}
if ( i % 16 ) {
int n ;
n = 16 - ( i % 16 ) ;
printf ( " " ) ;
if ( n > 8 ) printf ( " " ) ;
while ( n - - ) printf ( " " ) ;
n = i % 16 ;
if ( n > 8 ) n = 8 ;
print_asc ( & buf [ i - ( i % 16 ) ] , n ) ; printf ( " " ) ;
n = ( i % 16 ) - n ;
if ( n > 0 ) print_asc ( & buf [ i - n ] , n ) ;
printf ( " \n " ) ;
}
}
1999-12-21 03:04:37 +00:00
static void help ( void )
{
2003-04-14 01:47:16 +00:00
printf ( " \n "
" tdbtool: \n "
" create dbname : create a database \n "
" open dbname : open an existing database \n "
" erase : erase the database \n "
" dump : dump the database as strings \n "
2005-05-06 01:28:02 +00:00
" cdump : dump the database as connection records \n "
" keys : dump the database keys as strings \n "
" hexkeys : dump the database keys as hex values \n "
" info : print summary info about the database \n "
2003-04-14 01:47:16 +00:00
" insert key data : insert a record \n "
2003-07-16 16:51:51 +00:00
" move key file : move a record to a destination tdb \n "
2003-04-14 01:47:16 +00:00
" store key data : store a record (replace) \n "
" show key : show a record by key \n "
" delete key : delete a record by key \n "
" list : print the database hash table and freelist \n "
" free : print the database freelist \n "
2005-05-06 01:28:02 +00:00
" ! command : execute system command \n "
2003-04-14 01:47:16 +00:00
" 1 | first : print the first record \n "
" n | next : print the next record \n "
" q | quit : terminate \n "
" \\ n : repeat 'next' command \n "
" \n " ) ;
1999-12-21 03:04:37 +00:00
}
2005-03-17 18:25:15 +00:00
static void terror ( const char * why )
1999-12-21 03:04:37 +00:00
{
printf ( " %s \n " , why ) ;
}
2005-05-06 01:28:02 +00:00
static void create_tdb ( char * tdbname )
2001-08-07 23:21:45 +00:00
{
1999-12-21 03:04:37 +00:00
if ( tdb ) tdb_close ( tdb ) ;
2005-05-06 01:28:02 +00:00
tdb = tdb_open ( tdbname , 0 , TDB_CLEAR_IF_FIRST ,
2001-12-04 11:41:12 +00:00
O_RDWR | O_CREAT | O_TRUNC , 0600 ) ;
2001-08-04 18:17:30 +00:00
if ( ! tdb ) {
2005-05-06 01:28:02 +00:00
printf ( " Could not create %s: %s \n " , tdbname , strerror ( errno ) ) ;
2001-08-04 18:17:30 +00:00
}
1999-12-21 03:04:37 +00:00
}
2005-05-06 01:28:02 +00:00
static void open_tdb ( char * tdbname )
1999-12-21 03:04:37 +00:00
{
if ( tdb ) tdb_close ( tdb ) ;
2005-05-06 01:28:02 +00:00
tdb = tdb_open ( tdbname , 0 , 0 , O_RDWR , 0600 ) ;
2001-08-04 18:17:30 +00:00
if ( ! tdb ) {
2005-05-06 01:28:02 +00:00
printf ( " Could not open %s: %s \n " , tdbname , strerror ( errno ) ) ;
2001-08-04 18:17:30 +00:00
}
1999-12-21 03:04:37 +00:00
}
2005-05-06 01:28:02 +00:00
static void insert_tdb ( char * keyname , size_t keylen , char * data , size_t datalen )
1999-12-21 03:04:37 +00:00
{
TDB_DATA key , dbuf ;
2005-05-06 01:28:02 +00:00
key . dptr = keyname ;
key . dsize = keylen ;
dbuf . dptr = data ;
dbuf . dsize = datalen ;
1999-12-21 03:04:37 +00:00
if ( tdb_store ( tdb , key , dbuf , TDB_INSERT ) = = - 1 ) {
terror ( " insert failed " ) ;
}
}
2005-05-06 01:28:02 +00:00
static void store_tdb ( char * keyname , size_t keylen , char * data , size_t datalen )
1999-12-21 03:04:37 +00:00
{
TDB_DATA key , dbuf ;
2005-05-06 01:28:02 +00:00
key . dptr = keyname ;
key . dsize = keylen ;
dbuf . dptr = data ;
dbuf . dsize = datalen ;
2001-08-07 23:21:45 +00:00
printf ( " Storing key: \n " ) ;
print_rec ( tdb , key , dbuf , NULL ) ;
1999-12-21 03:04:37 +00:00
if ( tdb_store ( tdb , key , dbuf , TDB_REPLACE ) = = - 1 ) {
terror ( " store failed " ) ;
}
}
2005-05-06 01:28:02 +00:00
static void show_tdb ( char * keyname , size_t keylen )
1999-12-21 03:04:37 +00:00
{
TDB_DATA key , dbuf ;
2005-05-06 01:28:02 +00:00
key . dptr = keyname ;
key . dsize = keylen ;
1999-12-21 03:04:37 +00:00
dbuf = tdb_fetch ( tdb , key ) ;
2001-12-03 04:15:26 +00:00
if ( ! dbuf . dptr ) {
2005-05-06 01:28:02 +00:00
terror ( " fetch failed " ) ;
return ;
2001-12-03 04:15:26 +00:00
}
2003-07-16 16:26:40 +00:00
2000-10-05 22:19:34 +00:00
print_rec ( tdb , key , dbuf , NULL ) ;
2003-07-16 16:26:40 +00:00
free ( dbuf . dptr ) ;
return ;
1999-12-21 03:04:37 +00:00
}
2005-05-06 01:28:02 +00:00
static void delete_tdb ( char * keyname , size_t keylen )
1999-12-21 03:04:37 +00:00
{
TDB_DATA key ;
2005-05-06 01:28:02 +00:00
key . dptr = keyname ;
key . dsize = keylen ;
1999-12-21 03:04:37 +00:00
if ( tdb_delete ( tdb , key ) ! = 0 ) {
terror ( " delete failed " ) ;
}
}
2005-05-06 01:28:02 +00:00
static void move_rec ( char * keyname , size_t keylen , char * tdbname )
2003-07-16 16:51:51 +00:00
{
TDB_DATA key , dbuf ;
TDB_CONTEXT * dst_tdb ;
2005-05-06 01:28:02 +00:00
if ( ! tdbname ) {
2003-07-16 16:51:51 +00:00
terror ( " need destination tdb name " ) ;
return ;
}
2005-05-06 01:28:02 +00:00
key . dptr = keyname ;
key . dsize = keylen ;
2003-07-16 16:51:51 +00:00
dbuf = tdb_fetch ( tdb , key ) ;
if ( ! dbuf . dptr ) {
2005-05-06 01:28:02 +00:00
terror ( " fetch failed " ) ;
return ;
2003-07-16 16:51:51 +00:00
}
print_rec ( tdb , key , dbuf , NULL ) ;
2005-05-06 01:28:02 +00:00
dst_tdb = tdb_open ( tdbname , 0 , 0 , O_RDWR , 0600 ) ;
2003-07-16 16:51:51 +00:00
if ( ! dst_tdb ) {
terror ( " unable to open destination tdb " ) ;
return ;
}
if ( tdb_store ( dst_tdb , key , dbuf , TDB_REPLACE ) = = - 1 ) {
terror ( " failed to move record " ) ;
}
else
printf ( " record moved \n " ) ;
tdb_close ( dst_tdb ) ;
return ;
}
2001-01-29 21:34:08 +00:00
static int print_conn_key ( TDB_DATA key )
{
2005-09-30 17:13:37 +00:00
printf ( " \n key %d bytes \n " , ( int ) key . dsize ) ;
2001-01-29 21:34:08 +00:00
printf ( " pid =%5d " , ( ( connections_key * ) key . dptr ) - > pid ) ;
printf ( " cnum =%10d " , ( ( connections_key * ) key . dptr ) - > cnum ) ;
printf ( " name =[%s] \n " , ( ( connections_key * ) key . dptr ) - > name ) ;
return 0 ;
}
static int print_conn_data ( TDB_DATA dbuf )
{
2005-09-30 17:13:37 +00:00
printf ( " \n data %d bytes \n " , ( int ) dbuf . dsize ) ;
2001-01-29 21:34:08 +00:00
printf ( " pid =%5d " , ( ( connections_data * ) dbuf . dptr ) - > pid ) ;
printf ( " cnum =%10d " , ( ( connections_data * ) dbuf . dptr ) - > cnum ) ;
printf ( " name =[%s] \n " , ( ( connections_data * ) dbuf . dptr ) - > name ) ;
printf ( " uid =%5d " , ( ( connections_data * ) dbuf . dptr ) - > uid ) ;
printf ( " addr =[%s] \n " , ( ( connections_data * ) dbuf . dptr ) - > addr ) ;
printf ( " gid =%5d " , ( ( connections_data * ) dbuf . dptr ) - > gid ) ;
printf ( " machine=[%s] \n " , ( ( connections_data * ) dbuf . dptr ) - > machine ) ;
printf ( " start = %s \n " , ctime ( & ( ( connections_data * ) dbuf . dptr ) - > start ) ) ;
2005-05-06 01:28:02 +00:00
printf ( " magic = 0x%x " , ( ( connections_data * ) dbuf . dptr ) - > magic ) ;
printf ( " flags = 0x%x \n " , ( ( connections_data * ) dbuf . dptr ) - > bcast_msg_flags ) ;
2001-01-29 21:34:08 +00:00
return 0 ;
}
2001-12-11 08:24:36 +00:00
static int print_rec ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state )
1999-12-21 03:04:37 +00:00
{
2005-05-06 01:28:02 +00:00
if ( do_connections & & ( dbuf . dsize = = sizeof ( connections_data ) ) )
print_crec ( the_tdb , key , dbuf , state ) ;
else
print_arec ( the_tdb , key , dbuf , state ) ;
return 0 ;
}
static int print_crec ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state )
{
2001-01-29 21:34:08 +00:00
print_conn_key ( key ) ;
print_conn_data ( dbuf ) ;
return 0 ;
2005-05-06 01:28:02 +00:00
}
static int print_arec ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state )
{
2005-09-30 17:13:37 +00:00
printf ( " \n key %d bytes \n " , ( int ) key . dsize ) ;
2001-12-03 00:23:14 +00:00
print_asc ( key . dptr , key . dsize ) ;
2005-09-30 17:13:37 +00:00
printf ( " \n data %d bytes \n " , ( int ) dbuf . dsize ) ;
2000-04-24 14:36:44 +00:00
print_data ( dbuf . dptr , dbuf . dsize ) ;
1999-12-21 03:04:37 +00:00
return 0 ;
}
2001-12-11 08:24:36 +00:00
static int print_key ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state )
2001-11-28 03:58:33 +00:00
{
2005-09-30 17:13:37 +00:00
printf ( " key %d bytes: " , ( int ) key . dsize ) ;
2001-12-03 00:23:14 +00:00
print_asc ( key . dptr , key . dsize ) ;
printf ( " \n " ) ;
2001-11-28 03:58:33 +00:00
return 0 ;
}
2005-05-06 01:28:02 +00:00
static int print_hexkey ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state )
{
2005-09-30 17:13:37 +00:00
printf ( " key %d bytes \n " , ( int ) key . dsize ) ;
2005-05-06 01:28:02 +00:00
print_data ( key . dptr , key . dsize ) ;
printf ( " \n " ) ;
return 0 ;
}
1999-12-21 03:04:37 +00:00
static int total_bytes ;
2001-12-11 08:24:36 +00:00
static int traverse_fn ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf , void * state )
1999-12-21 03:04:37 +00:00
{
total_bytes + = dbuf . dsize ;
return 0 ;
}
static void info_tdb ( void )
{
int count ;
total_bytes = 0 ;
2005-05-06 01:28:02 +00:00
if ( ( count = tdb_traverse ( tdb , traverse_fn , NULL ) ) = = - 1 )
2001-02-13 16:28:48 +00:00
printf ( " Error = %s \n " , tdb_errorstr ( tdb ) ) ;
else
printf ( " %d records totalling %d bytes \n " , count , total_bytes ) ;
1999-12-21 03:04:37 +00:00
}
2005-03-17 18:25:15 +00:00
static char * tdb_getline ( const char * prompt )
2000-01-07 03:02:13 +00:00
{
2005-05-27 14:38:00 +00:00
static char thisline [ 1024 ] ;
2000-01-07 03:02:13 +00:00
char * p ;
fputs ( prompt , stdout ) ;
2005-05-27 14:38:00 +00:00
thisline [ 0 ] = 0 ;
p = fgets ( thisline , sizeof ( thisline ) - 1 , stdin ) ;
2000-01-07 03:02:13 +00:00
if ( p ) p = strchr ( p , ' \n ' ) ;
if ( p ) * p = 0 ;
2005-05-27 14:38:00 +00:00
return p ? thisline : NULL ;
2000-01-07 03:02:13 +00:00
}
2001-12-11 08:24:36 +00:00
static int do_delete_fn ( TDB_CONTEXT * the_tdb , TDB_DATA key , TDB_DATA dbuf ,
2000-02-28 00:37:13 +00:00
void * state )
{
2001-12-11 08:24:36 +00:00
return tdb_delete ( the_tdb , key ) ;
2000-02-28 00:37:13 +00:00
}
2001-12-11 08:24:36 +00:00
static void first_record ( TDB_CONTEXT * the_tdb , TDB_DATA * pkey )
2000-12-07 17:46:11 +00:00
{
TDB_DATA dbuf ;
2001-12-11 08:24:36 +00:00
* pkey = tdb_firstkey ( the_tdb ) ;
2000-12-07 17:46:11 +00:00
2001-12-11 08:24:36 +00:00
dbuf = tdb_fetch ( the_tdb , * pkey ) ;
2000-12-07 17:46:11 +00:00
if ( ! dbuf . dptr ) terror ( " fetch failed " ) ;
2002-07-15 10:35:28 +00:00
else {
print_rec ( the_tdb , * pkey , dbuf , NULL ) ;
}
2000-12-07 17:46:11 +00:00
}
2001-12-11 08:24:36 +00:00
static void next_record ( TDB_CONTEXT * the_tdb , TDB_DATA * pkey )
2000-12-07 17:46:11 +00:00
{
TDB_DATA dbuf ;
2001-12-11 08:24:36 +00:00
* pkey = tdb_nextkey ( the_tdb , * pkey ) ;
2000-12-07 17:46:11 +00:00
2001-12-11 08:24:36 +00:00
dbuf = tdb_fetch ( the_tdb , * pkey ) ;
2001-01-29 21:34:08 +00:00
if ( ! dbuf . dptr )
terror ( " fetch failed " ) ;
else
2001-12-11 08:24:36 +00:00
print_rec ( the_tdb , * pkey , dbuf , NULL ) ;
2000-12-07 17:46:11 +00:00
}
2005-05-06 01:28:02 +00:00
static int do_command ( void )
{
COMMAND_TABLE * ctp = cmd_table ;
enum commands mycmd = CMD_HELP ;
int cmd_len ;
do_connections = 0 ;
if ( cmdname & & strlen ( cmdname ) = = 0 ) {
mycmd = CMD_NEXT ;
} else {
while ( ctp - > name ) {
cmd_len = strlen ( ctp - > name ) ;
if ( strncmp ( ctp - > name , cmdname , cmd_len ) = = 0 ) {
mycmd = ctp - > cmd ;
break ;
}
ctp + + ;
}
}
switch ( mycmd ) {
case CMD_CREATE_TDB :
bIterate = 0 ;
create_tdb ( arg1 ) ;
return 0 ;
case CMD_OPEN_TDB :
bIterate = 0 ;
open_tdb ( arg1 ) ;
return 0 ;
case CMD_SYSTEM :
/* Shell command */
system ( arg1 ) ;
return 0 ;
case CMD_QUIT :
return 1 ;
default :
/* all the rest require a open database */
if ( ! tdb ) {
bIterate = 0 ;
terror ( " database not open " ) ;
help ( ) ;
return 0 ;
}
switch ( mycmd ) {
case CMD_ERASE :
bIterate = 0 ;
tdb_traverse ( tdb , do_delete_fn , NULL ) ;
return 0 ;
case CMD_DUMP :
bIterate = 0 ;
tdb_traverse ( tdb , print_rec , NULL ) ;
return 0 ;
case CMD_CDUMP :
do_connections = 1 ;
bIterate = 0 ;
tdb_traverse ( tdb , print_rec , NULL ) ;
return 0 ;
case CMD_INSERT :
bIterate = 0 ;
insert_tdb ( arg1 , arg1len , arg2 , arg2len ) ;
return 0 ;
case CMD_MOVE :
bIterate = 0 ;
move_rec ( arg1 , arg1len , arg2 ) ;
return 0 ;
case CMD_STORE :
bIterate = 0 ;
store_tdb ( arg1 , arg1len , arg2 , arg2len ) ;
return 0 ;
case CMD_SHOW :
bIterate = 0 ;
show_tdb ( arg1 , arg1len ) ;
return 0 ;
case CMD_KEYS :
tdb_traverse ( tdb , print_key , NULL ) ;
return 0 ;
case CMD_HEXKEYS :
tdb_traverse ( tdb , print_hexkey , NULL ) ;
return 0 ;
case CMD_DELETE :
bIterate = 0 ;
delete_tdb ( arg1 , arg1len ) ;
return 0 ;
case CMD_LIST_HASH_FREE :
tdb_dump_all ( tdb ) ;
return 0 ;
case CMD_LIST_FREE :
tdb_printfreelist ( tdb ) ;
return 0 ;
case CMD_INFO :
info_tdb ( ) ;
return 0 ;
case CMD_FIRST :
bIterate = 1 ;
first_record ( tdb , & iterate_kbuf ) ;
return 0 ;
case CMD_NEXT :
if ( bIterate )
next_record ( tdb , & iterate_kbuf ) ;
return 0 ;
case CMD_HELP :
help ( ) ;
return 0 ;
2005-05-27 14:38:00 +00:00
case CMD_CREATE_TDB :
case CMD_OPEN_TDB :
case CMD_SYSTEM :
case CMD_QUIT :
/*
* unhandled commands . cases included here to avoid compiler
* warnings .
*/
return 0 ;
2005-05-06 01:28:02 +00:00
}
}
return 0 ;
}
static char * convert_string ( char * instring , size_t * sizep )
{
size_t length = 0 ;
char * outp , * inp ;
char temp [ 3 ] ;
outp = inp = instring ;
while ( * inp ) {
if ( * inp = = ' \\ ' ) {
inp + + ;
if ( * inp & & strchr ( " 0123456789abcdefABCDEF " , ( int ) * inp ) ) {
temp [ 0 ] = * inp + + ;
temp [ 1 ] = ' \0 ' ;
if ( * inp & & strchr ( " 0123456789abcdefABCDEF " , ( int ) * inp ) ) {
temp [ 1 ] = * inp + + ;
temp [ 2 ] = ' \0 ' ;
}
* outp + + = ( char ) strtol ( ( const char * ) temp , NULL , 16 ) ;
} else {
* outp + + = * inp + + ;
}
} else {
* outp + + = * inp + + ;
}
length + + ;
}
* sizep = length ;
return instring ;
}
1999-12-21 03:04:37 +00:00
int main ( int argc , char * argv [ ] )
{
2005-05-27 14:38:00 +00:00
cmdname = ( char * ) " " ;
2005-05-06 01:28:02 +00:00
arg1 = NULL ;
arg1len = 0 ;
arg2 = NULL ;
arg2len = 0 ;
2000-10-05 22:19:34 +00:00
if ( argv [ 1 ] ) {
2005-05-27 14:38:00 +00:00
cmdname = ( char * ) " open " ;
2005-05-06 01:28:02 +00:00
arg1 = argv [ 1 ] ;
do_command ( ) ;
2005-05-27 14:38:00 +00:00
cmdname = ( char * ) " " ;
2005-05-06 01:28:02 +00:00
arg1 = NULL ;
2000-10-05 22:19:34 +00:00
}
2005-05-06 01:28:02 +00:00
switch ( argc ) {
case 1 :
case 2 :
/* Interactive mode */
while ( ( cmdname = tdb_getline ( " tdb> " ) ) ) {
arg2 = arg1 = NULL ;
2005-05-27 14:38:00 +00:00
if ( ( arg1 = strchr ( ( const char * ) cmdname , ' ' ) ) ! = NULL ) {
2005-05-06 01:28:02 +00:00
arg1 + + ;
arg2 = arg1 ;
while ( * arg2 ) {
if ( * arg2 = = ' ' ) {
* arg2 + + = ' \0 ' ;
break ;
}
if ( ( * arg2 + + = = ' \\ ' ) & & ( * arg2 = = ' ' ) ) {
arg2 + + ;
}
}
}
if ( arg1 ) arg1 = convert_string ( arg1 , & arg1len ) ;
if ( arg2 ) arg2 = convert_string ( arg2 , & arg2len ) ;
if ( do_command ( ) ) break ;
}
break ;
case 5 :
arg2 = convert_string ( argv [ 4 ] , & arg2len ) ;
case 4 :
arg1 = convert_string ( argv [ 3 ] , & arg1len ) ;
case 3 :
cmdname = argv [ 2 ] ;
default :
do_command ( ) ;
break ;
2000-03-13 01:35:09 +00:00
}
if ( tdb ) tdb_close ( tdb ) ;
return 0 ;
1999-12-21 03:04:37 +00:00
}