home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- * The Elm Mail System - $Revision: 5.3 $ $State: Exp $
- *
- * Copyright (c) 1988-1992 USENET Community Trust
- * Copyright (c) 1986,1987 Dave Taylor
- *******************************************************************************
- * Bug reports, patches, comments, suggestions should be sent to:
- *
- * Syd Weinstein, Elm Coordinator
- * elm@DSI.COM dsinc!elm
- *
- *******************************************************************************
- * $Log: aliasdb.c,v $
- * Revision 5.3 1993/04/21 01:40:12 syd
- * add seekset define
- *
- * Revision 5.2 1993/04/12 01:53:38 syd
- * Added fetch_alias() and next_addr_in_list() routines for use in
- * new elmalias utility.
- * From: chip@chinacat.unicom.com (Chip Rosenthal)
- *
- * Revision 5.1 1992/12/20 05:14:05 syd
- * Initial checkin
- *
- *
- ******************************************************************************/
-
- /** Alias interface with dbz routines.
-
- This code is shared with newalias and elm so that
- it is easier to do updates while in elm. The routines in
- this file are interface routines between elm alias code,
- newalias, and listalias and the dbm routines.
-
- **/
-
- #include "headers.h"
- #include <ctype.h>
- #include "ndbz.h"
-
- #ifndef SEEK_SET
- #define SEEK_SET 0 /* Set file pointer to "offset" */
- #define SEEK_CUR 1 /* Set file pointer to current plus "offset" */
- #define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
- #endif
-
- #ifdef BSD
- # include <sys/file.h>
- # undef tolower
- # undef toupper
- #endif
-
- /* byte-ordering stuff */
- #define MAPIN(o) ((db->dbz_bytesame) ? (of_t) (o) : bytemap((of_t)(o), db->dbz_conf.bytemap, db->dbz_mybmap))
- #define MAPOUT(o) ((db->dbz_bytesame) ? (of_t) (o) : bytemap((of_t)(o), db->dbz_mybmap, db->dbz_conf.bytemap))
-
- static of_t /* transformed result */
- bytemap(ino, map1, map2)
- of_t ino;
- int *map1;
- int *map2;
- {
- union oc {
- of_t o;
- char c[SOF];
- };
- union oc in;
- union oc out;
- register int i;
-
- in.o = ino;
- for (i = 0; i < SOF; i++)
- out.c[map2[i]] = in.c[map1[i]];
- return(out.o);
- }
-
- read_one_alias(db, ar)
- DBZ *db;
- struct alias_rec *ar;
- {
- /*
- * Read an alias (name, address, etc.) from the data file
- */
-
- FILE *data_file = db->dbz_basef;
-
- if (data_file == NULL)
- return(0); /* no alias file, but hash exists, error condition */
-
- if (fread((char *) ar, sizeof(struct alias_rec), 1, data_file) <= 0)
- return(0);
-
- ar->status = (int) MAPIN(ar->status);
- ar->alias = (char *) MAPIN(ar->alias);
- ar->last_name = (char *) MAPIN(ar->last_name);
- ar->name = (char *) MAPIN(ar->name);
- ar->comment = (char *) MAPIN(ar->comment);
- ar->address = (char *) MAPIN(ar->address);
- ar->type = (int) MAPIN(ar->type);
- ar->length = (long) MAPIN(ar->length);
-
- return(1);
- }
-
-
- /*
- * Retrieve an alias record and information from a database.
- *
- * If "alias" is non-NULL, it is the name of the alias to fetch (searching
- * is case insensitive). If the alias is found, we return a pointer to
- * dynamically allocated memory that holds an alias record *plus* the
- * text data for that record. If the lookup fails or an error occurs,
- * then a NULL is returned.
- *
- * If "alias" is NULL then the next alias in the database is retrieved.
- * This may be used to scan through the database. Again, a pointer to
- * dynamically allocated memory is returned. When the end of file is
- * reached, a NULL is returned.
- */
- struct alias_rec *fetch_alias(db, alias)
- DBZ *db;
- char *alias;
- {
- datum key, val;
- struct alias_rec arec;
- long pos;
- register struct alias_rec *ar;
- register char *buf, *s;
-
- /*
- * If an alias is specifed then locate it.
- */
- if (alias != NULL) {
-
- /*
- * Fetch location of this alias.
- */
- key.dptr = safe_strdup(alias);
- for (s = key.dptr ; *s != '\0' ; ++s) {
- if (isascii(*s) && isupper(*s))
- *s = tolower(*s);
- }
- key.dsize = strlen(alias);
- val = dbz_fetch(db, key);
- (void) free((malloc_t)key.dptr);
-
- /*
- * Make sure the alias was found.
- */
- if (val.dptr == NULL)
- return (struct alias_rec *)NULL;
-
- /*
- * Sanity check - return value should be a seek offset.
- */
- if (val.dsize != sizeof(long))
- return (struct alias_rec *)NULL;
-
- /*
- * Move to the position of the selected alias record.
- */
- pos = *((long *)(val.dptr)) - sizeof(struct alias_rec);
- if (fseek(db->dbz_basef, pos, SEEK_SET) != 0)
- return (struct alias_rec *)NULL;
-
- }
-
- /*
- * We are now positioned at the alias record we want. Pull it in.
- */
- if (!read_one_alias(db, &arec))
- return (struct alias_rec *)NULL;
-
- /*
- * Allocate space to hold the alias record and data content.
- */
- ar = (struct alias_rec *)
- safe_malloc(sizeof(struct alias_rec) + arec.length);
- *ar = arec;
- buf = (char *)ar + sizeof(struct alias_rec);
-
- /*
- * Read in the data content and fixup pointers in the alias record.
- */
- if (fread(buf, ar->length, 1, db->dbz_basef) != 1)
- return (struct alias_rec *)NULL;
- ar->alias += (int) buf;
- ar->last_name += (int) buf;
- ar->name += (int) buf;
- ar->comment += (int) buf;
- ar->address += (int) buf;
-
- return ar;
- }
-
-
- /*
- * Return a pointer to the next address in this list, and update the pointer
- * to the list. Addresses are seperated by whitespace and/or commas.
- * Return NULL when list finished. This routine scribbles on the list.
- */
- char *next_addr_in_list(aptr)
- char **aptr;
- {
- register char *front, *back;
-
- /*
- * Locate the first letter of the address.
- */
- front = *aptr;
- while (*front == ',' || isspace(*front))
- ++front;
- if (*front == '\0')
- return (char *) NULL;
-
- /*
- * Locate the end of the address.
- */
- back = front;
- while (*back != '\0' && *back != ',' & !isspace(*back))
- ++back;
- if (*back != '\0')
- *back++ = '\0';
-
- *aptr = back;
- return front;
- }
-
-