0

I am trying to do something like this:

     {
      cout << "command: ";
      cin >> m;
      cout << "option: ";
      cin >> o;
      system(m+o);
     }

so that the user can choose which command to run and an option if wanted

  • 4
    Did you try it? Did it fail? – KamilCuk Oct 29 '19 at 16:33
  • Sure, but you're going to have a bad time security-wise. – Federico klez Culloca Oct 29 '19 at 16:34
  • 1
    It depends on what `m`and `o` are. But anyway you probably rather want `m + " " + o`. – Jabberwocky Oct 29 '19 at 16:34
  • it failed even when i added .c_str() to both m and o,because if i added it only to m(which had red error line under it) it wouldn't work anyway. – CyberSlayer Oct 29 '19 at 16:36
  • 1
    @CyberSlayer: You need parentheses so that you call `.c_str()` on the whole of `(m+" "+o)` – MSalters Oct 29 '19 at 16:38
  • 1
    @FedericoklezCulloca: Short of a bad `sudo` rule or sticky bit, this is safe. The program runs a command with the same credentials as itself, which the user could also enter directly into the shell. – MSalters Oct 29 '19 at 16:40
  • 1
    if `m` and `o` are two strings, then `m + o` isnt "two strings", but only one. Please provide a [mcve] and report errors if any in the question – 463035818_is_not_an_ai Oct 29 '19 at 16:41
  • @MSalters I'm internet-oriented. I was thinking about that kind of stuff. – Federico klez Culloca Oct 29 '19 at 16:42
  • @formerlyknownas_463035818: Agree with [mcve] but the question is reasonable. He does have two strings, and his concatenation appear to fail. So it's reasonable to ask if `system` can take two strings instead so that there's no need to concatenate them. It's even more reasonable because `CreateProcess` on Windows does take command and options as two separate strings. – MSalters Oct 29 '19 at 16:47
  • @MSalters I didnt want to imply that the quesiton is not reasonable. I think you refer to the difference between a "please fix my code" and "how can I do X" question. I am not trying to suggest that the latter would make a worse question, its just that `m+o` can go wrong in several ways, but not if they are `std::string`s which isnt mentioned explicitly yet – 463035818_is_not_an_ai Oct 29 '19 at 17:03

2 Answers2

2

The system () function expects a char * parameter, you also are forgetting the space separation. You should do something like this:

system(std::string(m + " " + o).c_str())

Anyway I strongly recommend you not to use the system () function because it's a big security hole.

For more details about this I suggest you to read the following post:

Marcelino
  • 331
  • 2
  • 12
1

You can pass only argument to system() - a null terminated C-style string. However, that string may contain a command to any degree of complexity as long as the host system can handle that command.

Examples:

system("ls");
system("ls -alF");
system("ls -alF | some-other-program ");

Assuming m and o are of type std::string in your posted code, you probably need to use:

std::string command = m + " " + o;
system(command.c_str());
R Sahu
  • 204,454
  • 14
  • 159
  • 270