home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / sp / part01 / dbmstuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-19  |  1.6 KB  |  153 lines

  1. /* dbmstuff.c */
  2.  
  3. /*
  4.  * Interface to old and new dbm routines
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. #ifndef NEWDBM
  10.  
  11. #include <dbm.h>
  12.  
  13. /*ARGSUSED*/
  14. DBMINIT(path, flags)
  15. char *path;
  16. int flags;
  17. {
  18.  
  19.     return(dbminit(path));
  20. }
  21.  
  22. DBMCLOSE()
  23. {
  24.  
  25. #ifdef HAS_CLOSE
  26.     dbmclose();
  27. #else
  28.     close(3);        /* free up the file descriptors */
  29.     close(4);
  30. #endif
  31. }
  32.  
  33. datum
  34. FETCH(key)
  35. datum key;
  36. {
  37.     datum fetch();
  38.  
  39.     return(fetch(key));
  40. }
  41.  
  42. datum
  43. FIRSTKEY()
  44. {
  45.  
  46.     return(firstkey());
  47. }
  48.  
  49. datum
  50. NEXTKEY(key)
  51. datum key;
  52. {
  53.     return(nextkey(key));
  54. }
  55.  
  56. STORE(key, content)
  57. datum key, content;
  58. {
  59.  
  60.     return(store(key, content));
  61. }
  62.  
  63. REPLACE(key, content)
  64. datum key, content;
  65. {
  66.  
  67.     if (delete(key) == -1)
  68.         return(-1);
  69.     return(store(key, content));
  70. }
  71.  
  72. DELETE(key)
  73. datum key;
  74. {
  75.  
  76.     return(delete(key));
  77. }
  78.  
  79. #endif !NEWDBM
  80.  
  81. #ifdef NEWDBM
  82.  
  83. #include <ndbm.h>
  84.  
  85. static DBM *current_db = (DBM *) NULL;
  86.  
  87. DBMINIT(path, flags)
  88. char *path;
  89. int flags;
  90. {
  91.  
  92.     current_db = dbm_open(path, flags, 0);
  93.     if (current_db == (DBM *) NULL)
  94.         return(-1);
  95.     return(0);
  96. }
  97.  
  98. DBMCLOSE()
  99. {
  100.  
  101.     if (current_db != (DBM *) NULL) {
  102.         dbm_close(current_db);
  103.         current_db = (DBM *) NULL;
  104.     }
  105. }
  106.  
  107. datum
  108. FETCH(key)
  109. datum key;
  110. {
  111.  
  112.     return(dbm_fetch(current_db, key));
  113. }
  114.  
  115. datum
  116. FIRSTKEY()
  117. {
  118.  
  119.     return(dbm_firstkey(current_db));
  120. }
  121.  
  122. /*ARGSUSED*/
  123. datum
  124. NEXTKEY(key)
  125. datum key;
  126. {
  127.  
  128.     return(dbm_nextkey(current_db));
  129. }
  130.  
  131. REPLACE(key, content)
  132. datum key, content;
  133. {
  134.  
  135.     return(dbm_store(current_db, key, content, DBM_REPLACE)); 
  136. }
  137.  
  138. STORE(key, content)
  139. datum key, content;
  140. {
  141.  
  142.     return(dbm_store(current_db, key, content, DBM_INSERT));
  143. }
  144.  
  145. DELETE(key)
  146. datum key;
  147. {
  148.  
  149.     return(dbm_delete(current_db, key));
  150. }
  151.  
  152. #endif NEWDBM
  153.