X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/052b36d05a622a93733b735acce2de865b14627b..b817bfc642225b8c3c0b6a7e42d1fb949b61a606:/key-io.c diff --git a/key-io.c b/key-io.c index 6d4876f..c78b967 100644 --- a/key-io.c +++ b/key-io.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: key-io.c,v 1.2 2000/02/12 18:21:02 mdw Exp $ + * $Id: key-io.c,v 1.6 2004/04/08 01:36:15 mdw Exp $ * * Adding new keys to a key file * @@ -27,17 +27,6 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: key-io.c,v $ - * Revision 1.2 2000/02/12 18:21:02 mdw - * Overhaul of key management (again). - * - * Revision 1.1 1999/12/22 15:47:48 mdw - * Major key-management revision. - * - */ - /*----- Header files ------------------------------------------------------*/ #include @@ -60,7 +49,7 @@ /*----- Tweakable macros --------------------------------------------------*/ -#define KEY_LOAD(n) ((n) * 2) +#define KEY_INITSZ 16 /*----- Low-level functions -----------------------------------------------*/ @@ -120,7 +109,7 @@ static int insert(key_file *f, key *k) if (f->idload > 0) f->idload--; else if (hash_extend(&f->byid)) - f->idload = KEY_LOAD(f->byid.mask / 2); + f->idload = SYM_LIMIT(f->byid.mask / 2); /* --- Insert into the type table --- */ @@ -417,7 +406,7 @@ int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf) * * Arguments: @key_file *f@ = pointer to file structure to initialize * @const char *file@ = pointer to the file name - * @int how@ = opening options (@KOPEN_*@). + * @unsigned how@ = opening options (@KOPEN_*@). * @key_reporter *rep@ = error reporting function * @void *arg@ = argument for function * @@ -431,33 +420,27 @@ int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf) * owner only. */ -int key_open(key_file *f, const char *file, int how, +int key_open(key_file *f, const char *file, unsigned how, key_reporter *rep, void *arg) { - if (key_lockfile(f, file, how)) + if (key_lockfile(f, file, how)) { + rep(file, 0, strerror(errno), arg); return (-1); - - /* --- Trivial bits of initialization --- */ - + } f->f = 0; f->name = xstrdup(file); - /* --- Read the file of keys into the table --- */ - - hash_create(&f->byid, 16); - f->idload = KEY_LOAD(16); + hash_create(&f->byid, KEY_INITSZ); + f->idload = SYM_LIMIT(KEY_INITSZ); sym_create(&f->bytype); sym_create(&f->bytag); f->f |= KF_WRITE; - key_merge(f, file, f->fp, rep, arg); - if (how == KOPEN_READ) - f->f &= ~(KF_WRITE | KF_MODIFIED); - else - f->f &= ~KF_MODIFIED; - - /* --- Close the file if only needed for reading --- */ + if (f->fp) + key_merge(f, file, f->fp, rep, arg); + f->f &= ~KF_MODIFIED; - if (how == KOPEN_READ) { + if ((how & KOPEN_MASK) == KOPEN_READ) { + f->f &= ~KF_WRITE; fclose(f->fp); f->fp = 0; } @@ -481,7 +464,7 @@ int key_close(key_file *f) hash_base *b; hash_iter i; - if ((e = key_save(f)) != KWRITE_OK) + if (f->fp && (e = key_save(f)) != KWRITE_OK) return (e); /* --- Free all the individual keys --- */