build: travis: Set up Coverity scan
[netsniff-ng-new.git] / cpp.c
blob7739f0f8ca1f68674eeed87a0626fd2fdc59ac50
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <libgen.h>
5 #include "cpp.h"
6 #include "str.h"
7 #include "proc.h"
8 #include "xmalloc.h"
10 static size_t argv_len(char *const argv[])
12 size_t len = 0;
14 for (; argv && *argv; argv++)
15 len++;
17 return len;
20 int cpp_exec(char *in_file, char *out_file, size_t out_len, char *const argv[])
22 size_t argc = 7 + argv_len(argv);
23 char *tmp = xstrdup(in_file);
24 char **cpp_argv;
25 int fd, ret = -1;
26 char *base;
27 unsigned int i = 0;
29 base = basename(tmp);
30 slprintf(out_file, out_len, "/tmp/.tmp-XXXXXX-%s", base);
31 fd = mkstemps(out_file, strlen(base) + 1);
32 if (fd < 0)
33 goto err;
35 cpp_argv = xmalloc(argc * sizeof(char *));
37 cpp_argv[i++] = "cpp";
39 for (; argv && *argv; argv++, i++)
40 cpp_argv[i] = *argv;
42 cpp_argv[i++] = "-I";
43 cpp_argv[i++] = ETCDIRE_STRING;
44 cpp_argv[i++] = "-o";
45 cpp_argv[i++] = out_file;
46 cpp_argv[i++] = in_file;
47 cpp_argv[i++] = NULL;
49 ret = proc_exec("cpp", cpp_argv);
50 close(fd);
52 xfree(cpp_argv);
53 err:
54 xfree(tmp);
55 return ret;