Fix bug dividing small things by large ones.
[u/mdw/catacomb] / key-misc.c
index 2d9c855..afd0a4b 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: key-misc.c,v 1.1 1999/12/22 15:47:48 mdw Exp $
+ * $Id: key-misc.c,v 1.3 2001/06/22 19:39:12 mdw Exp $
  *
  * Simple key management
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: key-misc.c,v $
+ * Revision 1.3  2001/06/22 19:39:12  mdw
+ * New interface to find out whether a key has expired.  Also, a bug fix
+ * to @key_bytag@ so that it finds expired keys correctly.
+ *
+ * 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.
  *
@@ -42,7 +49,6 @@
 #include <string.h>
 #include <time.h>
 
-#include <mLib/alloc.h>
 #include <mLib/bits.h>
 #include <mLib/hash.h>
 #include <mLib/sub.h>
 
 #define KEY_LOAD(n) ((n) * 2)
 
-/*----- Error reporting ---------------------------------------------------*/
-
-/* --- @key_strerror@ --- *
- *
- * Arguments:  @int err@ = error code from @key_new@
- *
- * Returns:    Pointer to error string.
- *
- * Use:                Translates a @KERR@ error code into a human-readable
- *             string.
- */
-
-const char *key_strerror(int err)
-{
-  char *tab[] = {
-    "No error",
-    "Bad tag string",
-    "Bad type string",
-    "Bad comment string",
-    "Keyid already exists",
-    "Key tag already exists",
-    "Key file is read-only",
-    "Key will eventually expire",
-    "Bad key flags string",
-    "Unknown error code"
-  };
-  
-  unsigned e = -err;
-  if (e >= KERR_MAX)
-    e = KERR_MAX;
- return (tab[e]);
-}
-
 /*----- Iteration and iterators -------------------------------------------*/
 
 /* --- @key_mkiter@ --- *
@@ -205,7 +178,7 @@ key *key_bytag(key_file *f, const char *tag)
   uint32 id;
   key_ref *kr = sym_find(&f->bytag, tag, -1, 0, 0);
 
-  if (kr && !(KEY_EXPIRED(t, kr->k->exp) && KEY_EXPIRED(t, kr->k->exp)))
+  if (kr && !(KEY_EXPIRED(t, kr->k->exp) && KEY_EXPIRED(t, kr->k->del)))
     return (kr->k);
   id = strtoul(tag, &p, 16);
   if (!*p)
@@ -325,6 +298,19 @@ int key_delete(key_file *f, key *k)
   return (0);
 }
 
+/* --- @key_expired@ --- *
+ *
+ * Arguments:  @key *k@ = pointer to key block
+ *
+ * Returns:    Zero if the key is OK, nonzero if it's expired.
+ */
+
+int key_expired(key *k)
+{
+  time_t now = time(0);
+  return (KEY_EXPIRED(now, k->exp) || KEY_EXPIRED(now, k->del));
+}
+
 /* --- @key_expire@ --- *
  *
  * Arguments:  @key_file *f@ = pointer to file block