2 ** Copyright 2012, The CyanogenMod Project
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
8 ** http://www.apache.org/licenses/LICENSE-2.0
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
17 #include <sys/types.h>
30 /* reads a file, making sure it is terminated with \n \0 */
31 char* read_file(const char *fn)
36 int fd = open(fn, O_RDONLY);
37 if (fd < 0) return data;
39 if (fstat(fd, &st)) goto oops;
41 data = malloc(st.st_size + 2);
44 if (read(fd, data, st.st_size) != st.st_size) goto oops;
46 data[st.st_size] = '\n';
47 data[st.st_size + 1] = 0;
56 int get_property(const char *data, char *found, const char *searchkey, const char *not_found)
58 char *key, *value, *eol, *sol, *tmp;
59 if (data == NULL) goto defval;
61 char *dup = strdup(data);
64 while((eol = strchr(sol, '\n'))) {
69 value = strchr(key, '=');
70 if(value == 0) continue;
73 while(isspace(*key)) key++;
74 if(*key == '#') continue;
76 while((tmp > key) && isspace(*tmp)) *tmp-- = 0;
78 while(isspace(*value)) value++;
80 while((tmp > value) && isspace(*tmp)) *tmp-- = 0;
82 if (strncmp(searchkey, key, strlen(searchkey)) == 0) {
91 if (len >= PROPERTY_VALUE_MAX)
93 memcpy(found, value, len + 1);
98 len = strlen(not_found);
99 memcpy(found, not_found, len + 1);
104 * Fast version of get_property which purpose is to check
105 * whether the property with given prefix exists.
107 * Assume nobody is stupid enough to put a propery with prefix ro.cm.version
108 * in his build.prop on a non-CM ROM and comment it out.
110 int check_property(const char *data, const char *prefix)
114 return strstr(data, prefix) != NULL;