[net.sources] Simple shell interface for dbm

pc@ukc.UUCP (R.P.A.Collinson) (04/24/85)

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#-----cut here-----cut here-----cut here-----cut here-----
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	README
#	/usr/man/manl/dbm.l
#	Makefile
#	dbf.c
#	dbfirst.c
#	dbinit.c
#	dbnext.c
#	dbrm.c
#	dbs.c
# This archive created: Wed Apr 24 09:49:43 1985
cat << \SHAR_EOF > README
	This directory contains the sources for
a package of programs which jointly provide a shell script
interface to the 'dbm' library of database handling routines.

	File			Description
	----			-----------

	Makefile		Makefile
	README			This file
	dbf.c			Source of the fetch program.
	dbfirst.c		Source of the first key program.
	dbinit.c		Source of the initialisation program.
	dbm.1			Manual page.
	dbnext.c		Source of the next key program.
	dbrm.c			Source of the remove key program.
	dbs.c			Source of the store program.
SHAR_EOF
cat << \SHAR_EOF > /usr/man/manl/dbm.l
.TH DBM Local "UKC 18/4/85"
.SH NAME
dbm \- Shell interface to dbm library.
.SH SYNOPSIS
.B dbinit
dbfile,
.br
.B dbs
dbfile key content,
.br
.B dbf
dbfile key,
.br
.B dbrm
dbfile key,
.br
.B dbfirst
dbfile,
.br
.B dbnext
dbfile key,
.SH DESCRIPTION
.B Dbm
is a package of programs which jointly provide a shell-script interface to the
.I dbm(3x)
database routines.
For further details of the database itself, see that manual page.
.PP
The programs are as follows:
.IP dbinit 15n
This should be called once only, with the name of the database as
argument.
.B Dbinit
creates a pair of files needed by the
.I dbm(3x)
library.
.IP dbs 15n
Stores the data given as
.I content
under the
.I key
argument,
in the database
.I dbfile.
If the data could not be stored,
an error message is printed.
.IP dbf 15n
Fetches the data associated with
.I key
from the database
.I dbfile.
If the item is not in the database,
return a null string.
.IP dbrm 15n
Remove the data associated with
.I key
from the database named
.I dbfile.
If there was no such item, an error message is printed.
.IP dbfirst 15n
Given the name of a database,
return the first key.
Returns a null string if the database is empty.
.IP dbnext 15n
Given the name of a database and a key,
return the next key in succession.
At the end of the database,
a null string is returned.
.SH "RETURN VALUES"
All these programs return a zero exit code on success,
and a code of one on error.
.SH EXAMPLES
.PP
Suppose you wanted to keep records of the goal-scoring of soccer
players.
Having created an (empty) database called PLAYERS, with
.B dbinit,
you can store data as follows:
.sp 2
.ce 2
dbs PLAYERS rush "1,2,0,1"
dbs PLAYERS souness "0,1,2,1"
.PP
To fetch the data associated with the key "rush",
you would have
.sp 2
.ce
dbf PLAYERS rush
.PP
A simple shell script to traverse the entire database would be:
.sp 2
.nf
#! /bin/sh

key=`dbfirst PLAYERS`
while [ "X$key" != "X" ]
do
	echo -n "Scores for $key: "
	dbf PLAYERS $key
	key=`dbnext PLAYERS $key`
done
exit 0
.fi
.SH "SEE ALSO"
dbm(3x)
.SH AUTHOR
Peter Collinson, March 1985.
SHAR_EOF
cat << \SHAR_EOF > Makefile
#	Makefile	1.2	24/4/85
#
#	dbm shell interface Makefile
#
CFLAGS	=	-O
SRCS	=	dbf.c dbfirst.c dbnext.c dbrm.c dbs.c dbinit.c
OBJS	=	dbf.o dbfirst.o dbnext.o dbrm.o dbs.o dbinit.c
HDRS	=
TARGET	=	dbf dbfirst dbnext dbrm dbs dbinit
DESTDIR	=	/usr/local

