[Raw Msg Headers][Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

makendbm -- simple ndbm maker utility for Zmailer



Hello,

	I don't know how Rayan has supposed ndbm indexes to be
created, but as I did not find anything obvious, and my local user
population keeps growing (3000+ entries in fullnamemap database),
I had to roll my own.

Input data syntax:
	- Blank (white space) lines, and those starting with '#' are
	  comments, and thus discarded.
	- <white-space>* <token> <whitespace>+ [data-string] \n
	  First non-white-space character starts key token, it is
	  separated from datastring by one or more white-spaces.
	  Line terminating newline is removed before line is stored.
	  Data-string may be void.


	Here is an example how I use this database format.
(I have symlinks in  /etc/ to proper $MAILSHARE/db/ -files,
 I have multiple systems sharing same databases over the NFS..)

-------- from  standard.cf ------
# optional: Fullname database: <Full_Name>    <login>
if [ -f /etc/fullnames ]; then
        relation -t ndbm -mlf /etc/fullnames fullnamemap
else
        fullnamemap () { return 1 }
fi
---------------------------------

	I am running this on almoast all database lookups
we need here :)

	/Matti Aarnio <mea@utu.fi>


#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 03/24/1993 07:46 UTC by mea@polaris
# Source directory /data2/mea/src/mea/zmailer/utils
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#    739 -rw-r----- makendbm/Makefile
#   2781 -rw-r----- makendbm/makendbm.c
#   1513 -rw-r----- makendbm/ndbmlook.c
#
# ============= makendbm/Makefile ==============
if test ! -d 'makendbm'; then
    echo 'x - creating directory makendbm'
    mkdir 'makendbm'
fi
if test -f 'makendbm/Makefile' -a X"$1" != X"-c"; then
	echo 'x - skipping makendbm/Makefile (File already exists)'
else
echo 'x - extracting makendbm/Makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'makendbm/Makefile' &&
#
#  makendbm -- make NDBM database file for later lookups
#
SHELL   = /bin/sh
CC      = gcc
COPTS   = -g -O
CFLAGS	= $(COPTS)
PROGS   = makendbm ndbmlook
SOURCE  = makendbm.c ndbmlook.c
MAILBIN = /usr/local/lib/mail/bin
X
all: $(PROGS)
X
makendbm: makendbm.o
X	$(CC) $(CFLAGS) -o makendbm makendbm.o
X
ndbmlook: ndbmlook.o
X	$(CC) $(CFLAGS) -o ndbmlook ndbmlook.o
X
clean:
X	-rm *.o $(PROGS) *~
X
install:
X	for x in $(PROGS) ; do \
X	install -c -m 0755 $$x $(MAILBIN)/$$x.x; \
X	mv $(MAILBIN)/$$x.x $(MAILBIN)/$$x; done
X
depend:
X	../../bin/mkdep $(SOURCE)
X
# DO NOT DELETE THIS LINE -- mkdep uses it.
# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
X
makendbm.o: makendbm.c
ndbmlook.o: ndbmlook.c
X
# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
SHAR_EOF
chmod 0640 makendbm/Makefile ||
echo 'restore of makendbm/Makefile failed'
Wc_c="`wc -c < 'makendbm/Makefile'`"
test 739 -eq "$Wc_c" ||
	echo 'makendbm/Makefile: original size 739, current size' "$Wc_c"
fi
# ============= makendbm/makendbm.c ==============
if test -f 'makendbm/makendbm.c' -a X"$1" != X"-c"; then
	echo 'x - skipping makendbm/makendbm.c (File already exists)'
