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