source: git/Singular/ndbm.cc @ 74fced0

spielwiese
Last change on this file since 74fced0 was 74fced0, checked in by Kai Krüger <krueger@…>, 27 years ago
* ndbm.cc: fix open-bug for dos default mode of open is ascii, we need binary mode. git-svn-id: file:///usr/local/Singular/svn/trunk@629 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5//**************************************************************************/
6//
7// $Id: ndbm.cc,v 1.4 1997-08-15 09:49:13 krueger Exp $
8//
9//**************************************************************************/
10// 'ndbm.cc' containes all low-level functions to manipulate dbm-files
11// for the original Copyright of this file and 'ndbm.h' see below .
12//
13// some minor change where needed to compile and run under MacOS/MPW
14//
15//**************************************************************************/
16
17#include "mod2.h"
18#ifdef HAVE_DBM
19/*
20 * Copyright (c) 1983 Regents of the University of California.
21 * All rights reserved.  The Berkeley software License Agreement
22 * specifies the terms and conditions for redistribution.
23 */
24
25#if defined(LIBC_SCCS) && !defined(lint)
26static char sccsid[] = "@(#)ndbm.c        5.3 (Berkeley) 3/9/86";
27#endif LIBC_SCCS and not lint
28
29//**************************************************************************/
30
31#include <stdio.h>
32#ifdef macintosh
33#   include <stdlib.h>
34#   include <errno.h>
35#   include "fcntl.h"
36#   include <string.h>
37#   include <stat.h>
38#   define L_SET SEEK_SET
39#   define EPERM 1
40#   define ENOSPC 28
41#   define bcopy(a,b,c) memcpy(b,a,c)
42#   define bzero(a,b)   memset(a,0,b)
43#   define bcmp(a,b,c)  memcmp(a,b,c)
44#else /* not macintosh */
45#   include <sys/types.h>
46#   include <sys/stat.h>
47#   include <sys/file.h>
48#   include <errno.h>
49#   include <stdlib.h>
50#   include <string.h>
51#   include <unistd.h>
52#endif /* macintosh */
53#include "ndbm.h"
54
55#define BYTESIZ 8
56#undef setbit
57
58static  void dbm_access(register DBM *db, long hash);
59static  int getbit(register DBM *db);
60static  void setbit(register DBM *db);
61static  datum makdatum(char buf[PBLKSIZ], int n);
62static  int finddatum(char buf[PBLKSIZ], datum item);
63static  long hashinc(register DBM *db, long hash);
64static  long dcalchash(datum item);
65static  int delitem(char buf[PBLKSIZ], int n);
66static  int additem(char buf[PBLKSIZ], datum item, datum item1);
67extern  int errno;
68
69
70DBM *
71dbm_open(char *file, int flags, int mode)
72{
73  struct stat statb;
74  register DBM *db;
75
76  if ((db = (DBM *)malloc(sizeof *db)) == 0) {
77    errno = ENOMEM;
78    return ((DBM *)0);
79  }
80#ifdef macintosh
81  // It seems that the compile has some problems to commit the flags properly
82  // O_RDWR | O_CREAT = 0x102 change to 0x200. We don't know why.
83  // setting flags to O_RDWR | O_CREAT solved our problem. :-(
84  flags = O_RDWR | O_CREAT;
85#endif /* macintosh */
86#ifdef MSDOS
87  // default mode of open is ascii, we need binary mode.
88  flags |= O_BINARY;
89#endif
90  db->dbm_flags = (flags & 03) == O_RDONLY ? _DBM_RDONLY : 0;
91  if ((flags & 03) == O_WRONLY)
92    flags = (flags & ~03) | O_RDWR;
93  strcpy(db->dbm_pagbuf, file);
94  strcat(db->dbm_pagbuf, ".pag");
95#ifdef macintosh
96  db->dbm_pagf = open(db->dbm_pagbuf, flags);
97#else /* not macintosh */
98  db->dbm_pagf = open(db->dbm_pagbuf, flags, mode);
99#endif /* macintosh */
100  if (db->dbm_pagf < 0)
101    goto bad;
102  strcpy(db->dbm_pagbuf, file);
103  strcat(db->dbm_pagbuf, ".dir");
104#ifdef macintosh
105  db->dbm_dirf = open(db->dbm_pagbuf, flags);
106#else /* not macintosh */
107  db->dbm_dirf = open(db->dbm_pagbuf, flags, mode);
108#endif /* macintosh */
109  if (db->dbm_dirf < 0)
110    goto bad1;
111  fstat(db->dbm_dirf, &statb);
112  db->dbm_maxbno = statb.st_size*BYTESIZ-1;
113  db->dbm_pagbno = db->dbm_dirbno = -1;
114  return (db);
115bad1:
116  (void) close(db->dbm_pagf);
117bad:
118  free((char *)db);
119  return ((DBM *)0);
120}
121
122void
123dbm_close(DBM *db)
124{
125  (void) close(db->dbm_dirf);
126  (void) close(db->dbm_pagf);
127  free((char *)db);
128}
129
130long
131dbm_forder(register DBM *db, datum key)
132{
133  long hash;
134 
135  hash = dcalchash(key);
136  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1) {
137    db->dbm_blkno = hash & db->dbm_hmask;
138    db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
139    if (getbit(db) == 0)
140      break;
141  }
142  return (db->dbm_blkno);
143}
144
145datum
146dbm_fetch(register DBM *db, datum key)
147{
148  register i;
149  datum item;
150
151  if (dbm_error(db))
152    goto err;
153  dbm_access(db, dcalchash(key));
154  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0) {
155    item = makdatum(db->dbm_pagbuf, i+1);
156    if (item.dptr != NULL)
157      return (item);
158  }
159err:
160  item.dptr = NULL;
161  item.dsize = 0;
162  return (item);
163}
164
165int dbm_delete(register DBM *db, datum key)
166{
167  register i;
168  datum item;
169
170  if (dbm_error(db))
171    return (-1);
172  if (dbm_rdonly(db)) {
173    errno = EPERM;
174    return (-1);
175  }
176  dbm_access(db, dcalchash(key));
177  if ((i = finddatum(db->dbm_pagbuf, key)) < 0)
178    return (-1);
179  if (!delitem(db->dbm_pagbuf, i))
180    goto err;
181  db->dbm_pagbno = db->dbm_blkno;
182  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
183  if (write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ) {
184  err:
185    db->dbm_flags |= _DBM_IOERR;
186    return (-1);
187  }
188  return (0);
189}
190
191int dbm_store(register DBM *db, datum key, datum dat, int replace)
192{
193  register i;
194  int ret;
195  datum item, item1;
196  char ovfbuf[PBLKSIZ];
197
198  if (dbm_error(db))
199    return (-1);
200  if (dbm_rdonly(db)) {
201    errno = EPERM;
202    return (-1);
203  }
204loop:
205  dbm_access(db, dcalchash(key));
206  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0) {
207    if (!replace)
208      return (1);
209    if (!delitem(db->dbm_pagbuf, i)) {
210      db->dbm_flags |= _DBM_IOERR;
211      return (-1);
212    }
213  }
214  if (!additem(db->dbm_pagbuf, key, dat))
215    goto split;
216  db->dbm_pagbno = db->dbm_blkno;
217  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
218  if ( (ret=write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ)) != PBLKSIZ) {
219    db->dbm_flags |= _DBM_IOERR;
220    return (-1);
221  }
222  return (0);
223 
224split:
225  if (key.dsize+dat.dsize+3*sizeof(short) >= PBLKSIZ) {
226    db->dbm_flags |= _DBM_IOERR;
227    errno = ENOSPC;
228    return (-1);
229  }
230  bzero(ovfbuf, PBLKSIZ);
231  for (i=0;;) {
232    item = makdatum(db->dbm_pagbuf, i);
233    if (item.dptr == NULL)
234      break;
235    if (dcalchash(item) & (db->dbm_hmask+1)) {
236      item1 = makdatum(db->dbm_pagbuf, i+1);
237      if (item1.dptr == NULL) {
238        fprintf(stderr, "ndbm: split not paired\n");
239        db->dbm_flags |= _DBM_IOERR;
240        break;
241      }
242      if (!additem(ovfbuf, item, item1) ||
243          !delitem(db->dbm_pagbuf, i)) {
244        db->dbm_flags |= _DBM_IOERR;
245        return (-1);
246      }
247      continue;
248    }
249    i += 2;
250  }
251  db->dbm_pagbno = db->dbm_blkno;
252  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
253  if (write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ) {
254    db->dbm_flags |= _DBM_IOERR;
255    return (-1);
256  }
257  (void) lseek(db->dbm_pagf, (db->dbm_blkno+db->dbm_hmask+1)*PBLKSIZ, L_SET);
258  if (write(db->dbm_pagf, ovfbuf, PBLKSIZ) != PBLKSIZ) {
259    db->dbm_flags |= _DBM_IOERR;
260    return (-1);
261  }
262  setbit(db);
263  goto loop;
264}
265
266datum
267dbm_firstkey(DBM *db)
268{
269 
270  db->dbm_blkptr = 0L;
271  db->dbm_keyptr = 0;
272  return (dbm_nextkey(db));
273}
274
275datum
276dbm_nextkey(register DBM *db)
277{
278  struct stat statb;
279  datum item;
280 
281  if (dbm_error(db) || fstat(db->dbm_pagf, &statb) < 0)
282                goto err;
283  statb.st_size /= PBLKSIZ;
284  for (;;) {
285    if (db->dbm_blkptr != db->dbm_pagbno) {
286      db->dbm_pagbno = db->dbm_blkptr;
287      (void) lseek(db->dbm_pagf, db->dbm_blkptr*PBLKSIZ, L_SET);
288      if (read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
289        bzero(db->dbm_pagbuf, PBLKSIZ);
290#ifdef DEBUG
291      else if (chkblk(db->dbm_pagbuf) < 0)
292        db->dbm_flags |= _DBM_IOERR;
293#endif
294    }
295    if (((short *)db->dbm_pagbuf)[0] != 0) {
296      item = makdatum(db->dbm_pagbuf, db->dbm_keyptr);
297      if (item.dptr != NULL) {
298        db->dbm_keyptr += 2;
299        return (item);
300      }
301      db->dbm_keyptr = 0;
302    }
303    if (++db->dbm_blkptr >= statb.st_size)
304      break;
305  }
306err:
307  item.dptr = NULL;
308  item.dsize = 0;
309  return (item);
310}
311
312static void
313dbm_access(register DBM *db, long hash)
314{
315  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1) {
316    db->dbm_blkno = hash & db->dbm_hmask;
317    db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
318    if (getbit(db) == 0)
319      break;
320  }
321  if (db->dbm_blkno != db->dbm_pagbno) {
322    db->dbm_pagbno = db->dbm_blkno;
323    (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
324    if (read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
325      bzero(db->dbm_pagbuf, PBLKSIZ);
326#ifdef DEBUG
327    else if (chkblk(db->dbm_pagbuf) < 0)
328      db->dbm_flags |= _DBM_IOERR;
329#endif
330  }
331}
332
333static
334int getbit(register DBM *db)
335{
336  long bn;
337  register b, i, n;
338       
339
340  if (db->dbm_bitno > db->dbm_maxbno)
341    return (0);
342  n = db->dbm_bitno % BYTESIZ;
343  bn = db->dbm_bitno / BYTESIZ;
344  i = bn % DBLKSIZ;
345  b = bn / DBLKSIZ;
346  if (b != db->dbm_dirbno) {
347    db->dbm_dirbno = b;
348    (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
349    if (read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
350      bzero(db->dbm_dirbuf, DBLKSIZ);
351  }
352  return (db->dbm_dirbuf[i] & (1<<n));
353}
354
355static void
356setbit(register DBM *db)
357{
358  long bn;
359  register i, n, b;
360
361  if (db->dbm_bitno > db->dbm_maxbno)
362    db->dbm_maxbno = db->dbm_bitno;
363  n = db->dbm_bitno % BYTESIZ;
364  bn = db->dbm_bitno / BYTESIZ;
365  i = bn % DBLKSIZ;
366  b = bn / DBLKSIZ;
367  if (b != db->dbm_dirbno) {
368    db->dbm_dirbno = b;
369    (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
370    if (read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
371      bzero(db->dbm_dirbuf, DBLKSIZ);
372  }
373  db->dbm_dirbuf[i] |= 1<<n;
374  db->dbm_dirbno = b;
375  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
376  if (write(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
377    db->dbm_flags |= _DBM_IOERR;
378}
379
380static datum
381makdatum(char buf[PBLKSIZ], int n)
382{
383  register short *sp;
384  register t;
385  datum item;
386
387  sp = (short *)buf;
388  if ((unsigned)n >= sp[0]) {
389    item.dptr = NULL;
390    item.dsize = 0;
391    return (item);
392  }
393  t = PBLKSIZ;
394  if (n > 0)
395    t = sp[n];
396  item.dptr = buf+sp[n+1];
397  item.dsize = t - sp[n+1];
398  return (item);
399}
400
401static
402int finddatum(char buf[PBLKSIZ], datum item)
403{
404  register short *sp;
405  register int i, n, j;
406
407  sp = (short *)buf;
408  n = PBLKSIZ;
409  for (i=0, j=sp[0]; i<j; i+=2, n = sp[i]) {
410    n -= sp[i+1];
411    if (n != item.dsize)
412      continue;
413    if (n == 0 || bcmp(&buf[sp[i+1]], item.dptr, n) == 0)
414      return (i);
415  }
416  return (-1);
417}
418
419static  int hitab[16]
420/* ken's
421{
422        055,043,036,054,063,014,004,005,
423        010,064,077,000,035,027,025,071,
424};
425*/
426 = {    61, 57, 53, 49, 45, 41, 37, 33,
427        29, 25, 21, 17, 13,  9,  5,  1,
428};
429static  long hltab[64]
430 = {
431        06100151277L,06106161736L,06452611562L,05001724107L,
432        02614772546L,04120731531L,04665262210L,07347467531L,
433        06735253126L,06042345173L,03072226605L,01464164730L,
434        03247435524L,07652510057L,01546775256L,05714532133L,
435        06173260402L,07517101630L,02431460343L,01743245566L,
436        00261675137L,02433103631L,03421772437L,04447707466L,
437        04435620103L,03757017115L,03641531772L,06767633246L,
438        02673230344L,00260612216L,04133454451L,00615531516L,
439        06137717526L,02574116560L,02304023373L,07061702261L,
440        05153031405L,05322056705L,07401116734L,06552375715L,
441        06165233473L,05311063631L,01212221723L,01052267235L,
442        06000615237L,01075222665L,06330216006L,04402355630L,
443        01451177262L,02000133436L,06025467062L,07121076461L,
444        03123433522L,01010635225L,01716177066L,05161746527L,
445        01736635071L,06243505026L,03637211610L,01756474365L,
446        04723077174L,03642763134L,05750130273L,03655541561L,
447};
448
449static long
450hashinc(register DBM *db, long hash)
451{
452  long bit;
453
454  hash &= db->dbm_hmask;
455  bit = db->dbm_hmask+1;
456  for (;;) {
457    bit >>= 1;
458    if (bit == 0)
459      return (0L);
460    if ((hash & bit) == 0)
461      return (hash | bit);
462    hash &= ~bit;
463  }
464}
465
466static long
467dcalchash(datum item)
468{
469  register int s, c, j;
470  register char *cp;
471  register long hashl;
472  register int hashi;
473 
474  hashl = 0;
475  hashi = 0;
476  for (cp = item.dptr, s=item.dsize; --s >= 0; ) {
477    c = *cp++;
478    for (j=0; j<BYTESIZ; j+=4) {
479      hashi += hitab[c&017];
480      hashl += hltab[hashi&63];
481      c >>= 4;
482    }
483  }
484  return (hashl);
485}
486
487/*
488 * Delete pairs of items (n & n+1).
489 */
490static
491int delitem(char buf[PBLKSIZ], int n)
492{
493  register short *sp, *sp1;
494  register i1, i2;
495
496  sp = (short *)buf;
497  i2 = sp[0];
498  if ((unsigned)n >= i2 || (n & 1))
499    return (0);
500  if (n == i2-2) {
501    sp[0] -= 2;
502    return (1);
503  }
504  i1 = PBLKSIZ;
505  if (n > 0)
506    i1 = sp[n];
507  i1 -= sp[n+2];
508  if (i1 > 0) {
509    i2 = sp[i2];
510    bcopy(&buf[i2], &buf[i2 + i1], sp[n+2] - i2);
511  }
512  sp[0] -= 2;
513  for (sp1 = sp + sp[0], sp += n+1; sp <= sp1; sp++)
514    sp[0] = sp[2] + i1;
515  return (1);
516}
517
518/*
519 * Add pairs of items (item & item1).
520 */
521static
522int additem(char buf[PBLKSIZ], datum item, datum item1)
523{
524  register short *sp;
525  register i1, i2;
526
527  sp = (short *)buf;
528  i1 = PBLKSIZ;
529  i2 = sp[0];
530  if (i2 > 0)
531    i1 = sp[i2];
532  i1 -= item.dsize + item1.dsize;
533  if (i1 <= (i2+3) * sizeof(short))
534    return (0);
535  sp[0] += 2;
536  sp[++i2] = i1 + item1.dsize;
537  bcopy(item.dptr, &buf[i1 + item1.dsize], item.dsize);
538  sp[++i2] = i1;
539  bcopy(item1.dptr, &buf[i1], item1.dsize);
540  return (1);
541}
542
543#ifdef DEBUG
544static
545chkblk(char buf[PBLKSIZ])
546{
547  register short *sp;
548  register t, i;
549
550  sp = (short *)buf;
551  t = PBLKSIZ;
552  for (i=0; i<sp[0]; i++) {
553    if (sp[i+1] > t)
554      return (-1);
555    t = sp[i+1];
556  }
557  if (t < (sp[0]+1)*sizeof(short))
558    return (-1);
559  return (0);
560}
561#endif
562
563#endif /* HAVE_DBM */
Note: See TracBrowser for help on using the repository browser.