all:	$(TARGET)

dbf:	dbf.c
	$(CC) -o dbf $(CFLAGS) dbf.c -ldbm

dbfirst:	dbfirst.c
	$(CC) -o dbfirst $(CFLAGS) dbfirst.c -ldbm

dbnext:	dbnext.c
	$(CC) -o dbnext $(CFLAGS) dbnext.c -ldbm

dbrm:	dbrm.c
	$(CC) -o dbrm $(CFLAGS) dbrm.c -ldbm

dbs:	dbs.c
	$(CC) -o dbs $(CFLAGS) dbs.c -ldbm

dbinit:	dbinit.c
	$(CC) -o dbinit $(CFLAGS) dbinit.c

sources:	$(SRCS) $(HDRS)

$(SRCS) $(HDRS):
	sccs get $@

tags:	$(SRCS) $(HDRS)
	ctags $(HDRS) $(SRCS)

clean:
	-rm -f $(TARGET) *.o errs comment tags

install:
	for i in $(TARGET); do \
		install -s -m 751 $$i $(DESTDIR);\
	done

SHAR_EOF
cat << \SHAR_EOF > dbf.c

#ifndef lint
static char sccsid[] = "@(#)dbf.c	1.1 (UKC) 3/3/85";
#endif  lint
/***

* program name:
	dbf.c
* function:
	dbf name key
	prints the content with the key in the named file
* switches:
* libraries used:
	standard -ldbm
* compile time parameters:
	cc -o dbf -O dbf.c -ldbm
* history:
	Written 15/1/85 Peter Collinson

***/
#include <stdio.h>
#include <dbm.h>

datum key, content;

main(argc, argv)
char **argv;
{	if (argc != 3)
	{	fprintf(stderr, "Usage: dbf name key\n");
		exit(1);
	}
	if (dbminit(argv[1]) < 0)
		exit(1);
	key.dptr = argv[2];
	key.dsize = strlen(argv[2])+1;
	content = fetch(key);
	if (content.dsize == key.dsize && strcmp(content.dptr, key.dptr) == 0)
		exit(1);
	printf("%s\n", content.dptr);
	exit(0);
}
SHAR_EOF
cat << \SHAR_EOF > dbfirst.c
#ifndef lint
static char sccsid[] = "@(#)dbfirst.c	1.1 (UKC) 3/3/85";
#endif  lint
/***

* program name:
	dbfirst.c
* function:
	dbfirst name
	returns the first key in a dbm file
* switches:
* libraries used:
	standard -ldbm
* compile time parameters:
	cc -o dbfirst -O dbfirst.c -ldbm
* history:
	Written 03/03/85 Peter Collinson UKC

***/
#include <stdio.h>
#include <dbm.h>

datum key, content;

main(argc, argv)
char **argv;
{	if (argc != 2)
	{	fprintf(stderr, "Usage: dbfirst name\n");
		exit(1);
	}
	if (dbminit(argv[1]) < 0)
		exit(1);
	key = firstkey();
	if (key.dsize)
		printf("%s\n", key.dptr);
	exit(0);
}
SHAR_EOF
cat << \SHAR_EOF > dbinit.c
#ifndef lint
static char sccsid[] = "@(#)dbinit.c	1.1 (UKC) 18/4/85";
#endif  lint
/***

* program name:
	dbinit.c
* function:
	Create the pair of files needed for a 'dbm' database.
* switches:
* libraries used:
	standard -ldbm
* compile time parameters:
	cc -o dbinit -O dbinit.c -ldbm
* history:
	Written 18/4/85 Richard Hellier

***/
#include <stdio.h>
#include <dbm.h>

