fix
[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 pid_t
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   pid_t pid;
28
29   argv_size = 1;
30   va_start (ap, first_arg);
31   arg = first_arg;
32   last = NULL;
33   do
34     {
35       rpos = arg;
36       while ('\0' != *rpos)
37         {
38           if (' ' == *rpos)
39             {
40               if (last != NULL)
41                 argv_size++;
42               last = NULL;
43               while (' ' == *rpos)
44                 rpos++;
45             } 
46           if ( (last == NULL) && (*rpos != '\0') )
47             last = rpos;
48           if (*rpos != '\0')
49             rpos++;
50         }      
51       if (last != NULL)
52         argv_size++;
53     }
54   while (NULL != (arg = (va_arg (ap, const char*))));
55   va_end (ap);
56
57   argv = GNUNET_malloc (argv_size * sizeof (char *));
58   argv_size = 0;
59   va_start (ap, first_arg);
60   arg = first_arg;
61   last = NULL;
62   do
63     {
64       cp = GNUNET_strdup (arg);
65       pos = cp;
66       while ('\0' != *pos)
67         {
68           if (' ' == *pos)
69             {
70               *pos = '\0';
71               if (last != NULL)
72                 argv[argv_size++] = GNUNET_strdup (last);
73               last = NULL;
74               pos++;
75               while (' ' == *pos)
76                 pos++;
77             }
78           if ( (last == NULL) && (*pos != '\0') ) 
79             last = pos;
80           if (*pos != '\0')
81             pos++;
82         }
83       if (last != NULL)
84         argv[argv_size++] = GNUNET_strdup (last);
85       last = NULL;
86       GNUNET_free (cp);
87     }
88   while (NULL != (arg = (va_arg (ap, const char*))));
89   va_end (ap);
90   argv[argv_size] = NULL;
91   pid = GNUNET_OS_start_process_v (lsocks, argv[0], argv);
92   while (argv_size > 0)
93     GNUNET_free (argv[--argv_size]);
94   GNUNET_free (argv);
95   return pid;
96 }