a2aca9deacd5245e04f674b77f0ede211e177a2d
[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,
17                   const char *first_arg, ...)
18 {
19   va_list ap;
20   char **argv;
21   unsigned int argv_size;
22   const char *arg;
23   const char *rpos;
24   char *pos;
25   char *cp;
26   const char *last;
27   struct GNUNET_OS_Process *proc;
28
29   argv_size = 1;
30   va_start (ap, first_arg);
31   arg = first_arg;
32   last = NULL;
33 /* *INDENT-OFF* */
34   do
35     {
36 /* *INDENT-ON* */
37       rpos = arg;
38       while ('\0' != *rpos)
39         {
40           if (' ' == *rpos)
41             {
42               if (last != NULL)
43                 argv_size++;
44               last = NULL;
45               while (' ' == *rpos)
46                 rpos++;
47             } 
48           if ( (last == NULL) && (*rpos != '\0') )
49             last = rpos;
50           if (*rpos != '\0')
51             rpos++;
52         }      
53       if (last != NULL)
54         argv_size++;
55 /* *INDENT-OFF* */
56     }
57   while (NULL != (arg = (va_arg (ap, const char*))));
58 /* *INDENT-ON* */
59   va_end (ap);
60
61   argv = GNUNET_malloc (argv_size * sizeof (char *));
62   argv_size = 0;
63   va_start (ap, first_arg);
64   arg = first_arg;
65   last = NULL;
66 /* *INDENT-OFF* */
67   do
68     {
69 /* *INDENT-ON* */
70       cp = GNUNET_strdup (arg);
71       pos = cp;
72       while ('\0' != *pos)
73         {
74           if (' ' == *pos)
75             {
76               *pos = '\0';
77               if (last != NULL)
78                 argv[argv_size++] = GNUNET_strdup (last);
79               last = NULL;
80               pos++;
81               while (' ' == *pos)
82                 pos++;
83             }
84           if ( (last == NULL) && (*pos != '\0') ) 
85             last = pos;
86           if (*pos != '\0')
87             pos++;
88         }
89       if (last != NULL)
90         argv[argv_size++] = GNUNET_strdup (last);
91       last = NULL;
92       GNUNET_free (cp);
93 /* *INDENT-OFF* */
94     }
95   while (NULL != (arg = (va_arg (ap, const char*))));
96 /* *INDENT-ON* */
97   va_end (ap);
98   argv[argv_size] = NULL;
99   proc = GNUNET_OS_start_process_v (lsocks, argv[0], argv);
100   while (argv_size > 0)
101     GNUNET_free (argv[--argv_size]);
102   GNUNET_free (argv);
103   return proc;
104 }