aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorLibravatar smitsohu <smitsohu@gmail.com>2022-01-14 20:19:25 +0100
committerLibravatar smitsohu <smitsohu@gmail.com>2022-01-14 23:58:43 +0100
commitc764520b5aa343c00c3a73633511df039645973c (patch)
treeefa4f9e44786d571079e29e9a0223107893e0822 /src/lib
parentrefactor closing of file descriptors (diff)
downloadfirejail-c764520b5aa343c00c3a73633511df039645973c.tar.gz
firejail-c764520b5aa343c00c3a73633511df039645973c.tar.zst
firejail-c764520b5aa343c00c3a73633511df039645973c.zip
keep-fd option (#4845)
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/common.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/lib/common.c b/src/lib/common.c
index f46e7db1c..91d5125b1 100644
--- a/src/lib/common.c
+++ b/src/lib/common.c
@@ -31,6 +31,7 @@
31#include <dirent.h> 31#include <dirent.h>
32#include <string.h> 32#include <string.h>
33#include <time.h> 33#include <time.h>
34#include <limits.h>
34#include "../include/common.h" 35#include "../include/common.h"
35#define BUFLEN 4096 36#define BUFLEN 4096
36 37
@@ -320,6 +321,55 @@ const char *gnu_basename(const char *path) {
320 return last_slash+1; 321 return last_slash+1;
321} 322}
322 323
324// takes string with comma separated int values, returns int array
325int *str_to_int_array(const char *str, size_t *sz) {
326 assert(str && sz);
327
328 size_t curr_sz = 0;
329 size_t arr_sz = 16;
330 int *rv = malloc(arr_sz * sizeof(int));
331 if (!rv)
332 errExit("malloc");
333
334 char *dup = strdup(str);
335 if (!dup)
336 errExit("strdup");
337 char *tok = strtok(dup, ",");
338 if (!tok) {
339 free(dup);
340 free(rv);
341 goto errout;
342 }
343
344 while (tok) {
345 char *end;
346 long val = strtol(tok, &end, 10);
347 if (end == tok || *end != '\0' || val < INT_MIN || val > INT_MAX) {
348 free(dup);
349 free(rv);
350 goto errout;
351 }
352
353 if (curr_sz == arr_sz) {
354 arr_sz *= 2;
355 rv = realloc(rv, arr_sz * sizeof(int));
356 if (!rv)
357 errExit("realloc");
358 }
359 rv[curr_sz++] = val;
360
361 tok = strtok(NULL, ",");
362 }
363 free(dup);
364
365 *sz = curr_sz;
366 return rv;
367
368errout:
369 *sz = 0;
370 return NULL;
371}
372
323//************************** 373//**************************
324// time trace based on getticks function 374// time trace based on getticks function
325//************************** 375//**************************