home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume26
/
modempool
/
part01
/
db.c
next >
Wrap
C/C++ Source or Header
|
1993-04-05
|
4KB
|
159 lines
/*******************************************************************
*
* Module: @(#)db.c 4.2
*
* Description:
* Handle user database functions.
*
* Revision:
* Date By Reason
* ---- -- ------
* 910414 Lars Berntzon Created
* 910124 Lars H Carlsson Changed pid format to %10d\n
* 910124 Lars H Carlsson Changed mode of lockfile to 0444
* 910129 Lars Berntzon Changed to termio for eightbit tty.
* 910224 Lars Berntzon Newline right when loggin in.
* 910227 Lars Berntzon The loggfile now reopens everytime.
*
*******************************************************************/
static char SccsId[] = "@(#)db.c 4.2 92/04/16";
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdarg.h>
#include "modempool.h"
/*
* Local stuff.
*/
static FILE *db_fp = NULL;
static db_open(char *filename);
static db_read(char **ptrs, int n_ptrs);
static db_close(void);
/*******************************************************************
* D B _ F I N D
* -------------
*
* Description:
* Lookup user in database.
*
* Arguments:
* filename - Name of users database.
* ptrs - Pointers to result vectors.
* n_ptrs - Number of vectors.
* key_fiels - Key-field index.
* key - Value to match.
*
* Return:
* E_OK
* E_FAIL - Internal error.
* E_NOTFOUND - Key not found.
*
*******************************************************************/
db_find(char *filename, char **ptrs, int n_ptrs, int key_field, char *key)
{
if (key == NULL) {
logerr("db_find: programming error: null-key");
return E_FAIL;
}
if (key_field >= n_ptrs) {
logerr("db_find: programming error: key_field >= n_ptrs");
}
if (db_open(filename) != E_OK) {
return E_FAIL;
}
while(db_read(ptrs, n_ptrs) == E_OK)
{
if (ptrs[key_field] != NULL && strcmp(ptrs[key_field], key) == 0)
{
db_close();
return E_OK;
}
}
db_close();
return E_NOTFOUND;
}
/*******************************************************************
* D B _ R E A D
* -------------
*
* Description:
* Read one row in users database.
*
* Arguments:
* ptrs - Pointers to result vectors.
* n_ptrs - Number of verctors.
*
* Return:
* E_OK
* E_EOF - No more users.
*
*******************************************************************/
static db_read(char **ptrs, int n_ptrs)
{
static char in_line[NAME_SIZE];
char *p;
int i;
while (fgets(in_line, sizeof in_line, db_fp) != NULL)
{
p = strtok(in_line, ":\n");
for(i = 0; i < n_ptrs; i++) {
ptrs[i] = p;
if (p != NULL) p = strtok(NULL, ":\n");
}
if (ptrs[0] == NULL || ptrs[0][0] == '#') continue;
return E_OK;
}
return E_EOF;
}
/*******************************************************************
* D B _ O P E N
* -------------
*
* Description:
* Open the users database.
*
* Arguments:
* filename - the users database filename.
*
*******************************************************************/
static db_open(char *filename)
{
if ((db_fp = fopen(filename, "r")) == NULL) {
logerr("db_open: failed to open user database");
return E_FAIL;
}
return E_OK;
}
/*******************************************************************
* D B _ C L O S E
* ---------------
*
* Description:
* Close the users database.
*
*******************************************************************/
static db_close(void)
{
if (db_fp) fclose(db_fp);
db_fp = NULL;
return E_OK;
}