Fix Prevent and remove compile warning messages
[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  * 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'
41  * increment.
42  * Returns the read string or NULL in case of error.
43  */
44 static char *_pread_(FILE *stream, size_t block_size)
45 {
46         char *result = NULL;
47         size_t alloc = 0;
48         size_t length = 0;
49         char *string;
50
51         for (;;) {
52                 /* is on error ? */
53                 if (ferror(stream) != 0) {
54                         free(result);
55                         return NULL;
56                 }
57                 /* allocate enough memory */
58                 /* assert(length <= alloc) */
59                 if (length >= alloc) {
60                         alloc += block_size;
61                         string = realloc(result, alloc + 1); /* one more for ending null */
62                         if (string == NULL) {
63                                 free(result);
64                                 return NULL;
65                         }
66                         result = string;
67                 }
68                 /* assert(length < alloc) */
69                 /* assert(result != NULL); */
70                 /* is at end ? */
71                 if (feof(stream) != 0) {
72                         result[length] = 0;
73                         return result;
74                 }
75                 length += fread(result + length, 1, alloc - length, stream);
76         }
77 }
78
79 /*
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.
84  */
85 static int _ail_vconf_exec_(char **readen, const char *cmddef, ...)
86 {
87         int result;
88         FILE *stream;
89         char *command;
90         va_list ap;
91
92         *readen = NULL;
93         va_start(ap, cmddef);
94         result = vasprintf(&command, cmddef, ap);
95         va_end(ap);
96         if (result >= 0) {
97                 result = -1;
98                 stream = popen(command, "r");
99                 if (stream != NULL) {
100                         *readen = _pread_(stream, 1024);
101                         if (pclose(stream) != -1 && *readen != NULL)
102                                 result = 0;
103                 }
104                 free(command);
105         }
106         return result;
107 }
108
109 /*
110  * vconf_get_str with fallback to the command vconftool.
111  */
112 EXPORT_API char *ail_vconf_get_str(const char *keyname)
113 {
114         char *result;
115         char *data;
116         int status;
117         size_t length;
118
119         result = vconf_get_str(keyname);
120         if (result == NULL) {
121                 status = _ail_vconf_exec_(&data, CMD_VCONF_GET_STR, keyname);
122                 if (status == 0) {
123                         /* the string data is of the form 'key, value = ...\n' */
124                         result = strstr(data, " = ");
125                         if (result != NULL) {
126                                 /* skips the prefix " = " */
127                                 result = result + 3;
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);
134                         }
135                 }
136                 free(data);
137         }
138         return result;
139 }
140
141 /*
142  * vconf_set_str with fallback to the command vconftool.
143  */
144 EXPORT_API int ail_vconf_set_str(const char *keyname, const char *strval)
145 {
146         int result;
147         char *data;
148
149         result = vconf_set_str(keyname, strval);
150         if (result != 0) {
151                 result = _ail_vconf_exec_(&data, CMD_VCONF_SET_STR, keyname, strval);
152                 free(data);
153         }
154         return result;
155 }
156
157 #endif