int ndbm_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp) { DBM *db; db = dbm_open(map, O_RDONLY, 0); if (db) { struct stat stb; int error; #ifdef DBM_SUFFIX char dbfilename[256]; strcpy(dbfilename, map); strcat(dbfilename, DBM_SUFFIX); error = stat(dbfilename, &stb); #else /* not DBM_SUFFIX */ error = fstat(dbm_pagfno(db), &stb); #endif /* not DBM_SUFFIX */ if (!error && *tp < stb.st_mtime) { *tp = stb.st_mtime; error = -1; } else { error = search_ndbm(db, key, pval); } (void) dbm_close(db); return error; } return errno; }
int ndbm_init(mnt_map *m, char *map, time_t *tp) { DBM *db; db = dbm_open(map, O_RDONLY, 0); if (db) { struct stat stb; int error; #ifdef DBM_SUFFIX char dbfilename[256]; strcpy(dbfilename, map); strcat(dbfilename, DBM_SUFFIX); error = stat(dbfilename, &stb); #else /* not DBM_SUFFIX */ error = fstat(dbm_pagfno(db), &stb); #endif /* not DBM_SUFFIX */ if (error < 0) *tp = clocktime(); else *tp = stb.st_mtime; dbm_close(db); return 0; } return errno; }
static void dbm_dealloc(register dbmobject *dp) { if ( dp->di_dbm ) dbm_close(dp->di_dbm); PyObject_Del(dp); }
/* * Given a key, this function retrieves the text from the database * and checks the expire time on it. If it is still valid data, it * returns the number of bytes in text, else 0 or -1 on error. */ int cache_read(char *key, char **text) { #ifndef NOCACHE datum dbkey; datum dbstore; #ifdef HAVE_GDBM_OPEN GDBM_FILE dbf; #else DBM *dbf; #endif #endif time_t time_c; if (!cache) return 0; #ifndef NOCACHE dbkey.dptr = key; dbkey.dsize = strlen(key); dbf = dbm_open(cfname, DBM_ROPTIONS, DBM_MODE); if (!dbf) return -1; dbstore = dbm_fetch(dbf, dbkey); if ((dbstore.dptr == NULL)) { dbm_close(dbf); return 0; } memcpy(&time_c,dbstore.dptr,sizeof(time_c)); /* ensure suitable alignment */ if ((time(NULL)-time_c)/(60*60) > cfexpire) { dbm_close(dbf); return 0; } *text = malloc(dbstore.dsize); if (!*text) return -1; memcpy(*text, (char *)(dbstore.dptr)+sizeof(time_t), dbstore.dsize-sizeof(time_t)); dbm_close(dbf); return (dbstore.dsize-sizeof(time_t)); #else return 0; #endif /* !NOCACHE */ }
/* * save the user status in *accessdb * * return 0 -> ok * -1 -> failed to update accessdb */ int save_access_status (char *accessdb, char *loginname, int status, int delay,char *cookie) { char txt_nul[]="0"; DBM *db; datum key; datum data; time_t t; struct access_data access; if (cookie==NULL) { cookie=txt_nul; } db=dbm_open(accessdb, O_RDWR | O_CREAT, 0600); if (!db) return(-1); /* failed to create db */ key.dptr=(void *)loginname; key.dsize=strlen(loginname); time(&t); access.status=1; if (status) { data=dbm_fetch(db,key); if (data.dptr) { if(sizeof(access)!=data.dsize) { dbm_close(db); return(-1); } memcpy(&access,data.dptr,data.dsize); if (access.status && (t-access.ti>delay)) { access.status=0; } ++access.status; } } else access.status=0; access.ti=t; memset(access.cookie,'\0',sizeof(access.cookie)); strncpy(access.cookie,cookie,59); data.dptr=(void *)&access; data.dsize=sizeof(access); if (dbm_store(db,key,data,DBM_REPLACE)!=0) { dbm_close(db); return(-1); /* failed to store user data */ } dbm_close(db); return(0); }
int main() { DBM* dbm = 0; dbm_close(dbm); return 0; }
static void free_dbm(struct dbmdata *dbmp) { if (dbmp) { if (dbmp->di_dbm) dbm_close(dbmp->di_dbm); xfree(dbmp); } }
static PyObject * _dbm_dbm_close_impl(dbmobject *self) /*[clinic end generated code: output=c8dc5b6709600b86 input=046db72377d51be8]*/ { if (self->di_dbm) dbm_close(self->di_dbm); self->di_dbm = NULL; Py_RETURN_NONE; }
int main() { const char *database_name = "/tmp/ndbm" ; //create a dbm DBM *dbmptr = 0 ; TFUNVR(0, dbmptr, dbm_open(database_name, O_RDWR | O_CREAT, 0666)) //append some item to dbm from user const int DATA_MAX_LEN = 101 ; datum key_datum, data_datum ; int key, result ; char data[DATA_MAX_LEN] ; printf("Enter a database item, form is 'key data', when key is -1 to end.\n") ; scanf("%d", &key) ; while(-1 != key) { key_datum.dptr = (void *)&key ; key_datum.dsize = sizeof(key) ; fgets(data, DATA_MAX_LEN , stdin) ; data_datum.dptr = data ; data_datum.dsize = strlen(data) ; do{ if(1 == (result = dbm_store(dbmptr, key_datum, data_datum, DBM_INSERT))) { fprintf(stderr, "dbm_store has been a same key\n") ; break ; } else if(0 > result) { fprintf(stderr, "dbm_store failed\n") ; exit(1) ; } }while(0) ; //use for break can scanf scanf("%d", &key) ; } //read data frm dbm printf("Now you can search itme, enter a key, when key is -1 to end\n") ; scanf("%d", &key) ; while(-1 != key) { key_datum.dptr = (void *)&key ; key_datum.dsize = sizeof(key) ; data_datum = dbm_fetch(dbmptr, key_datum) ; if(NULL == data_datum.dptr) { printf("No has this key item, try again:") ; } else { printf("key:%-4d:%s\n", key, (char *)data_datum.dptr) ; } scanf("%d", &key) ; } //close dbm dbm_close(dbmptr) ; return 0 ; }
static PyObject * dbm__close(register dbmobject *dp, PyObject *unused) { if (dp->di_dbm) dbm_close(dp->di_dbm); dp->di_dbm = NULL; Py_INCREF(Py_None); return Py_None; }
static void free_dbm(void *ptr) { struct dbmdata *dbmp = ptr; if (dbmp) { if (dbmp->di_dbm) dbm_close(dbmp->di_dbm); xfree(dbmp); } }
static krb5_error_code NDBM_close(krb5_context context, HDB *db) { struct ndbm_db *d = db->hdb_db; dbm_close(d->db); close(d->lock_fd); free(d); return 0; }
static int _ndbm_close (mu_dbm_file_t db) { if (db->db_descr) { dbm_close ((DBM *) db->db_descr); db->db_descr = NULL; } return 0; }
static PyObject * _dbm_dbm_close_impl(dbmobject *self) /*[clinic end generated code: output=c8dc5b6709600b86 input=046db72377d51be8]*/ { if (self->di_dbm) dbm_close(self->di_dbm); self->di_dbm = NULL; Py_INCREF(Py_None); return Py_None; }
static PyObject * dbm__close(register dbmobject *dp, PyObject *args) { if (!PyArg_ParseTuple(args, ":close")) return NULL; if (dp->di_dbm) dbm_close(dp->di_dbm); dp->di_dbm = NULL; Py_INCREF(Py_None); return Py_None; }
/* * call-seq: * dbm.close * * Closes the database. */ static VALUE fdbm_close(VALUE obj) { struct dbmdata *dbmp; GetDBM(obj, dbmp); dbm_close(dbmp->di_dbm); dbmp->di_dbm = 0; return Qnil; }
void pdbm_close(PDBM &pdbm, cindex_t dim) { assert(dim); if (pdbm) { pdbm_prepare(pdbm, dim); dbm_close(pdbm_matrix(pdbm), dim); } assertx(pdbm_isValid(pdbm, dim)); }
endpwent() { if (pwf != NULL) { fclose(pwf); pwf = NULL; } if (_pw_db != (DBM *)0) { dbm_close(_pw_db); _pw_db = (DBM *)0; _pw_stayopen = 0; } }
static krb5_error_code NDBM_open(krb5_context context, HDB *db, int flags, mode_t mode) { krb5_error_code ret; struct ndbm_db *d = malloc(sizeof(*d)); char *lock_file; if(d == NULL) { krb5_set_error_message(context, ENOMEM, "malloc: out of memory"); return ENOMEM; } asprintf(&lock_file, "%s.lock", (char*)db->hdb_name); if(lock_file == NULL) { free(d); krb5_set_error_message(context, ENOMEM, "malloc: out of memory"); return ENOMEM; } d->db = dbm_open((char*)db->hdb_name, flags, mode); if(d->db == NULL){ ret = errno; free(d); free(lock_file); krb5_set_error_message(context, ret, "dbm_open(%s): %s", db->hdb_name, strerror(ret)); return ret; } d->lock_fd = open(lock_file, O_RDWR | O_CREAT, 0600); if(d->lock_fd < 0){ ret = errno; dbm_close(d->db); free(d); krb5_set_error_message(context, ret, "open(%s): %s", lock_file, strerror(ret)); free(lock_file); return ret; } free(lock_file); db->hdb_db = d; if((flags & O_ACCMODE) == O_RDONLY) ret = hdb_check_db_format(context, db); else ret = hdb_init_db(context, db); if(ret == HDB_ERR_NOENTRY) return 0; if (ret) { NDBM_close(context, db); krb5_set_error_message(context, ret, "hdb_open: failed %s database %s", (flags & O_ACCMODE) == O_RDONLY ? "checking format of" : "initialize", db->hdb_name); } return ret; }
int main (int argc, char *argv[]) { FILE *std_fp; DBM *db; datum dtKey, dtRec; char rec[MAX_STRING_LEN]; char key[MAX_STRING_LEN]; char *pch1, *pch2; int ndx; if (argc != 3) usage(argv[0]); if (!(std_fp = fopen(argv[2], "w"))) { fprintf(stderr,"%s: Could not open file %s for writing.\n", argv[0],argv[2]); perror("fopen"); exit(1); } if (!(db = dbm_open (argv[1], O_RDONLY, 0))) { fprintf(stderr,"%s: Could not open database file %s.\n", argv[0], argv[1]); exit(1); } for (dtKey = dbm_firstkey(db); dtKey.dptr; dtKey = dbm_nextkey(db)) { dtRec = dbm_fetch (db, dtKey); pch1 = key; for (ndx = 0; ndx < dtKey.dsize; ndx++) *pch1++ = dtKey.dptr[ndx]; *pch1 = '\0'; pch2 = rec; for (ndx = 0; ndx < dtRec.dsize; ndx++) *pch2++ = dtRec.dptr[ndx]; *pch2 = '\0'; printf ("Storing data <%s> with key <%s>\n", rec, key); if (strchr (rec, ' ')) fprintf (std_fp,"%s: %s\n", key, rec); else fprintf (std_fp,"%s:%s\n", key, rec); } dbm_close(db); exit(0); }
/* * returns the status of a user * 0 -> ok * !0 -> have to wait return value (s) to wait * -1 -> failed */ int get_access_status (char *accessdb, char *loginname, int max_invalid, int delay) { DBM *db; datum data; datum key; time_t t; struct access_data access; db=dbm_open(accessdb, O_RDONLY, 0666); if(!db) return -1; key.dptr=(void *)loginname; key.dsize=strlen(loginname); data=dbm_fetch(db,key); if(!data.dptr) { dbm_close(db); return(0); } if(sizeof(access)!=data.dsize) { dbm_close(db);return(-1); } memcpy(&access,data.dptr,data.dsize); dbm_close(db); if (access.status==0) return 0; if (access.status<max_invalid) return 0; time(&t); if(t-access.ti>delay) return 0; return(delay-(t-access.ti)); }
static void dumprgb (char *filename) { #ifdef NDBM DBM *rgb_dbm; #else int rgb_dbm; #endif datum key; rgb_dbm = dbm_open (filename, O_RDONLY, 0); if (!rgb_dbm) { fprintf (stderr, "%s: unable to open rgb database \"%s\"\n", ProgramName, filename); exit (1); } #ifndef NDBM #define dbm_nextkey(db) (nextkey(key)) /* need variable called key */ #endif for (key = dbm_firstkey(rgb_dbm); key.dptr != NULL; key = dbm_nextkey(rgb_dbm)) { datum value; value = dbm_fetch(rgb_dbm, key); if (value.dptr) { RGB rgb; unsigned short r, g, b; memcpy( (char *)&rgb, value.dptr, sizeof rgb); #define N(x) (((x) >> 8) & 0xff) r = N(rgb.red); g = N(rgb.green); b = N(rgb.blue); #undef N printf ("%3u %3u %3u\t\t", r, g, b); fwrite (key.dptr, 1, key.dsize, stdout); putchar ('\n'); } else { fprintf (stderr, "%s: no value found for key \"", ProgramName); fwrite (key.dptr, 1, key.dsize, stderr); fprintf (stderr, "\"\n"); } } dbm_close (rgb_dbm); }
/* - copy the original DBM * - close the copy * - print the difference */ std::ostream& dbm_cppPrintCloseDiff(std::ostream& out, const raw_t *dbm, cindex_t dim) { raw_t *copy = new raw_t[dim*dim]; assert(dbm && dim); dbm_copy(copy, dbm, dim); dbm_close(copy, dim); if (dbm_isEmpty(copy, dim)) { out << RED(BOLD) "Warning: empty DBM!" NORMAL " "; } dbm_cppPrintDiff(out, dbm, copy, dim); delete [] copy; return out; }
/* * This stores the passed text in the database with the key `key'. * Returns 0 on success and -1 on failure. */ int cache_store(char *key, const char *text) { #ifndef NOCACHE datum dbkey; datum dbstore; int ret; #ifdef HAVE_GDBM_OPEN GDBM_FILE dbf; #else DBM *dbf; #endif time_t *timeptr; char *ptr; if (cache) { dbkey.dptr = key; dbkey.dsize = strlen(key); ptr = malloc(strlen(text)+sizeof(time_t)+1); if (!ptr) return -1; memcpy(ptr+sizeof(time_t), text, strlen(text)+1); timeptr = (time_t *)ptr; *timeptr = time(NULL); dbstore.dptr = ptr; dbstore.dsize = strlen(text)+sizeof(time_t)+1; dbf = dbm_open(cfname, DBM_WOPTIONS, DBM_MODE); if (!dbf) return -1; else { ret = dbm_store(dbf, dbkey, dbstore, DBM_IOPTIONS); if (ret < 0) return -1; dbm_close(dbf); } } #endif return 0; }
sasldb_handle _sasldb_getkeyhandle(const sasl_utils_t *utils, sasl_conn_t *conn) { const char *path = SASL_DB_PATH; sasl_getopt_t *getopt; void *cntxt; DBM *db; handle_t *handle; if(!utils || !conn) return NULL; if(!db_ok) { utils->seterror(conn, 0, "Database not OK in _sasldb_getkeyhandle"); return NULL; } 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_RDONLY, S_IRUSR | S_IWUSR); if(!db) { utils->seterror(conn, 0, "Could not open db"); return NULL; } handle = utils->malloc(sizeof(handle_t)); if(!handle) { utils->seterror(conn, 0, "no memory in _sasldb_getkeyhandle"); dbm_close(db); return NULL; } handle->db = db; handle->first = 1; return (sasldb_handle)handle; }
void _pw_null_cleanup(void *junkola) { pwf_context_t *x = (pwf_context_t *)junkola; if (x) { if (x->pwf) { fclose(x->pwf); x->pwf = 0; } #ifdef DBM_PWD_SUPPORT if (x->pw_db) { dbm_close(x->pw_db); x->pw_db = 0; } #endif /* DBM_PWD_SUPPORT */ free((void *)x); } }
int ndbm_init(char *map, time_t *tp) { DBM *db; db = dbm_open(map, O_RDONLY, 0); if (db) { struct stat stb; if (fstat(dbm_pagfno(db), &stb) < 0) *tp = clocktime(); else *tp = stb.st_mtime; dbm_close(db); return 0; } return errno; }
int listusers(const char *path, listcb_t *cb) { DBM *indb; datum dkey, nextkey, dvalue; indb = dbm_open(path, O_RDONLY, S_IRUSR | S_IWUSR); if (!indb) { fprintf(stderr, "can't open %s\n", path); return 1; } dkey = dbm_firstkey(indb); while (dkey.dptr != NULL) { char *authid = dkey.dptr; char *realm = dkey.dptr+strlen(authid)+1; char *tmp = realm + strlen(realm)+1; char mech[1024]; int len = dkey.dsize - (tmp - ((char *)dkey.dptr)); if (len >= (int) sizeof mech) { fprintf(stderr, "malformed database entry\n"); break; } memcpy(mech, tmp, len); mech[dkey.dsize - (tmp - ((char *)dkey.dptr))] = '\0'; dvalue = dbm_fetch(indb, dkey); if (*authid && dvalue.dptr) { /* don't check return values */ cb(authid,realm,mech,dvalue.dptr,dvalue.dsize); } nextkey=dbm_nextkey(indb); dkey=nextkey; } dbm_close(indb); return 0; }
void endpwent() { pwf_context_t *_data; _data = _pw_get_data(); if (_data) { if (_data->pwf != NULL) { fclose(_data->pwf); _data->pwf = NULL; } #ifdef DBM_PWD_SUPPORT if (_data->pw_db != (DBM *)0) { dbm_close(_data->pw_db); _data->pw_db = (DBM *)0; _data->pw_stayopen = 0; } #endif /* DBM_PWD_SUPPORT */ } }
struct dba * dba_read(const char *fname) { struct dba *dba; struct dba_array *page; struct dbm_page *pdata; struct dbm_macro *mdata; const char *cp; int32_t im, ip, iv, npages; if (dbm_open(fname) == -1) return NULL; npages = dbm_page_count(); dba = dba_new(npages < 128 ? 128 : npages); for (ip = 0; ip < npages; ip++) { pdata = dbm_page_get(ip); page = dba_page_new(dba->pages, pdata->arch, pdata->desc, pdata->file + 1, *pdata->file); for (cp = pdata->name; *cp != '\0'; cp = strchr(cp, '\0') + 1) dba_page_add(page, DBP_NAME, cp); for (cp = pdata->sect; *cp != '\0'; cp = strchr(cp, '\0') + 1) dba_page_add(page, DBP_SECT, cp); if ((cp = pdata->arch) != NULL) while (*(cp = strchr(cp, '\0') + 1) != '\0') dba_page_add(page, DBP_ARCH, cp); cp = pdata->file; while (*(cp = strchr(cp, '\0') + 1) != '\0') dba_page_add(page, DBP_FILE, cp); } for (im = 0; im < MACRO_MAX; im++) { for (iv = 0; iv < dbm_macro_count(im); iv++) { mdata = dbm_macro_get(im, iv); dba_macro_new(dba, im, mdata->value, mdata->pp); } } dbm_close(); return dba; }