Data Structures | Macros | Functions
ndbm.h File Reference

Go to the source code of this file.

Data Structures

struct  DBM
 
struct  datum
 

Macros

#define PBLKSIZ   1024
 
#define DBLKSIZ   4096
 
#define _DBM_RDONLY   0x01 /* data base open read-only */
 
#define _DBM_IOERR   0x02 /* data base I/O error */
 
#define dbm_rdonly(db)   ((db)->dbm_flags & _DBM_RDONLY)
 
#define dbm_error(db)   ((db)->dbm_flags & _DBM_IOERR)
 
#define dbm_clearerr(db)   ((db)->dbm_flags &= ~_DBM_IOERR)
 
#define dbm_dirfno(db)   ((db)->dbm_dirf)
 
#define dbm_pagfno(db)   ((db)->dbm_pagf)
 
#define DBM_INSERT   0
 
#define DBM_REPLACE   1
 

Functions

DBMdbm_open (char *file, int flags, int mode)
 
void dbm_close (DBM *db)
 
datum dbm_fetch (register DBM *db, datum key)
 
datum dbm_firstkey (DBM *db)
 
datum dbm_nextkey (register DBM *db)
 
long dbm_forder (register DBM *db, datum key)
 
int dbm_delete (register DBM *db, datum key)
 
int dbm_store (register DBM *db, datum key, datum dat, int replace)
 

Data Structure Documentation

◆ DBM

struct DBM

Definition at line 54 of file ndbm.h.

Data Fields
long dbm_bitno
long dbm_blkno
long dbm_blkptr
long dbm_dirbno
char dbm_dirbuf[DBLKSIZ]
int dbm_dirf
int dbm_flags
long dbm_hmask
int dbm_keyptr
long dbm_maxbno
long dbm_pagbno
char dbm_pagbuf[PBLKSIZ]
int dbm_pagf

◆ datum

struct datum

Definition at line 83 of file ndbm.h.

Data Fields
char * dptr
int dsize

Macro Definition Documentation

◆ _DBM_IOERR

#define _DBM_IOERR   0x02 /* data base I/O error */

Definition at line 71 of file ndbm.h.

◆ _DBM_RDONLY

#define _DBM_RDONLY   0x01 /* data base open read-only */

Definition at line 70 of file ndbm.h.

◆ DBLKSIZ

#define DBLKSIZ   4096

Definition at line 52 of file ndbm.h.

◆ dbm_clearerr

#define dbm_clearerr (   db)    ((db)->dbm_flags &= ~_DBM_IOERR)

Definition at line 77 of file ndbm.h.

◆ dbm_dirfno

#define dbm_dirfno (   db)    ((db)->dbm_dirf)

Definition at line 80 of file ndbm.h.

◆ dbm_error

#define dbm_error (   db)    ((db)->dbm_flags & _DBM_IOERR)

Definition at line 75 of file ndbm.h.

◆ DBM_INSERT

#define DBM_INSERT   0

Definition at line 91 of file ndbm.h.

◆ dbm_pagfno

#define dbm_pagfno (   db)    ((db)->dbm_pagf)

Definition at line 81 of file ndbm.h.

◆ dbm_rdonly

#define dbm_rdonly (   db)    ((db)->dbm_flags & _DBM_RDONLY)

Definition at line 73 of file ndbm.h.

◆ DBM_REPLACE

#define DBM_REPLACE   1

Definition at line 92 of file ndbm.h.

◆ PBLKSIZ

#define PBLKSIZ   1024

Definition at line 50 of file ndbm.h.

Function Documentation

◆ dbm_close()

void dbm_close ( DBM db)

Definition at line 110 of file ndbm.cc.

111 {
112  (void) si_close(db->dbm_dirf);
113  (void) si_close(db->dbm_pagf);
114  free((char *)db);
115 }
int dbm_pagf
Definition: ndbm.h:56
#define free
Definition: omAllocFunc.c:12
int dbm_dirf
Definition: ndbm.h:55

◆ dbm_delete()

int dbm_delete ( register DBM db,
datum  key 
)

Definition at line 152 of file ndbm.cc.

153 {
154  register int i;
155  // datum item;
156 
157  if (dbm_error(db))
158  return (-1);
159  if (dbm_rdonly(db))
160  {
161  errno = EPERM;
162  return (-1);
163  }
164  dbm_access(db, dcalchash(key));
165  if ((i = finddatum(db->dbm_pagbuf, key)) < 0)
166  return (-1);
167  if (!delitem(db->dbm_pagbuf, i))
168  goto err;
169  db->dbm_pagbno = db->dbm_blkno;
170  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
171  if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
172  {
173  err:
174  db->dbm_flags |= _DBM_IOERR;
175  return (-1);
176  }
177  return (0);
178 }
static long dcalchash(datum item)
Definition: ndbm.cc:453
static void dbm_access(register DBM *db, long hash)
Definition: ndbm.cc:315
#define dbm_rdonly(db)
Definition: ndbm.h:73
static int delitem(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:478
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:405
int i
Definition: cfEzgcd.cc:123
#define _DBM_IOERR
Definition: ndbm.h:71
#define PBLKSIZ
Definition: ndbm.h:50
#define dbm_error(db)
Definition: ndbm.h:75

◆ dbm_fetch()

datum dbm_fetch ( register DBM db,
datum  key 
)

Definition at line 132 of file ndbm.cc.

133 {
134  register int i;
135  datum item;
136 
137  if (dbm_error(db))
138  goto err;
139  dbm_access(db, dcalchash(key));
140  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
141  {
142  item = makdatum(db->dbm_pagbuf, i+1);
143  if (item.dptr != NULL)
144  return (item);
145  }
146 err:
147  item.dptr = NULL;
148  item.dsize = 0;
149  return (item);
150 }
static long dcalchash(datum item)
Definition: ndbm.cc:453
static void dbm_access(register DBM *db, long hash)
Definition: ndbm.cc:315
char * dptr
Definition: ndbm.h:84
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:384
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:405
int dsize
Definition: ndbm.h:85
int i
Definition: cfEzgcd.cc:123
#define NULL
Definition: omList.c:10
Definition: ndbm.h:83
#define dbm_error(db)
Definition: ndbm.h:75

◆ dbm_firstkey()

datum dbm_firstkey ( DBM db)

Definition at line 265 of file ndbm.cc.

266 {
267 
268  db->dbm_blkptr = 0L;
269  db->dbm_keyptr = 0;
270  return (dbm_nextkey(db));
271 }
int dbm_keyptr
Definition: ndbm.h:62
datum dbm_nextkey(register DBM *db)
Definition: ndbm.cc:273
long dbm_blkptr
Definition: ndbm.h:61

◆ dbm_forder()

long dbm_forder ( register DBM db,
datum  key 
)

Definition at line 117 of file ndbm.cc.

118 {
119  long hash;
120 
121  hash = dcalchash(key);
122  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1)
123  {
124  db->dbm_blkno = hash & db->dbm_hmask;
125  db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
126  if (getbit(db) == 0)
127  break;
128  }
129  return (db->dbm_blkno);
130 }
static long dcalchash(datum item)
Definition: ndbm.cc:453
static int getbit(register DBM *db)
Definition: ndbm.cc:337

◆ dbm_nextkey()

datum dbm_nextkey ( register DBM db)

Definition at line 273 of file ndbm.cc.

274 {
275  struct stat statb;
276  datum item;
277 
278  if (dbm_error(db)
279  || singular_fstat(db->dbm_pagf, &statb) < 0
280  )
281  goto err;
282  statb.st_size /= PBLKSIZ;
283  for (;;)
284  {
285  if (db->dbm_blkptr != db->dbm_pagbno)
286  {
287  db->dbm_pagbno = db->dbm_blkptr;
288  (void) lseek(db->dbm_pagf, db->dbm_blkptr*PBLKSIZ, L_SET);
289  if (si_read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
290  memset(db->dbm_pagbuf, 0, PBLKSIZ);
291 #ifdef DEBUG
292  else if (chkblk(db->dbm_pagbuf) < 0)
293  db->dbm_flags |= _DBM_IOERR;
294 #endif
295  }
296  if (db->dbm_pagbuf[0] != 0 || db->dbm_pagbuf[1] != 0)
297  {
298  item = makdatum(db->dbm_pagbuf, db->dbm_keyptr);
299  if (item.dptr != NULL)
300  {
301  db->dbm_keyptr += 2;
302  return (item);
303  }
304  db->dbm_keyptr = 0;
305  }
306  if (++db->dbm_blkptr >= statb.st_size)
307  break;
308  }
309 err:
310  item.dptr = NULL;
311  item.dsize = 0;
312  return (item);
313 }
int singular_fstat(int fd, struct stat *buf)
Definition: misc_ip.cc:1057
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:384
#define _DBM_IOERR
Definition: ndbm.h:71
#define NULL
Definition: omList.c:10
#define PBLKSIZ
Definition: ndbm.h:50
Definition: ndbm.h:83
#define dbm_error(db)
Definition: ndbm.h:75

◆ dbm_open()

DBM* dbm_open ( char *  file,
int  flags,
int  mode 
)

Definition at line 72 of file ndbm.cc.

73 {
74  struct stat statb;
75  register DBM *db;
76 
77  if ((db = (DBM *)malloc(sizeof *db)) == 0)
78  {
79  errno = ENOMEM;
80  return ((DBM *)0);
81  }
82 #ifdef MSDOS
83  // default mode of open is ascii, we need binary mode.
84  flags |= O_BINARY;
85 #endif
86  db->dbm_flags = (flags & 03) == O_RDONLY ? _DBM_RDONLY : 0;
87  if ((flags & 03) == O_WRONLY)
88  flags = (flags & ~03) | O_RDWR;
89  strcpy(db->dbm_pagbuf, file);
90  strcat(db->dbm_pagbuf, ".pag");
91  db->dbm_pagf = si_open(db->dbm_pagbuf, flags, mode);
92  if (db->dbm_pagf < 0)
93  goto bad;
94  strcpy(db->dbm_pagbuf, file);
95  strcat(db->dbm_pagbuf, ".dir");
96  db->dbm_dirf = si_open(db->dbm_pagbuf, flags, mode);
97  if (db->dbm_dirf < 0)
98  goto bad1;
99  singular_fstat(db->dbm_dirf, &statb);
100  db->dbm_maxbno = statb.st_size*BYTESIZ-1;
101  db->dbm_pagbno = db->dbm_dirbno = -1;
102  return (db);
103 bad1:
104  (void) si_close(db->dbm_pagf);
105 bad:
106  free((char *)db);
107  return ((DBM *)0);
108 }
#define si_open(...)
int singular_fstat(int fd, struct stat *buf)
Definition: misc_ip.cc:1057
#define _DBM_RDONLY
Definition: ndbm.h:70
bool bad
Definition: facFactorize.cc:65
Definition: ndbm.h:54
void * malloc(size_t size)
Definition: omalloc.c:92
#define BYTESIZ
Definition: ndbm.cc:58
#define free
Definition: omAllocFunc.c:12
int status int void size_t count int const void size_t count const char int flags
Definition: si_signals.h:73

◆ dbm_store()

int dbm_store ( register DBM db,
datum  key,
datum  dat,
int  replace 
)

Definition at line 180 of file ndbm.cc.

181 {
182  register int i;
183  int ret;
184  datum item, item1;
185  char ovfbuf[PBLKSIZ];
186 
187  if (dbm_error(db))
188  return (-1);
189  if (dbm_rdonly(db))
190  {
191  errno = EPERM;
192  return (-1);
193  }
194 
195 _loop:
196  dbm_access(db, dcalchash(key));
197  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
198  {
199  if (!replace)
200  return (1);
201  if (!delitem(db->dbm_pagbuf, i))
202  {
203  db->dbm_flags |= _DBM_IOERR;
204  return (-1);
205  }
206  }
207  if (!additem(db->dbm_pagbuf, key, dat))
208  goto split;
209  db->dbm_pagbno = db->dbm_blkno;
210  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
211  if ( (ret=si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ)) != PBLKSIZ)
212  {
213  db->dbm_flags |= _DBM_IOERR;
214  return (-1);
215  }
216  return (0);
217 
218 split:
219  if (key.dsize+dat.dsize+3*sizeof(short) >= PBLKSIZ)
220  {
221  db->dbm_flags |= _DBM_IOERR;
222  errno = ENOSPC;
223  return (-1);
224  }
225  memset(ovfbuf, 0, PBLKSIZ);
226  for (i=0;;) {
227  item = makdatum(db->dbm_pagbuf, i);
228  if (item.dptr == NULL)
229  break;
230  if (dcalchash(item) & (db->dbm_hmask+1))
231  {
232  item1 = makdatum(db->dbm_pagbuf, i+1);
233  if (item1.dptr == NULL) {
234  fprintf(stderr, "ndbm: split not paired\n");
235  db->dbm_flags |= _DBM_IOERR;
236  break;
237  }
238  if (!additem(ovfbuf, item, item1) ||
239  !delitem(db->dbm_pagbuf, i))
240  {
241  db->dbm_flags |= _DBM_IOERR;
242  return (-1);
243  }
244  continue;
245  }
246  i += 2;
247  }
248  db->dbm_pagbno = db->dbm_blkno;
249  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
250  if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
251  {
252  db->dbm_flags |= _DBM_IOERR;
253  return (-1);
254  }
255  (void) lseek(db->dbm_pagf, (db->dbm_blkno+db->dbm_hmask+1)*PBLKSIZ, L_SET);
256  if (si_write(db->dbm_pagf, ovfbuf, PBLKSIZ) != PBLKSIZ)
257  {
258  db->dbm_flags |= _DBM_IOERR;
259  return (-1);
260  }
261  setbit(db);
262  goto _loop;
263 }
static void setbit(register DBM *db)
Definition: ndbm.cc:359
static long dcalchash(datum item)
Definition: ndbm.cc:453
static int additem(char buf[PBLKSIZ], datum item, datum item1)
Definition: ndbm.cc:510
static void dbm_access(register DBM *db, long hash)
Definition: ndbm.cc:315
char * dptr
Definition: ndbm.h:84
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:384
#define dbm_rdonly(db)
Definition: ndbm.h:73
static int delitem(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:478
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:405
int dsize
Definition: ndbm.h:85
int i
Definition: cfEzgcd.cc:123
static CFList split(const CanonicalForm &F, const int m, const Variable &x)
Definition: facMul.cc:3336
#define _DBM_IOERR
Definition: ndbm.h:71
#define NULL
Definition: omList.c:10
#define PBLKSIZ
Definition: ndbm.h:50
Definition: ndbm.h:83
#define dbm_error(db)
Definition: ndbm.h:75