static char * all_keys_for_dbm (int refnum) { Dbm * db; Datum k; char * retval = NULL; size_t clue = 0; char * x; if (!(db = lookup_dbm(refnum))) return NULL; k = sdbm_firstkey(db->db); x = Datum_to_string(k); malloc_strcat_wordlist_c(&retval, space, x, &clue); new_free(&x); for (;;) { k = sdbm_nextkey(db->db); if (k.dptr == NULL) break; x = Datum_to_string(k); malloc_strcat_wordlist_c(&retval, space, x, &clue); new_free(&x); } return retval; }
static int error_from_dbm (int refnum) { Dbm * db; if (!(db = lookup_dbm(refnum))) return -1; return sdbm_error(db->db); }
static int close_dbm (int refnum) { Dbm *db; if (!(db = lookup_dbm(refnum))) return -1; remove_dbm(db); return 0; }
static int error_from_dbm (int refnum) { Dbm * db; /* dbm_open() returns -1 on failure, and we save errno */ if (refnum == -1) return last_failed_open_errno; if (!(db = lookup_dbm(refnum))) return -1; return sdbm_error(db->db); }
static char * iterate_on_dbm (int refnum, int restart) { Dbm * db; Datum k; if (!(db = lookup_dbm(refnum))) return NULL; if (restart) k = sdbm_firstkey(db->db); else k = sdbm_nextkey(db->db); return Datum_to_string(k); }
/* RETURNS A MALLOCED STRING, EH! */ static char * read_from_dbm (int refnum, char *key) { Dbm *db; Datum k, d; if (!(db = lookup_dbm(refnum))) return NULL; k.dptr = key; k.dsize = strlen(key); d = sdbm_fetch(db->db, k); if (d.dptr == NULL) return NULL; return Datum_to_string(d); }
static int write_to_dbm (int refnum, char *key, char *data, int replace) { Dbm *db; Datum k, d; if (!(db = lookup_dbm(refnum))) return -1; k.dptr = key; k.dsize = strlen(key); d.dptr = data; d.dsize = strlen(data); if (sdbm_store(db->db, k, d, replace? DBM_REPLACE : DBM_INSERT)) return sdbm_error(db->db); return 0; }
static int delete_from_dbm (int refnum, char *key) { Dbm * db; Datum k; int retval; if (!(db = lookup_dbm(refnum))) return -1; k.dptr = key; k.dsize = strlen(key); retval = sdbm_delete(db->db, k); if (retval == 1) return -1; /* Key Not found */ else if (retval == -1) return sdbm_error(db->db); /* Errno error */ else return 0; }