else
echo 'x - extracting makendbm/makendbm.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'makendbm/makendbm.c' &&
/* Copyright 1993 - Matti Aarnio, Turku University, Turku, Finland
X   This will be free software, but only when it is finished.
X
X   The way the Zmailer uses DBM entries is by using strings with
X   their terminating NULL as keys, and as data..  Thus the length
X   is strlen(string)+1, not strlen(string) !
*/
X
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ndbm.h>
X
#define PROG "makendbm"
X
extern int errno;
extern char *sys_errlist[];
extern int sys_nerr;
X
extern void create_dbase();
X
void
usage(err,errno)
char *err;
int errno;
{
X  fprintf(stderr,"Usage: "PROG" database.name [infilename|-]\n");
X  fprintf(stderr,"  If no infilename is defined, database.name is assumed.\n");
X  fprintf(stderr,"  (ndbm appends  .pag, and .dir  into actual db file names..)\n");
X  fprintf(stderr," Error now: %s",err);
X  if (errno > 0 && errno < sys_nerr)
X    fprintf(stderr,", errno=%d (%s)",errno,sys_errlist[errno]);
X  fprintf(stderr,"\n");
X  exit (1);
}
X
int
main(argc,argv)
int argc;
char *argv[];
{
X  char *dbasename = NULL;
X  FILE *infile = NULL;
X  DBM *dbmfile;
X
X  if (argc < 2) usage("too few arguments",0);
X  if (argc > 3) usage("too many arguments",0);
X  dbasename = argv[1];
X
X  if (argc == 3) {
X    if (strcmp(argv[2],"-")==0)
X      infile = stdin;
X    else
X      infile = (FILE*)fopen(argv[2],"r");
X  } else
X    infile = (FILE*)fopen(argv[1],"r");
X
X  if (infile == NULL) usage("bad infile",errno);
X
X  dbmfile = dbm_open(dbasename, O_RDWR|O_CREAT|O_TRUNC, 0644);
X  if (dbmfile == NULL)
X    usage("Can't open dbase file",errno);
X
X
X  create_dbase(infile,dbmfile);
X
X  dbm_close(dbmfile);
X
X  return 0;
}
X
void
create_dbase(infile,dbmfile)
FILE *infile;
DBM *dbmfile;
{
X	char linebuf[2048];
X	char *s, *t;
X	datum dat;
X	datum key;
X
X	while (!feof(infile) && !ferror(infile) && !dbm_error(dbmfile)) {
X	  *linebuf = 0;
X	  fgets(linebuf,sizeof(linebuf)-1,infile);
X	  if (*linebuf == 0) break; /* Last! */
X	  s = (char *)strchr(linebuf,'\n');
X	  if (s) *s = 0; /* Zap \n from the end -- if it exists.. */
X	  if (*linebuf == '#') continue; /* Comment! */
X
X	  /* Scan first white-space separated token, point its start with t! */
X	  t = linebuf;
X	  while (*t == '\t' || *t == ' ') ++t;
X
X	  if (*t == 0) continue; /* Blank line! */
X
X	  s = t;
X	  while (*s && *s != '\t' && *s != ' ') ++s;
X	  if (*s) {
X	    *s = 0; /* Found end of input token, put 0 there, and look if
X		       there is some data on this line! */
X	    ++s;
X	    while (*s && (*s == '\t' || *s == ' ')) ++s;
X	    /* Point to begin of data after separating white space! */
X	  }
X
X
X	  key.dptr  = t;
X	  key.dsize = strlen(t)+1;
X	  dat.dptr  = s;
X	  dat.dsize = strlen(s)+1;
X
X	  if (dbm_store(dbmfile, key, dat, DBM_INSERT) != 0) {
X	    fprintf(stderr,"Key: `%s' is duplicate!\n",t);
X	  }
X	}
}
SHAR_EOF
chmod 0640 makendbm/makendbm.c ||
echo 'restore of makendbm/makendbm.c failed'
Wc_c="`wc -c < 'makendbm/makendbm.c'`"
test 2781 -eq "$Wc_c" ||
	echo 'makendbm/makendbm.c: original size 2781, current size' "$Wc_c"
fi
# ============= makendbm/ndbmlook.c ==============
if test -f 'makendbm/ndbmlook.c' -a X"$1" != X"-c"; then
	echo 'x - skipping makendbm/ndbmlook.c (File already exists)'
else
echo 'x - extracting makendbm/ndbmlook.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'makendbm/ndbmlook.c' &&
/* Copyright 1993 - Matti Aarnio, Turku University, Turku, Finland
X   This will be free software, but only when it is finished.
X
X   The way the Zmailer uses DBM entries is by using strings with
X   their terminating NULL as keys, and as data..  Thus the length
X   is strlen(string)+1, not strlen(string) !
*/
X
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ndbm.h>
X
#define PROG "ndbmlook"
X
extern int errno;
extern char *sys_errlist[];
extern int sys_nerr;
X
extern void create_dbase();
X
void
usage(err,errno)
char *err;
int errno;
{
X  fprintf(stderr,"Usage: "PROG" database.name key\n");
X  fprintf(stderr,"  (ndbm appends  .pag, and .dir  into actual db file names..)\n");
X  fprintf(stderr," Error now: %s",err);
X  if (errno > 0 && errno < sys_nerr)
X    fprintf(stderr,", errno=%d (%s)",errno,sys_errlist[errno]);
X  fprintf(stderr,"\n");
X  exit (1);
}
X
int
main(argc,argv)
int argc;
char *argv[];
{
X  char *dbasename = NULL;
X  FILE *infile = NULL;
X  DBM *dbmfile;
X  datum key;
X  datum result;
X
X  if (argc != 3) usage("wrong number of arguments",0);
X  dbasename = argv[1];
X  dbmfile = dbm_open(dbasename, O_RDONLY, 0644);
X  if (dbmfile == NULL)
X    usage("Can't open dbase file",errno);
X
X  key.dptr = argv[2];
X  key.dsize = strlen(argv[2]) +1;
X
X  result = dbm_fetch(dbmfile,key);
X  if (result.dptr == NULL) {
X    fprintf(stderr,"Key %s not found\n",argv[2]);
X    return 2;
X  }
X  printf("siz:%d, dat: %s\n",result.dsize,result.dptr);
X
X  dbm_close(dbmfile);
X
X  return 0;
}
SHAR_EOF
chmod 0640 makendbm/ndbmlook.c ||
echo 'restore of makendbm/ndbmlook.c failed'
Wc_c="`wc -c < 'makendbm/ndbmlook.c'`"
test 1513 -eq "$Wc_c" ||
	echo 'makendbm/ndbmlook.c: original size 1513, current size' "$Wc_c"
fi
exit 0