tags:

views:

50

answers:

2

I am planning to write a program that makes calls to cdrecord. (I am a beginner, a beginner trying to "scratch an itch") The program would be written in C++. I have identified that I need to be able to run cdrecord in order for this to work.

cdrecord is written in C. However the documentation on using it is from the command line. The source code includes a main function that powers the command line app, which is the same as the code I wand cdrecord to do.

I am wondering whether I should:

  1. Change main to another name, then include the source file and call it when I need to.
  2. Call the compiled program using the system() command.
  3. Something else.
+2  A: 

Using system() will allow you to not worry about cdrecord's code. Personally, I would only include the code in my own program if I had some very pressing issues that require me to include it. I think system() is the way to go.

http://www.cplusplus.com/reference/clibrary/cstdlib/system/

1) Is there any particular reason you would rather include it in your own code, as opposed to just using it as it is?

2) Do you have the rights to change the code and include it in your own program?

Sagar
Thanks. I was mainly concerned that system would create poor code.
Portablejim
Nah. In that case, I would go with `system()`.
Sagar
+1  A: 

system() is generally a nice way to go, just be careful not to inject arbitrary untrusted values into the string you execute. For example, if you have a web frontend where with a padsize option defaulted to 0, and someone types in not a number but "0; rm -rf *;", make sure you don't end up calling "cdrecord padsize=0; rm -rf *; ...".

The other thing with system is just that it can be slower starting a second distinct process - this might matter if you're running that program hundreds of times and each time it only has a few milliseconds of work to do, but in your case the overhead of launching is dwarfed by the likely cdrecord run-time.