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