Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6953417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:28:34+00:00 2026-05-27T14:28:34+00:00

This question talks about using the system command and passing variables. Here is an

  • 0

This question talks about using the system command and passing variables. Here is an example it gives:

string cmd("curl -b cookie.txt -d test=");
cmd += line;
cmd += "  http://example.com";
system(cmd.c_str());

One of the comments mentions that if line was passed and contained foo & fire_nukes.exe & REM then it’s quite possible something bad could happen.

PHP has a great function called escape_shell_args which can be used to escape parameters that are being passed to the program. Does C++ have a way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:28:35+00:00Added an answer on May 27, 2026 at 2:28 pm

    The best way is not to use system() at all. Use fork() and exec() and friends.
    Here’s an example:

    #include <string>
    #include <unistd.h>
    #include <error.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <errno.h>
    #include <cstdlib>
    
    int fork_curl(char *line)
    {
        std::string linearg("line=");
        linearg += line;
    
        int pid = fork();
        if(pid != 0) {
            /* We're in the parent process, return the child's pid. */
            return pid;
        }
        /* Otherwise, we're in the child process, so let's exec curl. */
    
        execlp("curl", "-b", "cookie.txt", "-d", linearg.c_str());  
        /* exec is not supposed to return, so something
           must have gone wrong. */
        exit(100);
        /* Note: You should probably use an exit  status
           that doesn't collide with these used by curl, if possible. */
    }
    
    int main(int argc, char* argv[])
    {
        int cpid = fork_curl("test");
        if(cpid == -1) {
            /* Failed to fork */
            error(1, errno, "Fork failed");
            return 1;
        }
    
        /* Optionally, wait for the child to exit and get
           the exit status. */
        int status;
        waitpid(cpid, &status, 0);
        if(! WIFEXITED(status)) {
            error(1, 0, "The child was killed or segfaulted or something\n");
        }
    
        status = WEXITSTATUS(status);
        /* Status now contains the exit status of the child process. */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen this question about deploying to WebSphere using the WAS ant tasks.
So in this answer to the question: Hidden Features of PHP ? Talks about
This is a(n) historical question, not a comparison-between-languages question: This article from 2005 talks
Here is one more newbie question: require 'tasks/rails' I saw this line in Rakefile
This question comes on the heels of the question asked here . The email
This question is a follow up to my previous question about getting the HTML
A follow-up on this system management question : Since I probably will not get
Background: I am aware of this SO question about Transactional NTFS (TxF) and this
There are several talks about this issue, but I havent found a clear complete
quick question about using ORMLite. I am trying to make sure that my implementation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.