main(argc, argv)
int	argc;
char	**argv;{
	char	dir_file[BUFSIZ],
		pag_file[BUFSIZ];

	if (argc != 2) {
		fprintf(stderr, "Usage: dbinit name\n");
		exit(1);
	}
	strcpy(dir_file, argv[1]);
	strcat(dir_file, ".dir");

	if (close(creat(dir_file, 0666)) == -1) {
		fprintf(stderr, "Can't create directory file (%s)\n", dir_file);
		exit(1);
	}

	strcpy(pag_file, argv[1]);
	strcat(pag_file, ".pag");

	if (close(creat(pag_file, 0666)) == -1) {
		fprintf(stderr, "Can't create data file (%s)\n", pag_file);
		unlink(dir_file);
		exit(1);
	}
	exit(0);
}
SHAR_EOF
cat << \SHAR_EOF > dbnext.c

#ifndef lint
static char sccsid[] = "@(#)dbnext.c	1.1 (UKC) 3/3/85";
#endif  lint
/***

* program name:
	dbnext.c
* function:
	dbnext name key
	prints the next key from a database
* switches:
* libraries used:
	standard -ldbm
* compile time parameters:
	cc -o dbnext -O dbnext.c -ldbm
* history:
	Written 03/03/85 Peter Collinson

***/
#include <stdio.h>
#include <dbm.h>

datum key, nxt;

main(argc, argv)
char **argv;
{	if (argc != 3)
	{	fprintf(stderr, "Usage: dbnext name key\n");
		exit(1);
	}
	if (dbminit(argv[1]) < 0)
		exit(1);
	key.dptr = argv[2];
	key.dsize = strlen(argv[2])+1;
	nxt = nextkey(key);
	if (nxt.dsize == key.dsize && strcmp(nxt.dptr, key.dptr) == 0)
		exit(1);
	printf("%s\n", nxt.dptr);
	exit(0);
}
SHAR_EOF
cat << \SHAR_EOF > dbrm.c
#ifndef lint
static char sccsid[] = "@(#)dbrm.c	1.1 (UKC) 3/3/85";
#endif  lint
/***

* program name:
	dbrm.c
* function:
	dbrm name key
	deletes the content with the key in the named file
* switches:
* libraries used:
	standard -ldbm
* compile time parameters:
	cc -o dbrm -O dbrm.c -ldbm
* history:
	Written 15/1/85 Peter Collinson

***/
#include <stdio.h>
#include <dbm.h>

datum key, content;

main(argc, argv)
char **argv;
{	if (argc != 3)
	{	fprintf(stderr, "Usage: dbrm name key\n");
		exit(1);
	}
	if (dbminit(argv[1]) < 0)
		exit(1);
	key.dptr = argv[2];
	key.dsize = strlen(argv[2])+1;
	if (delete(key) < 0)
	{	fprintf(stderr, "dbrm %s failed\n", argv[2]);
		exit(1);
	}
	exit(0);
}
SHAR_EOF
cat << \SHAR_EOF > dbs.c
#ifndef lint
static char sccsid[] = "@(#)dbs.c	1.1 (UKC) 3/3/85";
#endif  lint
/***

* program name:
	dbs.c
* function:
	dbs name key content
	loads the content with the key in the named file
* switches:
* libraries used:
	standard -ldbm
* compile time parameters:
	cc -o dbs -O dbs.c -ldbm
* history:
	Written 15/1/85 Peter Collinson

***/
#include <stdio.h>
#include <dbm.h>

datum key, content;

main(argc, argv)
char **argv;
{	if (argc != 4)
	{	fprintf(stderr, "Usage: dbs name key content\n");
		exit(1);
	}
	if (dbminit(argv[1]) < 0)
		exit(1);
	key.dptr = argv[2];
	key.dsize = strlen(argv[2])+1;
	content.dptr = argv[3];
	content.dsize = strlen(argv[3])+1;
	if (store(key, content) < 0)
	{	fprintf(stderr, "dbs %s %s failed\n", argv[2], argv[3]);
		exit(1);
	}
	exit(0);
}
SHAR_EOF
#	End of shell archive
exit 0
-- 
Pete Collinson
pc@ukc