-extending defaults
[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 pipe_control should a pipe be used to send signals to the child?
11  * @param lsocks array of listen sockets to dup starting at fd3 (systemd-style), or NULL
12  * @param first_arg first argument for argv (may be an empty string)
13  * @param ... more arguments, NULL terminated
14  * @return handle of the started process, NULL on error
15  */
16 static struct GNUNET_OS_Process *
17 do_start_process (int pipe_control,
18                   const SOCKTYPE * lsocks, const char *first_arg, ...)
19 {
20   va_list ap;
21   char **argv;
22   unsigned int argv_size;
23   const char *arg;
24   const char *rpos;
25   char *pos;
26   char *cp;
27   const char *last;
28   struct GNUNET_OS_Process *proc;
29
30   argv_size = 1;
31   va_start (ap, first_arg);
32   arg = first_arg;
33   last = NULL;
34 /* *INDENT-OFF* */
35   do
36     {
37 /* *INDENT-ON* */
38   rpos = arg;
39   while ('\0' != *rpos)
40     {
41       if (' ' == *rpos)
42         {
43           if (last != NULL)
44             argv_size++;
45           last = NULL;
46           while (' ' == *rpos)
47             rpos++;
48         }
49       if ((last == NULL) && (*rpos != '\0'))
50         last = rpos;
51       if (*rpos != '\0')
52         rpos++;
53     }
54   if (last != NULL)
55     argv_size++;
56 /* *INDENT-OFF* */
57     }
58   while (NULL != (arg = (va_arg (ap, const char*))));
59 /* *INDENT-ON* */
60   va_end (ap);
61
62   argv = GNUNET_malloc (argv_size * sizeof (char *));
63   argv_size = 0;
64   va_start (ap, first_arg);
65   arg = first_arg;
66   last = NULL;
67 /* *INDENT-OFF* */
68   do
69     {
70 /* *INDENT-ON* */
71   cp = GNUNET_strdup (arg);
72   pos = cp;
73   while ('\0' != *pos)
74     {
75       if (' ' == *pos)
76         {
77           *pos = '\0';
78           if (last != NULL)
79             argv[argv_size++] = GNUNET_strdup (last);
80           last = NULL;
81           pos++;
82           while (' ' == *pos)
83             pos++;
84         }
85       if ((last == NULL) && (*pos != '\0'))
86         last = pos;
87       if (*pos != '\0')
88         pos++;
89     }
90   if (last != NULL)
91     argv[argv_size++] = GNUNET_strdup (last);
92   last = NULL;
93   GNUNET_free (cp);
94 /* *INDENT-OFF* */
95     }
96   while (NULL != (arg = (va_arg (ap, const char*))));
97 /* *INDENT-ON* */
98   va_end (ap);
99   argv[argv_size] = NULL;
100   proc = GNUNET_OS_start_process_v (pipe_control, lsocks, argv[0], argv);
101   while (argv_size > 0)
102     GNUNET_free (argv[--argv_size]);
103   GNUNET_free (argv);
104   return proc;
105 }