4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #if !defined(NO_VCONF_BUXTON_FALLBACK)
31 #include "ail_vconf.h"
32 #include "ail_private.h"
34 #define VCONFTOOL "/usr/bin/vconftool"
35 #define CMD_VCONF_GET_STR VCONFTOOL " -q get '%s'"
36 #define CMD_VCONF_SET_STR VCONFTOOL " -q set -t string '%s' '%s' -f"
39 * Reads the content of the input 'stream' (it should be a text without nul
40 * characters) in a feshly allocated buffer, using allocations of 'block_size'
42 * Returns the read string or NULL in case of error.
44 static char *_pread_(FILE *stream, size_t block_size)
53 if (ferror(stream) != 0) {
57 /* allocate enough memory */
58 /* assert(length <= alloc) */
59 if (length >= alloc) {
61 string = realloc(result, alloc + 1); /* one more for ending null */
68 /* assert(length < alloc) */
69 /* assert(result != NULL); */
71 if (feof(stream) != 0) {
75 length += fread(result + length, 1, alloc - length, stream);
80 * Runs the command given by 'cmddef' and its arguments formated as printf.
81 * The resulting output stream of the command is return as a freshly allocated
82 * string in '*readen'.
83 * Retruns 0 in case of success or -1 in case of error.
85 static int _ail_vconf_exec_(char **readen, const char *cmddef, ...)
94 result = vasprintf(&command, cmddef, ap);
98 stream = popen(command, "r");
100 *readen = _pread_(stream, 1024);
101 if (pclose(stream) != -1 && *readen != NULL)
110 * vconf_get_str with fallback to the command vconftool.
112 EXPORT_API char *ail_vconf_get_str(const char *keyname)
119 result = vconf_get_str(keyname);
120 if (result == NULL) {
121 status = _ail_vconf_exec_(&data, CMD_VCONF_GET_STR, keyname);
123 /* the string data is of the form 'key, value = ...\n' */
124 result = strstr(data, " = ");
125 if (result != NULL) {
126 /* skips the prefix " = " */
128 /* remove trailing '\n' */
129 length = strlen(result);
130 if (length > 0 && result[length-1] == '\n')
131 result[length - 1] = 0;
132 /* get the final result */
133 result = strdup(result);
142 * vconf_set_str with fallback to the command vconftool.
144 EXPORT_API int ail_vconf_set_str(const char *keyname, const char *strval)
149 result = vconf_set_str(keyname, strval);
151 result = _ail_vconf_exec_(&data, CMD_VCONF_SET_STR, keyname, strval);