1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "php.h"
24
25 #if DBA_NDBM
26 #include "php_ndbm.h"
27
28 #include <fcntl.h>
29 #ifdef NDBM_INCLUDE_FILE
30 #include NDBM_INCLUDE_FILE
31 #endif
32
33 #define NDBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
34
DBA_OPEN_FUNC(ndbm)35 DBA_OPEN_FUNC(ndbm)
36 {
37 DBM *dbf;
38 int gmode = 0;
39 int filemode = 0644;
40 dba_info *pinfo = (dba_info *) info;
41
42 switch(info->mode) {
43 case DBA_READER:
44 gmode = O_RDONLY;
45 break;
46 case DBA_WRITER:
47 gmode = O_RDWR;
48 break;
49 case DBA_CREAT:
50 gmode = O_RDWR | O_CREAT;
51 break;
52 case DBA_TRUNC:
53 gmode = O_RDWR | O_CREAT | O_TRUNC;
54 break;
55 default:
56 return FAILURE; /* not possible */
57 }
58
59 if(info->argc > 0) {
60 filemode = zval_get_long(&info->argv[0]);
61 }
62
63 dbf = dbm_open(info->path, gmode, filemode);
64
65 pinfo->dbf = dbf;
66 return SUCCESS;
67 }
68
DBA_CLOSE_FUNC(ndbm)69 DBA_CLOSE_FUNC(ndbm)
70 {
71 dbm_close(info->dbf);
72 }
73
DBA_FETCH_FUNC(ndbm)74 DBA_FETCH_FUNC(ndbm)
75 {
76 datum gval;
77 char *new = NULL;
78
79 NDBM_GKEY;
80 gval = dbm_fetch(info->dbf, gkey);
81 if(gval.dptr) {
82 if(newlen) *newlen = gval.dsize;
83 new = estrndup(gval.dptr, gval.dsize);
84 }
85 return new;
86 }
87
DBA_UPDATE_FUNC(ndbm)88 DBA_UPDATE_FUNC(ndbm)
89 {
90 datum gval;
91
92 NDBM_GKEY;
93 gval.dptr = (char *) val;
94 gval.dsize = vallen;
95
96 if(!dbm_store(info->dbf, gkey, gval, mode == 1 ? DBM_INSERT : DBM_REPLACE))
97 return SUCCESS;
98 return FAILURE;
99 }
100
DBA_EXISTS_FUNC(ndbm)101 DBA_EXISTS_FUNC(ndbm)
102 {
103 datum gval;
104 NDBM_GKEY;
105 gval = dbm_fetch(info->dbf, gkey);
106 if(gval.dptr) {
107 return SUCCESS;
108 }
109 return FAILURE;
110 }
111
DBA_DELETE_FUNC(ndbm)112 DBA_DELETE_FUNC(ndbm)
113 {
114 NDBM_GKEY;
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 char *key = NULL;
122
123 gkey = dbm_firstkey(info->dbf);
124 if(gkey.dptr) {
125 if(newlen) *newlen = gkey.dsize;
126 key = estrndup(gkey.dptr, gkey.dsize);
127 }
128 return key;
129 }
130
DBA_NEXTKEY_FUNC(ndbm)131 DBA_NEXTKEY_FUNC(ndbm)
132 {
133 datum gkey;
134 char *nkey = NULL;
135
136 gkey = dbm_nextkey(info->dbf);
137 if(gkey.dptr) {
138 if(newlen) *newlen = gkey.dsize;
139 nkey = estrndup(gkey.dptr, gkey.dsize);
140 }
141 return nkey;
142 }
143
DBA_OPTIMIZE_FUNC(ndbm)144 DBA_OPTIMIZE_FUNC(ndbm)
145 {
146 return SUCCESS;
147 }
148
DBA_SYNC_FUNC(ndbm)149 DBA_SYNC_FUNC(ndbm)
150 {
151 return SUCCESS;
152 }
153
DBA_INFO_FUNC(ndbm)154 DBA_INFO_FUNC(ndbm)
155 {
156 return estrdup("NDBM");
157 }
158
159 #endif
160