dba_ndbm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if DBA_NDBM
  24. #include "php_ndbm.h"
  25. #include <fcntl.h>
  26. #ifdef NDBM_INCLUDE_FILE
  27. #include NDBM_INCLUDE_FILE
  28. #endif
  29. #define NDBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
  30. DBA_OPEN_FUNC(ndbm)
  31. {
  32. DBM *dbf;
  33. int gmode = 0;
  34. int filemode = 0644;
  35. dba_info *pinfo = (dba_info *) info;
  36. switch(info->mode) {
  37. case DBA_READER:
  38. gmode = O_RDONLY;
  39. break;
  40. case DBA_WRITER:
  41. gmode = O_RDWR;
  42. break;
  43. case DBA_CREAT:
  44. gmode = O_RDWR | O_CREAT;
  45. break;
  46. case DBA_TRUNC:
  47. gmode = O_RDWR | O_CREAT | O_TRUNC;
  48. break;
  49. default:
  50. return FAILURE; /* not possible */
  51. }
  52. if(info->argc > 0) {
  53. convert_to_long_ex(info->argv[0]);
  54. filemode = Z_LVAL_PP(info->argv[0]);
  55. }
  56. dbf = dbm_open(info->path, gmode, filemode);
  57. pinfo->dbf = dbf;
  58. return SUCCESS;
  59. }
  60. DBA_CLOSE_FUNC(ndbm)
  61. {
  62. dbm_close(info->dbf);
  63. }
  64. DBA_FETCH_FUNC(ndbm)
  65. {
  66. datum gval;
  67. char *new = NULL;
  68. NDBM_GKEY;
  69. gval = dbm_fetch(info->dbf, gkey);
  70. if(gval.dptr) {
  71. if(newlen) *newlen = gval.dsize;
  72. new = estrndup(gval.dptr, gval.dsize);
  73. }
  74. return new;
  75. }
  76. DBA_UPDATE_FUNC(ndbm)
  77. {
  78. datum gval;
  79. NDBM_GKEY;
  80. gval.dptr = (char *) val;
  81. gval.dsize = vallen;
  82. if(!dbm_store(info->dbf, gkey, gval, mode == 1 ? DBM_INSERT : DBM_REPLACE))
  83. return SUCCESS;
  84. return FAILURE;
  85. }
  86. DBA_EXISTS_FUNC(ndbm)
  87. {
  88. datum gval;
  89. NDBM_GKEY;
  90. gval = dbm_fetch(info->dbf, gkey);
  91. if(gval.dptr) {
  92. return SUCCESS;
  93. }
  94. return FAILURE;
  95. }
  96. DBA_DELETE_FUNC(ndbm)
  97. {
  98. NDBM_GKEY;
  99. return(dbm_delete(info->dbf, gkey) == -1 ? FAILURE : SUCCESS);
  100. }
  101. DBA_FIRSTKEY_FUNC(ndbm)
  102. {
  103. datum gkey;
  104. char *key = NULL;
  105. gkey = dbm_firstkey(info->dbf);
  106. if(gkey.dptr) {
  107. if(newlen) *newlen = gkey.dsize;
  108. key = estrndup(gkey.dptr, gkey.dsize);
  109. }
  110. return key;
  111. }
  112. DBA_NEXTKEY_FUNC(ndbm)
  113. {
  114. datum gkey;
  115. char *nkey = NULL;
  116. gkey = dbm_nextkey(info->dbf);
  117. if(gkey.dptr) {
  118. if(newlen) *newlen = gkey.dsize;
  119. nkey = estrndup(gkey.dptr, gkey.dsize);
  120. }
  121. return nkey;
  122. }
  123. DBA_OPTIMIZE_FUNC(ndbm)
  124. {
  125. return SUCCESS;
  126. }
  127. DBA_SYNC_FUNC(ndbm)
  128. {
  129. return SUCCESS;
  130. }
  131. DBA_INFO_FUNC(ndbm)
  132. {
  133. return estrdup("NDBM");
  134. }
  135. #endif
  136. /*
  137. * Local variables:
  138. * tab-width: 4
  139. * c-basic-offset: 4
  140. * End:
  141. * vim600: sw=4 ts=4 fdm=marker
  142. * vim<600: sw=4 ts=4
  143. */