Return to dba_dbm.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / dba |
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
1.1.1.4 ! misho 5: | Copyright (c) 1997-2014 The PHP Group |
1.1 misho 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:
1.1.1.2 misho 19: /* $Id$ */
1.1 misho 20:
21: #ifdef HAVE_CONFIG_H
22: #include "config.h"
23: #endif
24:
25: #include "php.h"
26:
27: #if DBA_DBM
28: #include "php_dbm.h"
29:
30: #ifdef DBM_INCLUDE_FILE
31: #include DBM_INCLUDE_FILE
32: #endif
33: #if DBA_GDBM
34: #include "php_gdbm.h"
35: #endif
36:
37: #include <unistd.h>
38: #include <sys/types.h>
39: #include <sys/stat.h>
40: #include <fcntl.h>
41:
42: #define DBM_DATA dba_dbm_data *dba = info->dbf
43: #define DBM_GKEY datum gkey; gkey.dptr = (char *) key; gkey.dsize = keylen
44:
45: #define TRUNC_IT(extension, mode) \
46: snprintf(buf, MAXPATHLEN, "%s" extension, info->path); \
47: buf[MAXPATHLEN-1] = '\0'; \
48: if((fd = VCWD_OPEN_MODE(buf, O_CREAT | mode | O_WRONLY, filemode)) == -1) \
49: return FAILURE; \
50: close(fd);
51:
52:
53: typedef struct {
54: datum nextkey;
55: } dba_dbm_data;
56:
57: DBA_OPEN_FUNC(dbm)
58: {
59: int fd;
60: int filemode = 0644;
61:
62: if(info->argc > 0) {
63: convert_to_long_ex(info->argv[0]);
64: filemode = Z_LVAL_PP(info->argv[0]);
65: }
66:
67: if(info->mode == DBA_TRUNC) {
68: char buf[MAXPATHLEN];
69:
70: /* dbm/ndbm original */
71: TRUNC_IT(".pag", O_TRUNC);
72: TRUNC_IT(".dir", O_TRUNC);
73: }
74:
75: if(info->mode == DBA_CREAT) {
76: char buf[MAXPATHLEN];
77:
78: TRUNC_IT(".pag", 0);
79: TRUNC_IT(".dir", 0);
80: }
81:
82: if(dbminit((char *) info->path) == -1) {
83: return FAILURE;
84: }
85:
86: info->dbf = pemalloc(sizeof(dba_dbm_data), info->flags&DBA_PERSISTENT);
87: memset(info->dbf, 0, sizeof(dba_dbm_data));
88: return SUCCESS;
89: }
90:
91: DBA_CLOSE_FUNC(dbm)
92: {
93: pefree(info->dbf, info->flags&DBA_PERSISTENT);
94: dbmclose();
95: }
96:
97: DBA_FETCH_FUNC(dbm)
98: {
99: datum gval;
100: char *new = NULL;
101:
102: DBM_GKEY;
103: gval = fetch(gkey);
104: if(gval.dptr) {
105: if(newlen) *newlen = gval.dsize;
106: new = estrndup(gval.dptr, gval.dsize);
107: }
108: return new;
109: }
110:
111: DBA_UPDATE_FUNC(dbm)
112: {
113: datum gval;
114:
115: DBM_GKEY;
116:
117: if (mode == 1) { /* insert */
118: gval = fetch(gkey);
119: if(gval.dptr) {
120: return FAILURE;
121: }
122: }
123:
124: gval.dptr = (char *) val;
125: gval.dsize = vallen;
126:
127: return (store(gkey, gval) == -1 ? FAILURE : SUCCESS);
128: }
129:
130: DBA_EXISTS_FUNC(dbm)
131: {
132: datum gval;
133: DBM_GKEY;
134:
135: gval = fetch(gkey);
136: if(gval.dptr) {
137: return SUCCESS;
138: }
139: return FAILURE;
140: }
141:
142: DBA_DELETE_FUNC(dbm)
143: {
144: DBM_GKEY;
145: return(delete(gkey) == -1 ? FAILURE : SUCCESS);
146: }
147:
148: DBA_FIRSTKEY_FUNC(dbm)
149: {
150: DBM_DATA;
151: datum gkey;
152: char *key = NULL;
153:
154: gkey = firstkey();
155: if(gkey.dptr) {
156: if(newlen) *newlen = gkey.dsize;
157: key = estrndup(gkey.dptr, gkey.dsize);
158: dba->nextkey = gkey;
159: } else
160: dba->nextkey.dptr = NULL;
161: return key;
162: }
163:
164: DBA_NEXTKEY_FUNC(dbm)
165: {
166: DBM_DATA;
167: datum gkey;
168: char *nkey = NULL;
169:
170: if(!dba->nextkey.dptr) return NULL;
171:
172: gkey = nextkey(dba->nextkey);
173: if(gkey.dptr) {
174: if(newlen) *newlen = gkey.dsize;
175: nkey = estrndup(gkey.dptr, gkey.dsize);
176: dba->nextkey = gkey;
177: } else
178: dba->nextkey.dptr = NULL;
179: return nkey;
180: }
181:
182: DBA_OPTIMIZE_FUNC(dbm)
183: {
184: /* dummy */
185: return SUCCESS;
186: }
187:
188: DBA_SYNC_FUNC(dbm)
189: {
190: return SUCCESS;
191: }
192:
193: DBA_INFO_FUNC(dbm)
194: {
195: #if DBA_GDBM
196: if (!strcmp(DBM_VERSION, "GDBM"))
197: {
198: return dba_info_gdbm(hnd, info TSRMLS_CC);
199: }
200: #endif
201: return estrdup(DBM_VERSION);
202: }
203:
204: #endif
205:
206: /*
207: * Local variables:
208: * tab-width: 4
209: * c-basic-offset: 4
210: * End:
211: * vim600: sw=4 ts=4 fdm=marker
212: * vim<600: sw=4 ts=4
213: */