+/*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
#include "portable.h"
#include <stdio.h>
-#include <stdlib.h>
#include <limits.h>
+#include <ac/stdlib.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/ctype.h>
#include <ac/unistd.h>
#include <ac/wait.h>
+#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
+#endif
+#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
+#endif
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
-#include "ldapconfig.h"
+#ifdef HAVE_IO_H
+#include <io.h>
+#endif
+
+#include "ldap_defaults.h"
#include "../slap.h"
#include "../back-ldbm/back-ldbm.h"
-#define EDITOR "/usr/ucb/vi"
-
-static struct dbcache *openchoice(char c, int mode, int verbose, char **fname);
+static DBCache *openchoice(char c, int mode, int verbose, char **fname);
static void print_entry(FILE *fp, char c, Datum *key, char *klabel, Datum *data, char *dlabel);
-static void free_and_close(struct dbcache *dbc, Datum key, Datum data);
+static void free_and_close(DBCache *dbc, Datum key, Datum data);
static void edit_entry(char c, Datum *data);
static void get_keydata(FILE *fp, char c, Datum *key, Datum *data);
-static struct dbcache *dbc;
+static DBCache *dbc;
static LDBM dbp;
static Backend *be = NULL;
char buf[256];
Datum savekey, key, data, last;
char *fname;
- ID id;
- IDList *idl;
+ ID id, cursor;
+ ID_BLOCK *idl;
Backend *tbe;
int i;
char *tailorfile;
fprintf( stderr,
"usage: %s [-d level] [-f slapdconfigfile]\n",
argv[0] );
- exit( -1 );
- break;
+ exit( EXIT_FAILURE );
}
}
* initialize stuff and figure out which backend we're dealing with
*/
- init();
- read_config( tailorfile, &be, NULL );
+ slap_init(SLAP_TOOL_MODE, "ldbmtest");
+ read_config( tailorfile );
+ slap_startup(-1);
while ( 1 ) {
printf( "dbtest: " );
get_keydata( stdin, buf[1], &key, NULL );
if ( (idl = idl_fetch( be, dbc, key )) != NULL ) {
data.dptr = (char *) idl;
- data.dsize = (idl->b_nmax + 1) * sizeof(ID);
+ data.dsize = (ID_BLOCK_NMAX(idl) + 1) * sizeof(ID);
print_entry( stdout, buf[1], &key, "key: ",
&data, "data:\n" );
}
free_and_close( dbc, key, data );
break;
+#ifndef HAVE_WINSOCK
case 'e': /* edit an entry */
if ( (dbc = openchoice( buf[1], LDBM_WRITER, 1, NULL ))
== NULL ) {
free_and_close( dbc, key, data );
break;
+#endif
case 'a': /* add an entry */
if ( (dbc = openchoice( buf[1], LDBM_WRITER, 1, NULL ))
get_keydata( stdin, buf[1], &key, &data );
- idl = (IDList *) data.dptr;
- for ( id = idl_firstid( idl ); id != NOID;
- id = idl_nextid( idl, id ) ) {
+ idl = (ID_BLOCK *) data.dptr;
+ for ( id = idl_firstid( idl, &cursor ); id != NOID;
+ id = idl_nextid( idl, &cursor ) ) {
if ( idl_insert_key( be, dbc, key, id )
!= 0 ) {
fprintf( stderr,
printf( "suffix: " );
fflush( stdout );
if ( fgets( buf, sizeof(buf), stdin ) == NULL ) {
- exit( 0 );
+ exit( EXIT_SUCCESS );
} else {
buf[strlen( buf ) - 1] = '\0';
}
}
}
- return( 0 );
+ slap_shutdown(-1);
+ slap_destroy();
+
+ return( EXIT_SUCCESS );
}
static void
-free_and_close( struct dbcache *dbc, Datum key, Datum data )
+free_and_close( DBCache *dbc, Datum key, Datum data )
{
ldbm_cache_really_close( be, dbc );
if ( key.dptr != NULL )
get_idlist( FILE *fp, Datum *data )
{
char buf[20];
- int i, j, fd, tty;
- IDList *p;
- int psize, pmax;
- int nmax, nids;
+ int i, fd, tty;
+ ID_BLOCK *p;
+ unsigned int psize, pmax;
+ unsigned int nmax, nids;
fd = fileno( fp );
tty = isatty( fd );
if ( psize + sizeof(ID) > pmax ) {
pmax += BUFSIZ;
- p = (IDList *) myrealloc( (char *) p, pmax );
+ p = (ID_BLOCK *) myrealloc( (char *) p, pmax );
}
if ( strncmp( buf, "nids=0", 6 ) == 0 ) {
continue;
}
- p->b_ids[i++] = atol( buf );
+ ID_BLOCK_ID(p,i++) = atol( buf );
psize += sizeof(ID);
}
if ( nmax == 0 ) {
printf( "%d IDs entered. Max number of ids? [%d] ", i,
i );
if ( fgets( buf, sizeof(buf), fp ) != NULL &&
- isdigit( buf[0] ) ) {
+ isdigit( (unsigned char) buf[0] ) ) {
nmax = atol( buf );
}
} else {
}
}
if ( i > 0 ) {
- p->b_nmax = nmax;
+ ID_BLOCK_NMAX(p) = nmax;
if ( nids != 0 ) {
- p->b_nids = 0;
- p->b_ids[i] = NOID;
+ ID_BLOCK_NIDS(p) = 0;
+ ID_BLOCK_ID(p,i) = NOID;
} else {
- p->b_nids = i;
+ ID_BLOCK_NIDS(p) = i;
}
- qsort( (void *) p->b_ids, i, sizeof(ID), dnid_cmp );
+ qsort( (void *) &ID_BLOCK_ID(p, 0), i, sizeof(ID), dnid_cmp );
}
data->dptr = (char *) p;
- data->dsize = (nmax + 2) * sizeof(ID);
+ data->dsize = (nmax + ID_BLOCK_IDS_OFFSET) * sizeof(ID);
}
static void
{
char buf[BUFSIZ];
char *p;
- int pmax, psize, len;
+ unsigned int pmax, psize, len;
int fd;
fd = fileno( fp );
data->dsize = psize + 1;
}
+#ifndef HAVE_WINSOCK
static void
edit_entry( char c, Datum *data )
{
char *editor;
if ( (editor = getenv( "EDITOR" )) == NULL ) {
- editor = EDITOR;
+ editor = LDAP_EDITOR;
}
execl( editor, editor, tmpname, NULL );
perror( "execl" );
- exit( 1 );
+ exit( EXIT_FAILURE );
}
fclose( fp );
#ifdef HAVE_WAITPID
- if ( waitpid( (pid_t) -1, NULL, WAIT_FLAGS ) < 0 ) {
+ if ( waitpid( (pid_t) -1, NULL, WAIT_FLAGS ) < 0 )
#else
- if ( wait4( (pid_t) -1, &status, WAIT_FLAGS, 0 ) < 0 ) {
+ if ( wait3( (pid_t) -1, &status, WAIT_FLAGS, 0 ) < 0 )
#endif
+ {
perror( "wait" );
return;
}
fclose( fp );
unlink( tmpname );
}
+#endif
-static struct dbcache *
+static DBCache *
openfile( char *name, int namesiz, int mode, int verbose, char c )
{
- struct dbcache *dbc;
+ DBCache *dbc;
if ( name == NULL || *name == '\0' ) {
if ( c == 'f' ) {
printf( " file: " );
if ( fgets( name, namesiz, stdin ) == NULL )
- exit( 0 );
+ exit( EXIT_SUCCESS );
name[strlen( name ) - 1] = '\0';
} else {
printf( " attr: " );
if ( fgets( name, namesiz, stdin ) == NULL )
- exit( 0 );
+ exit( EXIT_SUCCESS );
name[strlen( name ) - 1] = '\0';
}
}
return( dbc );
}
-static struct dbcache *
+static DBCache *
openchoice( char c, int mode, int verbose, char **fname )
{
static char name[MAXPATHLEN];
)
{
ID id;
- IDList *idl;
- int i;
+ ID_BLOCK *idl;
+ unsigned int i;
char msg[2];
if ( data != NULL && data->dptr == NULL ) {
case 'e': /* id2entry - key is dnid, data is entry */
if ( key != NULL ) {
SAFEMEMCPY( (char *) &id, key->dptr, sizeof(ID) );
- fprintf( fp, "%s %lu\n", klabel, id );
+ fprintf( fp, "%s %ld\n", klabel, id );
}
if ( data != NULL ) {
if ( dlabel ) {
fprintf( fp, "%s%s (len %d)\n", klabel, key->dptr,
key->dsize );
if ( data != NULL ) {
- idl = (IDList *) data->dptr;
+ idl = (ID_BLOCK *) data->dptr;
if ( dlabel )
fprintf( fp, "%s\tnmax=%ld\n\tncur=%ld\n", dlabel,
- idl->b_nmax, idl->b_nids );
+ ID_BLOCK_NMAX(idl), ID_BLOCK_NIDS(idl) );
- if ( INDIRECT_BLOCK( idl ) ) {
- for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
- fprintf( fp, "\t%ld\n", idl->b_ids[i] );
+ if ( ID_BLOCK_INDIRECT( idl ) ) {
+ for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
+ fprintf( fp, "\t%ld\n", ID_BLOCK_ID(idl, i) );
}
- } else if ( ALLIDS( idl ) ) {
+ } else if ( ID_BLOCK_ALLIDS( idl ) ) {
fprintf( fp, "\tALLIDS (1..%ld)\n",
- idl->b_nids - 1 );
+ ID_BLOCK_NIDS(idl) - 1 );
} else {
- for ( i = 0; i < idl->b_nids; i++ ) {
- fprintf( fp, "\t%ld\n", idl->b_ids[i] );
+ for ( i = 0; i < ID_BLOCK_NIDS(idl); i++ ) {
+ fprintf( fp, "\t%ld\n", ID_BLOCK_ID(idl,i) );
}
}
}
if ( tty )
printf( " dn: " );
if ( fgets( kbuf, sizeof(kbuf), fp ) == NULL ) {
- exit( 0 );
+ exit( EXIT_SUCCESS );
}
kbuf[strlen( kbuf ) - 1] = '\0';
key->dptr = strdup( kbuf );
if ( tty )
printf( " dnid: " );
if ( fgets( dbuf, sizeof(dbuf), fp ) == NULL ) {
- exit( 0 );
+ exit( EXIT_SUCCESS );
}
n = atol( dbuf );
data->dptr = (char *) malloc( sizeof(n) );
if ( tty )
printf( " dnid: " );
if ( fgets( kbuf, sizeof(kbuf), fp ) == NULL ) {
- exit( 0 );
+ exit( EXIT_SUCCESS );
}
n = atol( kbuf );
key->dptr = (char *) malloc( sizeof(n) );
if ( tty )
printf( " key: " );
if ( fgets( kbuf, sizeof(kbuf), fp ) == NULL ) {
- exit( 0 );
+ exit( EXIT_SUCCESS );
}
kbuf[strlen( kbuf ) - 1] = '\0';
key->dptr = strdup( kbuf );