/* -*-c-*-
*
- * $Id: key-io.c,v 1.3 2001/01/20 11:56:48 mdw Exp $
+ * $Id: key-io.c,v 1.4 2001/02/03 11:57:38 mdw Exp $
*
* Adding new keys to a key file
*
/*----- Revision history --------------------------------------------------*
*
* $Log: key-io.c,v $
+ * Revision 1.4 2001/02/03 11:57:38 mdw
+ * Allow creating keyfiles with no file attached.
+ *
* Revision 1.3 2001/01/20 11:56:48 mdw
* Use mLib exported tuning parameters for hashtable.
*
*
* 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
*
* 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))
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, 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;
}
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 --- */