2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
22 #include <sys/types.h>
27 #define STRING_MAXLEN 1024
29 buff_addc (char* buff, char* buffEnd, int c)
31 int avail = buffEnd - buff;
33 if (avail <= 0) /* already in overflow mode */
36 if (avail == 1) { /* overflowing, the last byte is reserved for zero */
41 buff[0] = (char) c; /* add char and terminating zero */
47 buff_adds (char* buff, char* buffEnd, const char* s)
51 return buff_addb(buff, buffEnd, s, slen);
55 buff_addb (char* buff, char* buffEnd, const void* data, int len)
57 int avail = (buffEnd - buff);
59 if (avail <= 0 || len <= 0) /* already overflowing */
65 memcpy(buff, data, len);
69 /* ensure there is a terminating zero */
70 if (buff >= buffEnd) { /* overflow */
79 buff_add (char* buff, char* buffEnd, const char* format, ... )
83 avail = (buffEnd - buff);
89 va_start(args, format);
90 nn = vsnprintf( buff, avail, format, args);
94 /* some C libraries return -1 in case of overflow,
95 * but they will also do that if the format spec is
96 * invalid. We assume SDB is not buggy enough to
97 * trigger that last case. */
100 else if (nn > avail) {
106 /* ensure that there is a terminating zero */
115 char *str_trim(const char* string)
117 const char* s = string;
118 const char* e = string + (strlen(string) - 1);
121 while(*s == ' ' || *s == '\t') // ltrim
123 while(*e == ' ' || *e == '\t') // rtrim
135 int spawn(char* program, char** arg_list)
140 if ((pid = fork()) < 0) {
141 fprintf(stderr, "couldn't fork: %d\n", errno);
143 } else if (pid == 0) {
144 if ((pid = fork()) < 0) {
145 fprintf(stderr, "couldn't fork: %d\n", errno);
147 } else if (pid > 0) {
148 // init takes the process, and the process is not able to be zombie
151 execvp (program, arg_list);
152 fprintf(stderr, "failed to spawn: never reach here!:%s\n", program);
155 if (waitpid(pid, &ret, 0) != pid) {
156 fprintf(stderr, "failed to wait pid\n");
162 char** str_split(char* a_str, const char a_delim) {
166 char* last_comma = 0;
172 /* Count how many elements will be extracted. */
174 if (a_delim == *tmp) {
181 /* Add space for trailing token. */
182 count += last_comma < (a_str + strlen(a_str) - 1);
184 /* Add space for terminating null string so caller
185 knows where the list of returned strings ends. */
188 result = malloc(sizeof(char*) * count);
192 char* token = strtok_r(a_str, delim, &ptr);
195 //assert(idx < count);
196 *(result + idx++) = strdup(token);
197 token = strtok_r(0, delim, &ptr);
199 //assert(idx == count - 1);