move GNUNET_TRANSPORT_ATS_ to GNUNET_ATS_
[oweals/gnunet.git] / src / arm / do_start_process.c
1 /**
2  * Actually start a process.  All of the arguments given to this
3  * function are strings that are used for the "argv" array.  However,
4  * if those strings contain spaces, the given argument is split into
5  * multiple argv entries without spaces.  Similarly, if an argument is
6  * the empty string, it is skipped.  This function has the inherent
7  * limitation that it does NOT allow passing command line arguments
8  * with spaces to the new process.
9  *
10  * @param lsocks array of listen sockets to dup starting at fd3 (systemd-style), or NULL
11  * @param first_arg first argument for argv (may be an empty string)
12  * @param ... more arguments, NULL terminated
13  * @return PID of the started process, -1 on error
14  */
15 static struct GNUNET_OS_Process *
16 do_start_process (const int *lsocks, const char *first_arg, ...)
17 {
18   va_list ap;
19   char **argv;
20   unsigned int argv_size;
21   const char *arg;
22   const char *rpos;
23   char *pos;
24   char *cp;
25   const char *last;
26   struct GNUNET_OS_Process *proc;
27
28   argv_size = 1;
29   va_start (ap, first_arg);
30   arg = first_arg;
31   last = NULL;
32 /* *INDENT-OFF* */
33   do
34     {
35 /* *INDENT-ON* */
36   rpos = arg;
37   while ('\0' != *rpos)
38   {
39     if (' ' == *rpos)
40     {
41       if (last != NULL)
42         argv_size++;
43       last = NULL;
44       while (' ' == *rpos)
45         rpos++;
46     }
47     if ((last == NULL) && (*rpos != '\0'))
48       last = rpos;
49     if (*rpos != '\0')
50       rpos++;
51   }
52   if (last != NULL)
53     argv_size++;
54 /* *INDENT-OFF* */
55     }
56   while (NULL != (arg = (va_arg (ap, const char*))));
57 /* *INDENT-ON* */
58   va_end (ap);
59
60   argv = GNUNET_malloc (argv_size * sizeof (char *));
61   argv_size = 0;
62   va_start (ap, first_arg);
63   arg = first_arg;
64   last = NULL;
65 /* *INDENT-OFF* */
66   do
67     {
68 /* *INDENT-ON* */
69   cp = GNUNET_strdup (arg);
70   pos = cp;
71   while ('\0' != *pos)
72   {
73     if (' ' == *pos)
74     {
75       *pos = '\0';
76       if (last != NULL)
77         argv[argv_size++] = GNUNET_strdup (last);
78       last = NULL;
79       pos++;
80       while (' ' == *pos)
81         pos++;
82     }
83     if ((last == NULL) && (*pos != '\0'))
84       last = pos;
85     if (*pos != '\0')
86       pos++;
87   }
88   if (last != NULL)
89     argv[argv_size++] = GNUNET_strdup (last);
90   last = NULL;
91   GNUNET_free (cp);
92 /* *INDENT-OFF* */
93     }
94   while (NULL != (arg = (va_arg (ap, const char*))));
95 /* *INDENT-ON* */
96   va_end (ap);
97   argv[argv_size] = NULL;
98   proc = GNUNET_OS_start_process_v (lsocks, argv[0], argv);
99   while (argv_size > 0)
100     GNUNET_free (argv[--argv_size]);
101   GNUNET_free (argv);
102   return proc;
103 }