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