Issue a command line statement from within a C/C++ program?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Issue a command line statement from within a C/C++ program?
I've almost finished a little app in KDevelop; on selecting certain parameters I have created a string which is something that you would type at a bash prompt in the following basic format:
Does anyone know if it's possible to execute a command from within an application? I am sure it must be achievable somehow. I have done the same thing with a Python script which was pretty straightforward.
For completeness, I mention system(), which does essentially what fork() + exec() + wait() do, all rolled into one call (but has some caveats wrt suid root programs), as well as popen(), which gives you the option of feeding data to the launched program or reading its standard output data into your program.
--- rod.
While searching on Google for how to use exec, I found system(). I knoecked up a quick program which on a button press invoked this line:
system("kwrite");
Sure enough that launched kwrite, so I assume it will execute other command line statements. I need to hook up the Hauppaugge box and see if it works now.
Just one more point: system() loads a shell, which will interpet your commandline. If your argument list is already defined, then it will be more efficient (speed, memory) to use either popen() or fork() + exec(). It is easier to monitor the state of your child process if you use exec(), by calling wait(). This can be useful if you want to kill the child, if you want to limit the number of children, etc.
Also, if any of your commandline arguments come from user input, you must be careful about what arguments you send to the system() call. This is a security issue.
--- rod.
Also, if any of your commandline arguments come from user input, you must be careful about what arguments you send to the system() call. This is a security issue.
Just to clarify, if you had this:
char buf[1024];
sprintf(buf, "kwrite \'%s\'", userarg); //userarg has already been checked for length
system(buf);
The user could inject code into the server by making their argument something like this:
' $(rm -rf your_files) '
Which would thus make the system call this:
kwrite '' $(rm -rf your_files) ''
Now I have a question: How would one execute a command (e.g. kwrite) without invoking a shell while having the ability to read the stdout from that command?
Unfortunately, popen() itself makes use of the shell as well (and thus carries any similar “security risk”). You can duplicate all the functionality of popen() (except the shell part) by calling pipe() and fork(). In the child you should do a dup2() on the relevant file descriptors, followed by execlp(). In the parent, you can then read() or write() (whichever you decided per the dup2()) the other end of the pipe. If you want a true “FILE *” interface, just use fdopen().
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.