Add sub package ail_vconf-devel
[platform/core/appfw/ail.git] / src / ail_vconf.c
1 /*
2  * ail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #if !defined(NO_VCONF_BUXTON_FALLBACK)
23
24 #define _GNU_SOURCE
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29
30 #include <vconf.h>
31 #include "ail_vconf.h"
32 #include "ail_private.h"
33
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"
37
38 /*
39
40  Reads the content of the input 'stream' (it should be a text without nul
41 characters) in a feshly allocated buffer, using allocations of 'block_size'
42 increment.
43
44  Returns the read string or NULL in case of error.
45 */
46 static char *_pread_(FILE *stream, size_t block_size)
47 {
48         char *result = NULL;
49         size_t alloc = 0;
50         size_t length = 0;
51         char *string;
52
53         for(;;) {
54                 /* is on error ? */
55                 if (ferror(stream) != 0) {
56                         free(result);
57                         return NULL;
58                 }
59                 /* allocate enough memory */
60                 /* assert(length <= alloc) */
61                 if (length >= alloc) {
62                         alloc += block_size;
63                         string = realloc(result, alloc + 1); /* one more for ending null */
64                         if (string == NULL) {
65                                 free(result);
66                                 return NULL;
67                         }
68                         result = string;
69                 }
70                 /* assert(length < alloc) */
71                 /* assert(result != NULL); */
72                 /* is at end ? */
73                 if (feof(stream) != 0) {
74                         result[length] = 0;
75                         return result;
76                 }
77                 length += fread(result + length, 1, alloc - length, stream);
78         }
79 }
80
81 /*
82  Runs the command given by 'cmddef' and its arguments formated as printf.
83
84  The resulting output stream of the command is return as a freshly allocated
85 string in '*readen'.
86
87  Retruns 0 in case of success or -1 in case of error.
88 */
89 static int _ail_vconf_exec_(char **readen, const char *cmddef, ...)
90 {
91         int result;
92         FILE *stream;
93         char *command;
94         va_list ap;
95
96         *readen = NULL;
97         va_start(ap, cmddef);
98         result = vasprintf(&command, cmddef, ap);
99         va_end(ap);
100         if (result >= 0) {
101                 result = -1;
102                 stream = popen(command, "r");
103                 if (stream != NULL) {
104                         *readen = _pread_(stream, 1024);
105                         if (pclose(stream) != -1 && *readen != NULL) {
106                                 result = 0;
107                         }
108                 }
109                 free(command);
110         }
111         return result;
112 }
113
114 /*
115  vconf_get_str with fallback to the command vconftool.
116 */
117 EXPORT_API char *ail_vconf_get_str(const char *keyname)
118 {
119         char *result;
120         char *data;
121         int status;
122         size_t length;
123
124         result = vconf_get_str(keyname);
125         if (result == NULL) {
126                 status = _ail_vconf_exec_(&data, CMD_VCONF_GET_STR, keyname);
127                 if (status == 0) {
128                         /* the string data is of the form 'key, value = ...\n' */
129                         result = strstr(data, " = ");
130                         if (result != NULL) {
131                                 /* skips the prefix " = " */
132                                 result = result + 3;
133                                 /* remove trailing '\n' */
134                                 length = strlen(result);
135                                 if (length > 0 && result[length-1] == '\n') {
136                                         result[length-1] = 0;
137                                 }
138                                 /* get the final result */
139                                 result = strdup(result);
140                         }
141                 }
142                 free(data);
143         }
144         return result;
145 }
146
147 /*
148  vconf_set_str with fallback to the command vconftool.
149 */
150 EXPORT_API int ail_vconf_set_str(const char *keyname, const char *strval)
151 {
152         int result;
153         char *data;
154
155         result = vconf_set_str(keyname, strval);
156         if (result != 0) {
157                 result = _ail_vconf_exec_(&data, CMD_VCONF_SET_STR, keyname, strval);
158                 free(data);
159         }
160         return result;
161 }
162
163 #endif
164
165
166