int pw_dbm_remove(const struct passwd *pw) { datum key; static int once; char data[BUFSIZ]; if (! once) { if (! pw_dbm) setpwent (); once++; } if (! pw_dbm) return 0; key.dsize = strlen (pw->pw_name); key.dptr = pw->pw_name; if (dbm_delete (pw_dbm, key)) return 0; key.dsize = sizeof pw->pw_uid; key.dptr = (char *) &pw->pw_uid; if (dbm_delete (pw_dbm, key)) return 0; return 1; }
int database_delete(DBM *db, s48_value key) { datum work_key; work_key.dptr = ADDRESS_AFTER_HEADER(key, char); work_key.dsize = S48_STRING_LENGTH(key); return(dbm_delete(db, work_key)); }
value caml_dbm_delete(value vdb, value vkey) /* ML */ { datum key; key.dptr = String_val(vkey); key.dsize = string_length(vkey); if (dbm_delete(extract_dbm(vdb), key) < 0) raise_dbm("dbm_delete"); else return Val_unit; }
void runSuccess() { char buf[] = "file"; //@ assert \valid(buf+(0..\block_length(buf)-1)); //@ assert \exists integer x; buf[x] == '\0'; DBM* db = dbm_open(buf, O_RDWR, S_IRUSR); if (db == NULL) {return;} dbm_delete(db, anydatum()); }
int otp_delete (void *v, OtpContext *ctx) { DBM *dbm = (DBM *)v; datum key; key.dsize = strlen(ctx->user); key.dptr = ctx->user; return dbm_delete(dbm, key); }
int ldbm_delete( LDBM ldbm, Datum key ) { int rc; LDBM_WLOCK; rc = dbm_delete( ldbm, key ); LDBM_WUNLOCK; return rc; }
static apr_status_t vt_ndbm_del(apr_dbm_t *dbm, apr_datum_t key) { int rc; datum kd; kd.dptr = key.dptr; kd.dsize = key.dsize; rc = dbm_delete(dbm->file, kd); /* store any error info into DBM, and return a status code. */ return set_error(dbm, ndbm2s(rc)); }
void dbm_del_group(DBM *dbmgroup, char *group) { datum key,resp; key.dptr = group; key.dsize = strlen(group); resp = dbm_fetch(dbmgroup,key); if (resp.dptr) { printf("Deleting group %s.\n",group); dbm_delete(dbmgroup,key); } else { fprintf(stderr,"Group %s does not exist.\n",group); exit(1); } }
void testValues() { f = 2; int result; char buf[] = "file"; //@ assert \valid(buf+(0..\block_length(buf)-1)); //@ assert \exists integer x; buf[x] == '\0'; DBM* db = dbm_open(buf, O_RDWR, S_IRUSR); if (db == NULL) {return;} result = dbm_delete(db, anydatum()); //@ assert result <= 0; //@ assert f == 2; //@ assert vacuous: \false; }
static int dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) { datum krec, drec; int tmp_size; if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, "dbm mappings have string indices only"); return -1; } krec.dsize = tmp_size; if (dp->di_dbm == NULL) { PyErr_SetString(DbmError, "DBM object has already been closed"); return -1; } dp->di_size = -1; if (w == NULL) { if ( dbm_delete(dp->di_dbm, krec) < 0 ) { dbm_clearerr(dp->di_dbm); PyErr_SetString(PyExc_KeyError, PyString_AS_STRING((PyStringObject *)v)); return -1; } } else { if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, "dbm mappings have string elements only"); return -1; } drec.dsize = tmp_size; if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) { dbm_clearerr(dp->di_dbm); PyErr_SetString(DbmError, "cannot add item to database"); return -1; } } if ( dbm_error(dp->di_dbm) ) { dbm_clearerr(dp->di_dbm); PyErr_SetString(DbmError, ""); return -1; } return 0; }
static krb5_error_code NDBM__del(krb5_context context, HDB *db, krb5_data key) { struct ndbm_db *d = (struct ndbm_db *)db->hdb_db; datum k; int code; krb5_error_code ret; k.dptr = key.data; k.dsize = key.length; ret = db->hdb_lock(context, db, HDB_WLOCK); if(ret) return ret; code = dbm_delete(d->db, k); db->hdb_unlock(context, db); if(code < 0) return errno; return 0; }
int del_cdt_entry(const char *cd_catalog_ptr, const int track_no) { char key_to_del[CAT_CAT_LEN + 10]; datum local_key_datum; int result; /* check database initialized and parameters valid */ if (!cdc_dbm_ptr || !cdt_dbm_ptr) return (0); if (strlen(cd_catalog_ptr) >= CAT_CAT_LEN) return (0); /* ensure the search key contains only the valid string and nulls */ memset(&key_to_del, '\0', sizeof(key_to_del)); sprintf(key_to_del, "%s %d", cd_catalog_ptr, track_no); local_key_datum.dptr = (void *) key_to_del; local_key_datum.dsize = sizeof(key_to_del); result = dbm_delete(cdt_dbm_ptr, local_key_datum); /* dbm_delete() uses 0 for success */ if (result == 0) return (1); return (0); } /* del_cdt_entry */
static int _ndbm_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key) { DBM *dbm = db->db_descr; datum keydat; keydat.dptr = key->mu_dptr; keydat.dsize = key->mu_dsize; errno = 0; switch (dbm_delete (dbm, keydat)) { case 0: break; case 1: return MU_ERR_NOENT; case -1: db->db_errno.n = errno; return MU_ERR_FAILURE; } return 0; }
int main(void) { struct test_data items_to_store[ITEMS_USED]; struct test_data items_retrieved; char key_to_use[20]; int i, result; datum key_datum; datum data_datum; DBM *dbm_ptr; dbm_ptr = dbm_open(TEST_DB_FILE, O_RDWR | O_CREAT, 0666); if(!dbm_ptr) { fprintf(stderr, "Failed to open database\n"); exit(EXIT_FAILURE); } memset(items_to_store, '\0', sizeof(items_to_store)); strcpy(items_to_store[0].misc_chars, "First!"); items_to_store[0].any_integer = 47; strcpy(items_to_store[0].more_chars, "foo"); strcpy(items_to_store[1].misc_chars, "bar"); items_to_store[1].any_integer = 13; strcpy(items_to_store[1].more_chars, "unlucky?"); strcpy(items_to_store[2].misc_chars, "Third"); items_to_store[2].any_integer = 3; strcpy(items_to_store[2].more_chars, "baz"); for(i = 0; i < ITEMS_USED; i++) { sprintf(key_to_use, "%c%c%d", items_to_store[i].misc_chars[0], items_to_store[i].more_chars[0], items_to_store[i].any_integer); key_datum.dptr = (void *)key_to_use; key_datum.dsize = strlen(key_to_use); data_datum.dptr = (void *)&items_to_store[i]; data_datum.dsize = sizeof(struct test_data); result = dbm_store(dbm_ptr, key_datum, data_datum, DBM_REPLACE); if(result != 0) { fprintf(stderr, "dbm_store failed on key %s\n", key_to_use); exit(2); } } /* let's delete some data */ sprintf(key_to_use, "bu%d", 13); key_datum.dptr = key_to_use; key_datum.dsize = strlen(key_to_use); if(dbm_delete(dbm_ptr, key_datum) == 0) printf("Data with key %s deleted\n", key_to_use); else printf("nothing deleted for key %s", key_to_use); for(key_datum =dbm_firstkey(dbm_ptr); key_datum.dptr; key_datum = dbm_nextkey(dbm_ptr)) { data_datum = dbm_fetch(dbm_ptr, key_datum); if(data_datum.dptr) { printf("Data retrieved\n"); memcpy(&items_retrieved, data_datum.dptr, data_datum.dsize); printf("Retrieved items - %s %d %s\n", items_retrieved.misc_chars, items_retrieved.any_integer, items_retrieved.more_chars); } else printf("No data found for key %s\n", key_to_use); } dbm_close(dbm_ptr); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { typedef enum { YOW, FETCH, STORE, DELETE, SCAN, REGEXP } commands; char opt; int flags; int giveusage = 0; int verbose = 0; commands what = YOW; char *comarg[3]; int st_flag = DBM_INSERT; int argn; DBM *db; datum key; datum content; flags = O_RDWR; argn = 0; while ((opt = getopt(argc, argv, "acdfFm:rstvx")) != ':') { switch (opt) { case 'a': what = SCAN; break; case 'c': flags |= O_CREAT; break; case 'd': what = DELETE; break; case 'f': what = FETCH; break; case 'F': what = REGEXP; break; case 'm': flags &= ~(000007); if (strcmp(optarg, "r") == 0) flags |= O_RDONLY; else if (strcmp(optarg, "w") == 0) flags |= O_WRONLY; else if (strcmp(optarg, "rw") == 0) flags |= O_RDWR; else { fprintf(stderr, "Invalid mode: \"%s\"\n", optarg); giveusage = 1; } break; case 'r': st_flag = DBM_REPLACE; break; case 's': what = STORE; break; case 't': flags |= O_TRUNC; break; case 'v': verbose = 1; break; case 'x': flags |= O_EXCL; break; case '!': giveusage = 1; break; case '?': if (argn < 3) comarg[argn++] = optarg; else { fprintf(stderr, "Too many arguments.\n"); giveusage = 1; } break; } } if (giveusage || what == YOW || argn < 1) { fprintf(stderr, "Usage: %s database [-m r|w|rw] [-crtx] -a|-d|-f|-F|-s [key [content]]\n", argv[0]); exit(-1); } if ((db = dbm_open(comarg[0], flags, 0777)) == NULL) { fprintf(stderr, "Error opening database \"%s\"\n", comarg[0]); exit(-1); } if (argn > 1) key = read_datum(comarg[1]); if (argn > 2) content = read_datum(comarg[2]); switch (what) { case SCAN: key = dbm_firstkey(db); if (dbm_error(db)) { fprintf(stderr, "Error when fetching first key\n"); goto db_exit; } while (key.dptr != NULL) { content = dbm_fetch(db, key); if (dbm_error(db)) { fprintf(stderr, "Error when fetching "); print_datum(key); printf("\n"); goto db_exit; } print_datum(key); printf(": "); print_datum(content); printf("\n"); if (dbm_error(db)) { fprintf(stderr, "Error when fetching next key\n"); goto db_exit; } key = dbm_nextkey(db); } break; case REGEXP: if (argn < 2) { fprintf(stderr, "Missing regular expression.\n"); goto db_exit; } if (re_comp(comarg[1])) { fprintf(stderr, "Invalid regular expression\n"); goto db_exit; } key = dbm_firstkey(db); if (dbm_error(db)) { fprintf(stderr, "Error when fetching first key\n"); goto db_exit; } while (key.dptr != NULL) { if (re_exec(key2s(key))) { content = dbm_fetch(db, key); if (dbm_error(db)) { fprintf(stderr, "Error when fetching "); print_datum(key); printf("\n"); goto db_exit; } print_datum(key); printf(": "); print_datum(content); printf("\n"); if (dbm_error(db)) { fprintf(stderr, "Error when fetching next key\n"); goto db_exit; } } key = dbm_nextkey(db); } break; case FETCH: if (argn < 2) { fprintf(stderr, "Missing fetch key.\n"); goto db_exit; } content = dbm_fetch(db, key); if (dbm_error(db)) { fprintf(stderr, "Error when fetching "); print_datum(key); printf("\n"); goto db_exit; } if (content.dptr == NULL) { fprintf(stderr, "Cannot find "); print_datum(key); printf("\n"); goto db_exit; } print_datum(key); printf(": "); print_datum(content); printf("\n"); break; case DELETE: if (argn < 2) { fprintf(stderr, "Missing delete key.\n"); goto db_exit; } if (dbm_delete(db, key) || dbm_error(db)) { fprintf(stderr, "Error when deleting "); print_datum(key); printf("\n"); goto db_exit; } if (verbose) { print_datum(key); printf(": DELETED\n"); } break; case STORE: if (argn < 3) { fprintf(stderr, "Missing key and/or content.\n"); goto db_exit; } if (dbm_store(db, key, content, st_flag) || dbm_error(db)) { fprintf(stderr, "Error when storing "); print_datum(key); printf("\n"); goto db_exit; } if (verbose) { print_datum(key); printf(": "); print_datum(content); printf(" STORED\n"); } break; } db_exit: dbm_clearerr(db); dbm_close(db); if (dbm_error(db)) { fprintf(stderr, "Error closing database \"%s\"\n", comarg[0]); exit(-1); } }
int _sasldb_putdata(const sasl_utils_t *utils, sasl_conn_t *conn, const char *authid, const char *realm, const char *propName, const char *data, size_t data_len) { int result = SASL_OK; char *key; size_t key_len; DBM *db; datum dkey; void *cntxt; sasl_getopt_t *getopt; const char *path = SASL_DB_PATH; if (!utils) return SASL_BADPARAM; if (!authid || !realm || !propName) { utils->seterror(conn, 0, "Bad parameter in db_ndbm.c: _sasldb_putdata"); return SASL_BADPARAM; } result = _sasldb_alloc_key(utils, authid, realm, propName, &key, &key_len); if (result != SASL_OK) { utils->seterror(conn, 0, "Could not allocate key in _sasldb_putdata"); return result; } if (utils->getcallback(conn, SASL_CB_GETOPT, (sasl_callback_ft *)&getopt, &cntxt) == SASL_OK) { const char *p; if (getopt(cntxt, NULL, "sasldb_path", &p, NULL) == SASL_OK && p != NULL && *p != 0) { path = p; } } db = dbm_open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if (! db) { utils->log(conn, SASL_LOG_ERR, "SASL error opening password file. " "Do you have write permissions?\n"); utils->seterror(conn, 0, "Could not open db for write"); result = SASL_FAIL; goto cleanup; } dkey.dptr = key; dkey.dsize = key_len; if (data) { datum dvalue; dvalue.dptr = (void *)data; if(!data_len) data_len = strlen(data); dvalue.dsize = data_len; if (dbm_store(db, dkey, dvalue, DBM_REPLACE)) { utils->seterror(conn, 0, "Couldn't update db"); result = SASL_FAIL; } } else { if (dbm_delete(db, dkey)) { utils->seterror(conn, 0, "Couldn't update db"); result = SASL_NOUSER; } } dbm_close(db); cleanup: utils->free(key); return result; }
int main(int argc, char *argv[]) { int i, dbRet; DBM *myDB; datum daKey, daVal; char *keys[NUMELE] = {"foo", "bar", "foobar", "foo"}; char *vals[NUMELE] = {"FOO", "BAR", "FOOBAR", "FOOZAM"}; /* Open the database (create) */ myDB = dbm_open("ndbmTest", O_RDWR | O_CREAT, 0660); if(myDB == NULL) { printf("ERROR: Could not open the DB file. Error number: %d.\n", errno); exit(1); } else { printf("DB created.\n"); printf("DB opened.\n"); } /* Put stuff in the DB... */ for(i=0; i<NUMELE; i++) { daKey.dptr = keys[i]; daKey.dsize = strlen(keys[i])+1; daVal.dptr = vals[i]; daVal.dsize = strlen(vals[i])+1; dbRet = dbm_store(myDB, daKey, daVal, DBM_INSERT); // DBM_REPLACE for replace behaviour switch(dbRet) { case 0: printf("Store: '%s' ==> '%s'\n", (char *)daKey.dptr, (char *)daVal.dptr); break; case 1: // Only can happen with DBM_INSERT printf("Could not store '%s' because it was already in the DB\n", (char *)daKey.dptr); break; default: printf("ERROR: Could not insert item (%s,%s). \n", (char *)daKey.dptr, (char *)daVal.dptr); exit(1); } /* end switch */ } /* end for */ /* Lookup the second one.. */ daKey.dptr = keys[1]; daKey.dsize = strlen(keys[1])+1; daVal = dbm_fetch(myDB, daKey); if(daVal.dptr == NULL) { printf("ERROR: Could not look up %s\n", (char *)daKey.dptr); } else { printf("Got record: '%s' ==> '%s'\n", (char *)daKey.dptr, (char *)daVal.dptr); } /* Delete an element */ daKey.dptr = keys[1]; daKey.dsize = strlen(keys[1])+1; if(dbm_delete(myDB, daKey) < 0) { printf("ERROR: Could not delete item with key %s\n", daKey.dptr); } else { printf("Deleted: '%s'\n", (char *)daKey.dptr); }/* end if */ /* Close the DB (flush everything to the file) */ dbm_close(myDB); printf("DB closed... Bye!\n"); return 0; } /* end func main */
void runFailure() { dbm_delete(NULL, anydatum()); }
int main() { struct test_data items_to_store[ITEMS_USED]; struct test_data item_retrieved; char key_to_use[20]; int i, result; // a datum is dbm's wrapper around binary data. It consists of a void* // to our data and a size_t giving the data size datum key_datum; datum data_datum; // a DBM struct is a wrapper around two files, one used to track indices // and another used to store the binary data. DBM *dbm_p; dbm_p = dbm_open(TEST_DB_FILE, O_RDWR | O_CREAT, 0666); if (!dbm_p) { fprintf(stderr, "Failed to open database\n"); exit(EXIT_FAILURE); } // set all data to zero first, then set some data // note that we *need* all zeros in some cases, e.g. if we use // char arrays as keys. If they are only used for values it's less // important. memset(items_to_store, '\0', sizeof(items_to_store)); // Ff47 strcpy(items_to_store[0].misc_chars, "First!"); items_to_store[0].any_integer = 47; strcpy(items_to_store[0].more_chars, "foo"); // bu13 strcpy(items_to_store[1].misc_chars, "bar"); items_to_store[1].any_integer = 13; strcpy(items_to_store[1].more_chars, "unlucky?"); // Tb3 strcpy(items_to_store[2].misc_chars, "Third"); items_to_store[2].any_integer = 3; strcpy(items_to_store[2].more_chars, "baz"); for (i = 0; i < ITEMS_USED; i++) { // make the key string sprintf(key_to_use, "%c%c%d", items_to_store[i].misc_chars[0], items_to_store[i].more_chars[0], items_to_store[i].any_integer); // make the key. Check that you understand why we *must* use // strlen() here and not sizeof() key_datum.dptr = (void *) key_to_use; key_datum.dsize = strlen(key_to_use); // make the data data_datum.dptr = (void *) &items_to_store[i]; data_datum.dsize = sizeof(struct test_data); // write the data. Note that we have a choice of what to do // if we see a known key; here it doesn't matter. result = dbm_store(dbm_p, key_datum, data_datum, DBM_REPLACE); if (result != 0) { fprintf(stderr, "Failed to write to database\n"); exit(2); } } // now try retrieving the second entry sprintf(key_to_use, "bu%d", 13); key_datum.dptr = (void *) key_to_use; key_datum.dsize = strlen(key_to_use); data_datum = dbm_fetch(dbm_p, key_datum); // if we don't find a key, we get a datum with NULL dptr if (data_datum.dptr) { printf("Data retrieved\n"); memcpy(&item_retrieved, data_datum.dptr, data_datum.dsize); printf("Retrieved item - %s %d %s\n", item_retrieved.misc_chars, item_retrieved.any_integer, item_retrieved.more_chars); } else { printf("No data found for key %s\n", key_to_use); } printf("\n\nNow we'll try deleting and then cycling\n\n"); // delete the entry we just looked at if (dbm_delete(dbm_p, key_datum) == 0) { printf("Data with key %s deleted\n", key_to_use); } else { printf("Nothing deleted for key %s\n", key_to_use); } // now use dbm_firstkey and dbm_nextkey to iterate over keys. // ... the order in which they appear is not defined for (key_datum = dbm_firstkey(dbm_p); key_datum.dptr; // the end condition is a NULL dptr key_datum = dbm_nextkey(dbm_p)) { // get the data. The data_datum shouldn't be null in this situation, // but it could happen due to race conditions and such, so we should // still guard against it. data_datum = dbm_fetch(dbm_p, key_datum); if (data_datum.dptr) { printf("Data retrieved for key %s\n", (char *)key_datum.dptr); memcpy(&item_retrieved, data_datum.dptr, data_datum.dsize); printf("Retrieved item - %s %d %s\n", item_retrieved.misc_chars, item_retrieved.any_integer, item_retrieved.more_chars); } else { // the authors oopsed here, don't use `key_to_use` printf("No data found for key %s\n", (char *) key_datum.dptr); } } // don't forget to close the database! dbm_close(dbm_p); exit(EXIT_SUCCESS); }
extern int c_dbm_delete (DBM *db, datum *key) { return dbm_delete(db, *key); }