0

I want to fork and exec a binary in c. The parameter for calling the binary contains a space, see first line of sample code. If execute my binary with `plist [2] = conPtr -> conmon_cmd[f];` the parameter is not evaluated correctly.

Using the two commented line will bring me the result which I desire. How can I escape the space in the first line of the code snippet so that I have only one plist parameter for the arguments?

conPtr->conmon_cmd = "clear_config all";
[...]
printf("child process started (PID = %i)\n", pid);  
char *plist[6];
plist [0] = "/tmp/conmon_audinate_controller";
plist [1] = "localhost";
plist [2] = conPtr -> conmon_cmd;
//plist [2] = "clear_config";
//plist [3] = "all";
plist [4] = (char *)NULL;
char *file = "conmon_audinate_controller";
execvp (file,  plist );                        

Info: the conmon_audinate_controller is an external executable. In the bash on my ucLinux the call would be like:
/tmp # ./conmon_audinate_controller localhost clear_config all

Stefatronik
  • 312
  • 2
  • 16
  • What is the variable `f`? – Mathieu Feb 08 '18 at 11:59
  • sry f should not have been there, I removed it – Stefatronik Feb 08 '18 at 12:06
  • What is `/tmp/conmon_audinate_controller`? Is it a script of some sort that parses arguments? A binary executable? You need to post a [minimal, complete, verifiable example](https://stackoverflow.com/help/mcve) or the only answers you get are going to be guesses. – Andrew Henle Feb 08 '18 at 12:08
  • `Using the two commented line will bring me the result which I desire` if that fix your problem why don't your go ahead with that? Do you face any issue because of that? – Abhijit Pritam Dutta Feb 08 '18 at 12:57
  • because the parameters do not always have a structure like that. Anyway I changed my code so that it doesn't matter if one or more parameters are forwarded. – Stefatronik Feb 08 '18 at 13:25

1 Answers1

2

If I understand correctly, you have a single C-string (with two words) that you want to pass as two arguments to conmon_audinate_controller.

exec family functions don't support such thing - "clear_config all" will be passed as a single argument. What you can do is split conPtr->conmon_cmd, using strtok for example, into two and pass two arguments (like the commented out lines).

P.P
  • 117,907
  • 20
  • 175
  • 238