home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / smail-3.1.28 / pd / sdbm / dbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-12  |  2.4 KB  |  122 lines

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