home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / ext / SDBM_File / sdbm / dbm.c < prev    next >
C/C++ Source or Header  |  1999-07-20  |  2KB  |  118 lines

  1. /*
  2.  * Copyright (c) 1985 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)dbm.c    5.4 (Berkeley) 5/24/89";
  20. #endif /* not lint */
  21.  
  22. #include    "dbm.h"
  23.  
  24. #define    NODB    ((DBM *)0)
  25.  
  26. static DBM *cur_db = NODB;
  27.  
  28. static char no_db[] = "dbm: no open database\n";
  29.  
  30. int
  31. dbminit(char *file)
  32. {
  33.     if (cur_db != NODB)
  34.         dbm_close(cur_db);
  35.  
  36.     cur_db = dbm_open(file, 2, 0);
  37.     if (cur_db == NODB) {
  38.         cur_db = dbm_open(file, 0, 0);
  39.         if (cur_db == NODB)
  40.             return (-1);
  41.     }
  42.     return (0);
  43. }
  44.  
  45. long
  46. forder(datum key)
  47. {
  48.     if (cur_db == NODB) {
  49.         printf(no_db);
  50.         return (0L);
  51.     }
  52.     return (dbm_forder(cur_db, key));
  53. }
  54.  
  55. datum
  56. fetch(datum key)
  57. {
  58.     datum item;
  59.  
  60.     if (cur_db == NODB) {
  61.         printf(no_db);
  62.         item.dptr = 0;
  63.         return (item);
  64.     }
  65.     return (dbm_fetch(cur_db, key));
  66. }
  67.  
  68. int
  69. delete(datum key)
  70. {
  71.     if (cur_db == NODB) {
  72.         printf(no_db);
  73.         return (-1);
  74.     }
  75.     if (dbm_rdonly(cur_db))
  76.         return (-1);
  77.     return (dbm_delete(cur_db, key));
  78. }
  79.  
  80. int
  81. store(datum key, datum dat)
  82. {
  83.     if (cur_db == NODB) {
  84.         printf(no_db);
  85.         return (-1);
  86.     }
  87.     if (dbm_rdonly(cur_db))
  88.         return (-1);
  89.  
  90.     return (dbm_store(cur_db, key, dat, DBM_REPLACE));
  91. }
  92.  
  93. datum
  94. firstkey(void)
  95. {
  96.     datum item;
  97.  
  98.     if (cur_db == NODB) {
  99.         printf(no_db);
  100.         item.dptr = 0;
  101.         return (item);
  102.     }
  103.     return (dbm_firstkey(cur_db));
  104. }
  105.  
  106. datum
  107. nextkey(datum key)
  108. {
  109.     datum item;
  110.  
  111.     if (cur_db == NODB) {
  112.         printf(no_db);
  113.         item.dptr = 0;
  114.         return (item);
  115.     }
  116.     return (dbm_nextkey(cur_db, key));
  117. }
  118.