LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2008, 04:12 PM   #1
DiBosco
Member
 
Registered: Nov 2001
Location: Manchester, UK
Distribution: Mageia
Posts: 807

Rep: Reputation: 40
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:

echo 58000 > /sys/class/pvrusb2/sn-8563645/ctl_volume/cur_val

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.
 
Old 05-22-2008, 04:27 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Yes, you can do it with exec(). In your case it will probably be fork() to create two processes and exec() later.

Seeing only one example, I'd say it's easier to make open(), write() and then close().
 
Old 05-22-2008, 05:08 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 05-22-2008, 05:13 PM   #4
DiBosco
Member
 
Registered: Nov 2001
Location: Manchester, UK
Distribution: Mageia
Posts: 807

Original Poster
Rep: Reputation: 40
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.
 
Old 05-23-2008, 09:50 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 05-23-2008 at 09:53 AM.
 
Old 05-23-2008, 10:09 AM   #6
JoeyAdams
Member
 
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
Quote:
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?
 
Old 05-23-2008, 12:55 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
popen()
--- rod.
 
Old 05-23-2008, 07:34 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by theNbomr View Post
popen()
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().
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use c++ to use a command line program jboss1995 Programming 4 04-05-2008 10:22 PM
bash case statement with multiple command line options pwc101 Programming 16 06-01-2007 11:43 AM
how to write command line program? vito_huang Linux - Software 3 04-14-2006 12:20 AM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM
command line program installation popcorn5714 Linux - Newbie 4 09-14-2002 03:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration