My Project  debian-1:4.1.1-p2+ds-4build2
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 (DBM *db, datum key)
 
datum dbm_firstkey (DBM *db)
 
datum dbm_nextkey (DBM *db)
 
long dbm_forder (DBM *db, datum key)
 
int dbm_delete (DBM *db, datum key)
 
int dbm_store (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 97 of file ndbm.cc.

98 {
99  (void) si_close(db->dbm_dirf);
100  (void) si_close(db->dbm_pagf);
101  free((char *)db);
102 }

◆ dbm_delete()

int dbm_delete ( DBM db,
datum  key 
)

Definition at line 139 of file ndbm.cc.

140 {
141  int i;
142  // datum item;
143 
144  if (dbm_error(db))
145  return (-1);
146  if (dbm_rdonly(db))
147  {
148  errno = EPERM;
149  return (-1);
150  }
151  dbm_access(db, dcalchash(key));
152  if ((i = finddatum(db->dbm_pagbuf, key)) < 0)
153  return (-1);
154  if (!delitem(db->dbm_pagbuf, i))
155  goto err;
156  db->dbm_pagbno = db->dbm_blkno;
157  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
158  if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
159  {
160  err:
161  db->dbm_flags |= _DBM_IOERR;
162  return (-1);
163  }
164  return (0);
165 }

◆ dbm_fetch()

datum dbm_fetch ( DBM db,
datum  key 
)

Definition at line 119 of file ndbm.cc.

120 {
121  int i;
122  datum item;
123 
124  if (dbm_error(db))
125  goto err;
126  dbm_access(db, dcalchash(key));
127  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
128  {
129  item = makdatum(db->dbm_pagbuf, i+1);
130  if (item.dptr != NULL)
131  return (item);
132  }
133 err:
134  item.dptr = NULL;
135  item.dsize = 0;
136  return (item);
137 }

◆ dbm_firstkey()

datum dbm_firstkey ( DBM db)

Definition at line 252 of file ndbm.cc.

253 {
254 
255  db->dbm_blkptr = 0L;
256  db->dbm_keyptr = 0;
257  return (dbm_nextkey(db));
258 }

◆ dbm_forder()

long dbm_forder ( DBM db,
datum  key 
)

Definition at line 104 of file ndbm.cc.

105 {
106  long hash;
107 
108  hash = dcalchash(key);
109  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1)
110  {
111  db->dbm_blkno = hash & db->dbm_hmask;
112  db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
113  if (getbit(db) == 0)
114  break;
115  }
116  return (db->dbm_blkno);
117 }

◆ dbm_nextkey()

datum dbm_nextkey ( DBM db)

Definition at line 260 of file ndbm.cc.

261 {
262  struct stat statb;
263  datum item;
264 
265  if (dbm_error(db)
266  || singular_fstat(db->dbm_pagf, &statb) < 0
267  )
268  goto err;
269  statb.st_size /= PBLKSIZ;
270  for (;;)
271  {
272  if (db->dbm_blkptr != db->dbm_pagbno)
273  {
274  db->dbm_pagbno = db->dbm_blkptr;
275  (void) lseek(db->dbm_pagf, db->dbm_blkptr*PBLKSIZ, SEEK_SET);
276  if (si_read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
277  memset(db->dbm_pagbuf, 0, PBLKSIZ);
278 #ifdef DEBUG
279  else if (chkblk(db->dbm_pagbuf) < 0)
280  db->dbm_flags |= _DBM_IOERR;
281 #endif
282  }
283  if (((short *)db->dbm_pagbuf)[0] != 0)
284  {
285  item = makdatum(db->dbm_pagbuf, db->dbm_keyptr);
286  if (item.dptr != NULL)
287  {
288  db->dbm_keyptr += 2;
289  return (item);
290  }
291  db->dbm_keyptr = 0;
292  }
293  if (++db->dbm_blkptr >= statb.st_size)
294  break;
295  }
296 err:
297  item.dptr = NULL;
298  item.dsize = 0;
299  return (item);
300 }

◆ dbm_open()

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

Definition at line 59 of file ndbm.cc.

60 {
61  struct stat statb;
62  DBM *db;
63 
64  if ((db = (DBM *)malloc(sizeof *db)) == 0)
65  {
66  errno = ENOMEM;
67  return ((DBM *)0);
68  }
69 #ifdef MSDOS
70  // default mode of open is ascii, we need binary mode.
71  flags |= O_BINARY;
72 #endif
73  db->dbm_flags = (flags & 03) == O_RDONLY ? _DBM_RDONLY : 0;
74  if ((flags & 03) == O_WRONLY)
75  flags = (flags & ~03) | O_RDWR;
76  strcpy(db->dbm_pagbuf, file);
77  strcat(db->dbm_pagbuf, ".pag");
78  db->dbm_pagf = si_open(db->dbm_pagbuf, flags, mode);
79  if (db->dbm_pagf < 0)
80  goto bad;
81  strcpy(db->dbm_pagbuf, file);
82  strcat(db->dbm_pagbuf, ".dir");
83  db->dbm_dirf = si_open(db->dbm_pagbuf, flags, mode);
84  if (db->dbm_dirf < 0)
85  goto bad1;
86  singular_fstat(db->dbm_dirf, &statb);
87  db->dbm_maxbno = statb.st_size*BYTESIZ-1;
88  db->dbm_pagbno = db->dbm_dirbno = -1;
89  return (db);
90 bad1:
91  (void) si_close(db->dbm_pagf);
92 bad:
93  free((char *)db);
94  return ((DBM *)0);
95 }

◆ dbm_store()

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

Definition at line 167 of file ndbm.cc.

168 {
169  int i;
170  int ret;
171  datum item, item1;
172  char ovfbuf[PBLKSIZ];
173 
174  if (dbm_error(db))
175  return (-1);
176  if (dbm_rdonly(db))
177  {
178  errno = EPERM;
179  return (-1);
180  }
181 
182 _loop:
183  dbm_access(db, dcalchash(key));
184  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
185  {
186  if (!replace)
187  return (1);
188  if (!delitem(db->dbm_pagbuf, i))
189  {
190  db->dbm_flags |= _DBM_IOERR;
191  return (-1);
192  }
193  }
194  if (!additem(db->dbm_pagbuf, key, dat))
195  goto split;
196  db->dbm_pagbno = db->dbm_blkno;
197  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
198  if ( (ret=si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ)) != PBLKSIZ)
199  {
200  db->dbm_flags |= _DBM_IOERR;
201  return (-1);
202  }
203  return (0);
204 
205 split:
206  if (key.dsize+dat.dsize+3*sizeof(short) >= PBLKSIZ)
207  {
208  db->dbm_flags |= _DBM_IOERR;
209  errno = ENOSPC;
210  return (-1);
211  }
212  memset(ovfbuf, 0, PBLKSIZ);
213  for (i=0;;) {
214  item = makdatum(db->dbm_pagbuf, i);
215  if (item.dptr == NULL)
216  break;
217  if (dcalchash(item) & (db->dbm_hmask+1))
218  {
219  item1 = makdatum(db->dbm_pagbuf, i+1);
220  if (item1.dptr == NULL) {
221  fprintf(stderr, "ndbm: split not paired\n");
222  db->dbm_flags |= _DBM_IOERR;
223  break;
224  }
225  if (!additem(ovfbuf, item, item1) ||
226  !delitem(db->dbm_pagbuf, i))
227  {
228  db->dbm_flags |= _DBM_IOERR;
229  return (-1);
230  }
231  continue;
232  }
233  i += 2;
234  }
235  db->dbm_pagbno = db->dbm_blkno;
236  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
237  if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
238  {
239  db->dbm_flags |= _DBM_IOERR;
240  return (-1);
241  }
242  (void) lseek(db->dbm_pagf, (db->dbm_blkno+db->dbm_hmask+1)*PBLKSIZ, SEEK_SET);
243  if (si_write(db->dbm_pagf, ovfbuf, PBLKSIZ) != PBLKSIZ)
244  {
245  db->dbm_flags |= _DBM_IOERR;
246  return (-1);
247  }
248  setbit(db);
249  goto _loop;
250 }
delitem
static int delitem(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:465
DBM::dbm_hmask
long dbm_hmask
Definition: ndbm.h:60
SEEK_SET
#define SEEK_SET
Definition: mod2.h:116
additem
static int additem(char buf[PBLKSIZ], datum item, datum item1)
Definition: ndbm.cc:497
DBM::dbm_blkptr
long dbm_blkptr
Definition: ndbm.h:61
DBM
Definition: ndbm.h:54
singular_fstat
int singular_fstat(int fd, struct stat *buf)
Definition: misc_ip.cc:1084
dbm_nextkey
datum dbm_nextkey(DBM *db)
Definition: ndbm.cc:260
dbm_error
#define dbm_error(db)
Definition: ndbm.h:75
DBM::dbm_dirf
int dbm_dirf
Definition: ndbm.h:55
DBM::dbm_bitno
long dbm_bitno
Definition: ndbm.h:59
bad
bool bad
Definition: facFactorize.cc:65
flags
int status int void size_t count int const void size_t count const char int flags
Definition: si_signals.h:73
DBM::dbm_keyptr
int dbm_keyptr
Definition: ndbm.h:62
dbm_rdonly
#define dbm_rdonly(db)
Definition: ndbm.h:73
datum
Definition: ndbm.h:83
dcalchash
static long dcalchash(datum item)
Definition: ndbm.cc:440
getbit
static int getbit(DBM *db)
Definition: ndbm.cc:324
i
int i
Definition: cfEzgcd.cc:125
setbit
static void setbit(DBM *db)
Definition: ndbm.cc:346
DBM::dbm_flags
int dbm_flags
Definition: ndbm.h:57
_DBM_RDONLY
#define _DBM_RDONLY
Definition: ndbm.h:70
PBLKSIZ
#define PBLKSIZ
Definition: ndbm.h:50
malloc
void * malloc(size_t size)
Definition: omalloc.c:92
DBM::dbm_pagf
int dbm_pagf
Definition: ndbm.h:56
BYTESIZ
#define BYTESIZ
Definition: ndbm.cc:46
free
#define free
Definition: omAllocFunc.c:12
_DBM_IOERR
#define _DBM_IOERR
Definition: ndbm.h:71
DBM::dbm_pagbno
long dbm_pagbno
Definition: ndbm.h:64
split
static CFList split(const CanonicalForm &F, const int m, const Variable &x)
Definition: facMul.cc:3336
datum::dptr
char * dptr
Definition: ndbm.h:84
si_open
#define si_open(...)
finddatum
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:392
NULL
#define NULL
Definition: omList.c:10
dbm_access
static void dbm_access(DBM *db, long hash)
Definition: ndbm.cc:302
DBM::dbm_blkno
long dbm_blkno
Definition: ndbm.h:63
makdatum
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:371
datum::dsize
int dsize
Definition: ndbm.h:85
DBM::dbm_pagbuf
char dbm_pagbuf[PBLKSIZ]
Definition: ndbm.h:65