/*
 * $jwk: postfix-stats-get.c,v 1.2 2007/05/30 01:24:27 jwk Exp $
 *
 * Copyright (c) 2004 by Joel Knight
 * www.packetmischief.ca
 *
 * [2004.09.21]
 */


#include <stdio.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef LINUX
#include <db_185.h>
#else
#include <db.h>
#endif

#define DBFILE "/tmp/postfix-stats.db"

int main(int argc, char *argv[])
{
	DB *db = NULL;
	DBT key, value;

	if (argc == 1)
		exit(1);
	
	if ((db = dbopen(DBFILE, O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 
			DB_HASH, NULL)) == NULL) {
		perror("Couldn't open dbfile");
		return (1);
	}
	
	memset(&key, 0, sizeof(key));
	memset(&value, 0, sizeof(value));
	key.data = (u_char *)argv[1];
	key.size = strlen(argv[1]);
	if (db->get(db, &key, &value, NULL)) {
		printf("No such key \"%s\"\n", key.data);
		return (1);
	}

	printf("%d\n", atoi(value.data));
	
	db->close(db);
	return (0);
}
