summaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c248
1 files changed, 0 insertions, 248 deletions
diff --git a/src/utils.c b/src/utils.c
index 206c684..2cc5675 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -76,254 +76,6 @@ void fdlist_reset(struct fdlist *list)
76 list->count = 0; 76 list->count = 0;
77} 77}
78 78
79#define CAPACITY_STEP 8
80
81void collection_init(struct collection *col)
82{
83 col->list = malloc(sizeof(void *) * CAPACITY_STEP);
84 memset(col->list, 0, sizeof(void *) * CAPACITY_STEP);
85 col->capacity = CAPACITY_STEP;
86}
87
88void collection_free(struct collection *col)
89{
90 free(col->list);
91 col->list = NULL;
92 col->capacity = 0;
93}
94
95void collection_add(struct collection *col, void *element)
96{
97 int i;
98 for(i=0; i<col->capacity; i++) {
99 if(!col->list[i]) {
100 col->list[i] = element;
101 return;
102 }
103 }
104 col->list = realloc(col->list, sizeof(void*) * (col->capacity + CAPACITY_STEP));
105 memset(&col->list[col->capacity], 0, sizeof(void *) * CAPACITY_STEP);
106 col->list[col->capacity] = element;
107 col->capacity += CAPACITY_STEP;
108}
109
110void collection_remove(struct collection *col, void *element)
111{
112 int i;
113 for(i=0; i<col->capacity; i++) {
114 if(col->list[i] == element) {
115 col->list[i] = NULL;
116 return;
117 }
118 }
119 util_error("collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity);
120}
121
122int collection_count(struct collection *col)
123{
124 int i, cnt = 0;
125 for(i=0; i<col->capacity; i++) {
126 if(col->list[i])
127 cnt++;
128 }
129 return cnt;
130}
131
132void collection_copy(struct collection *dest, struct collection *src)
133{
134 if (!dest || !src) return;
135 dest->capacity = src->capacity;
136 dest->list = malloc(sizeof(void*) * src->capacity);
137 memcpy(dest->list, src->list, sizeof(void*) * src->capacity);
138}
139
140#ifndef HAVE_STPCPY
141/**
142 * Copy characters from one string into another
143 *
144 * @note: The strings should not overlap, as the behavior is undefined.
145 *
146 * @s1: The source string.
147 * @s2: The destination string.
148 *
149 * @return a pointer to the terminating `\0' character of @s1,
150 * or NULL if @s1 or @s2 is NULL.
151 */
152char *stpcpy(char * s1, const char * s2)
153{
154 if (s1 == NULL || s2 == NULL)
155 return NULL;
156
157 strcpy(s1, s2);
158
159 return s1 + strlen(s2);
160}
161#endif
162
163/**
164 * Concatenate strings into a newly allocated string
165 *
166 * @note: Specify NULL for the last string in the varargs list
167 *
168 * @str: The first string in the list
169 * @...: Subsequent strings. Use NULL for the last item.
170 *
171 * @return a newly allocated string, or NULL if @str is NULL. This will also
172 * return NULL and set errno to ENOMEM if memory is exhausted.
173 */
174char *string_concat(const char *str, ...)
175{
176 size_t len;
177 va_list args;
178 char *s;
179 char *result;
180 char *dest;
181
182 if (!str)
183 return NULL;
184
185 /* Compute final length */
186
187 len = strlen(str) + 1; /* plus 1 for the null terminator */
188
189 va_start(args, str);
190 s = va_arg(args, char *);
191 while (s) {
192 len += strlen(s);
193 s = va_arg(args, char*);
194 }
195 va_end(args);
196
197 /* Concat each string */
198
199 result = malloc(len);
200 if (!result)
201 return NULL; /* errno remains set */
202
203 dest = result;
204
205 dest = stpcpy(dest, str);
206
207 va_start(args, str);
208 s = va_arg(args, char *);
209 while (s) {
210 dest = stpcpy(dest, s);
211 s = va_arg(args, char *);
212 }
213 va_end(args);
214
215 return result;
216}
217
218int buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length)
219{
220 FILE *f;
221 uint64_t size;
222
223 *length = 0;
224
225 f = fopen(filename, "rb");
226 if (!f) {
227 return 0;
228 }
229
230 fseek(f, 0, SEEK_END);
231 size = ftell(f);
232 rewind(f);
233
234 if (size == 0) {
235 fclose(f);
236 return 0;
237 }
238
239 *buffer = (char*)malloc(sizeof(char)*(size+1));
240
241 if (!buffer) {
242 return 0;
243 }
244
245 int ret = 1;
246 if (fread(*buffer, sizeof(char), size, f) != size) {
247 usbmuxd_log(LL_ERROR, "%s: ERROR: couldn't read %d bytes from %s", __func__, (int)size, filename);
248 free(*buffer);
249 ret = 0;
250 errno = EIO;
251 }
252 fclose(f);
253
254 *length = size;
255 return ret;
256}
257
258int buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length)
259{
260 FILE *f;
261
262 f = fopen(filename, "wb");
263 if (f) {
264 size_t written = fwrite(buffer, sizeof(char), length, f);
265 fclose(f);
266
267 if (written == length) {
268 return 1;
269 }
270 else {
271 // Not all data could be written.
272 errno = EIO;
273 return 0;
274 }
275 }
276 else {
277 // Failed to open the file, let the caller know.
278 return 0;
279 }
280}
281
282int plist_read_from_filename(plist_t *plist, const char *filename)
283{
284 char *buffer = NULL;
285 uint64_t length;
286
287 if (!filename)
288 return 0;
289
290 if (!buffer_read_from_filename(filename, &buffer, &length)) {
291 return 0;
292 }
293
294 if ((length > 8) && (memcmp(buffer, "bplist00", 8) == 0)) {
295 plist_from_bin(buffer, length, plist);
296 } else {
297 plist_from_xml(buffer, length, plist);
298 }
299
300 free(buffer);
301
302 return 1;
303}
304
305int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format)
306{
307 char *buffer = NULL;
308 uint32_t length;
309
310 if (!plist || !filename)
311 return 0;
312
313 if (format == PLIST_FORMAT_XML)
314 plist_to_xml(plist, &buffer, &length);
315 else if (format == PLIST_FORMAT_BINARY)
316 plist_to_bin(plist, &buffer, &length);
317 else
318 return 0;
319
320 int res = buffer_write_to_filename(filename, buffer, length);
321
322 free(buffer);
323
324 return res;
325}
326
327#ifndef HAVE_CLOCK_GETTIME 79#ifndef HAVE_CLOCK_GETTIME
328typedef int clockid_t; 80typedef int clockid_t;
329#define CLOCK_MONOTONIC 1 81#define CLOCK_MONOTONIC 1