xref: /PHP-8.1/ext/dba/dba_inifile.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: Marcus Boerger <helly@php.net>                               |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 
23 #if DBA_INIFILE
24 #include "php_inifile.h"
25 
26 #include "libinifile/inifile.h"
27 
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 
35 #define INIFILE_DATA \
36 	inifile *dba = info->dbf
37 
38 #define INIFILE_GKEY \
39 	key_type ini_key; \
40 	if (!key) { \
41 		php_error_docref(NULL, E_WARNING, "No key specified"); \
42 		return 0; \
43 	} \
44 	ini_key = inifile_key_split((char*)key) /* keylen not needed here */
45 
46 #define INIFILE_DONE \
47 	inifile_key_free(&ini_key)
48 
DBA_OPEN_FUNC(inifile)49 DBA_OPEN_FUNC(inifile)
50 {
51 	info->dbf = inifile_alloc(info->fp, info->mode == DBA_READER, info->flags&DBA_PERSISTENT);
52 
53 	return info->dbf ? SUCCESS : FAILURE;
54 }
55 
DBA_CLOSE_FUNC(inifile)56 DBA_CLOSE_FUNC(inifile)
57 {
58 	INIFILE_DATA;
59 
60 	inifile_free(dba, info->flags&DBA_PERSISTENT);
61 }
62 
DBA_FETCH_FUNC(inifile)63 DBA_FETCH_FUNC(inifile)
64 {
65 	val_type ini_val;
66 
67 	INIFILE_DATA;
68 	INIFILE_GKEY;
69 
70 	ini_val = inifile_fetch(dba, &ini_key, skip);
71 	*newlen = ini_val.value ? strlen(ini_val.value) : 0;
72 	INIFILE_DONE;
73 	return ini_val.value;
74 }
75 
DBA_UPDATE_FUNC(inifile)76 DBA_UPDATE_FUNC(inifile)
77 {
78 	val_type ini_val;
79 	int res;
80 
81 	INIFILE_DATA;
82 	INIFILE_GKEY;
83 
84 	ini_val.value = val;
85 
86 	if (mode == 1) {
87 		res = inifile_append(dba, &ini_key, &ini_val);
88 	} else {
89 		res = inifile_replace(dba, &ini_key, &ini_val);
90 	}
91 	INIFILE_DONE;
92 	switch(res) {
93 	case -1:
94 		php_error_docref1(NULL, key, E_WARNING, "Operation not possible");
95 		return FAILURE;
96 	default:
97 	case 0:
98 		return SUCCESS;
99 	case 1:
100 		return FAILURE;
101 	}
102 }
103 
DBA_EXISTS_FUNC(inifile)104 DBA_EXISTS_FUNC(inifile)
105 {
106 	val_type ini_val;
107 
108 	INIFILE_DATA;
109 	INIFILE_GKEY;
110 
111 	ini_val = inifile_fetch(dba, &ini_key, 0);
112 	INIFILE_DONE;
113 	if (ini_val.value) {
114 		inifile_val_free(&ini_val);
115 		return SUCCESS;
116 	}
117 	return FAILURE;
118 }
119 
DBA_DELETE_FUNC(inifile)120 DBA_DELETE_FUNC(inifile)
121 {
122 	int res;
123 	bool found = 0;
124 
125 	INIFILE_DATA;
126 	INIFILE_GKEY;
127 
128 	res =  inifile_delete_ex(dba, &ini_key, &found);
129 
130 	INIFILE_DONE;
131 	return (res == -1 || !found ? FAILURE : SUCCESS);
132 }
133 
DBA_FIRSTKEY_FUNC(inifile)134 DBA_FIRSTKEY_FUNC(inifile)
135 {
136 	INIFILE_DATA;
137 
138 	if (inifile_firstkey(dba)) {
139 		char *result = inifile_key_string(&dba->curr.key);
140 		*newlen = strlen(result);
141 		return result;
142 	} else {
143 		return NULL;
144 	}
145 }
146 
DBA_NEXTKEY_FUNC(inifile)147 DBA_NEXTKEY_FUNC(inifile)
148 {
149 	INIFILE_DATA;
150 
151 	if (!dba->curr.key.group && !dba->curr.key.name) {
152 		return NULL;
153 	}
154 
155 	if (inifile_nextkey(dba)) {
156 		char *result = inifile_key_string(&dba->curr.key);
157 		*newlen = strlen(result);
158 		return result;
159 	} else {
160 		return NULL;
161 	}
162 }
163 
DBA_OPTIMIZE_FUNC(inifile)164 DBA_OPTIMIZE_FUNC(inifile)
165 {
166 	/* dummy */
167 	return SUCCESS;
168 }
169 
DBA_SYNC_FUNC(inifile)170 DBA_SYNC_FUNC(inifile)
171 {
172 	/* dummy */
173 	return SUCCESS;
174 }
175 
DBA_INFO_FUNC(inifile)176 DBA_INFO_FUNC(inifile)
177 {
178 	return estrdup(inifile_version());
179 }
180 
181 #endif
182