Issuing OS commands from within C

If you are writing system utilities then you may need to issue system commands from within your C program. This is straightforward in Linux and a call to system(cmd) is what you need. The argument cmd should contain the entire command, complete with any arguments, redirections, etc. By default, whatever is output by the command is written to stdout, with any error messages going to stderr, so if you do not want to see the output you'll need to direct it to the null device /dev/null.

Detecting errors if you cannot see the messages

The problem with hiding the output is that you cannot see any errors. However, system(cmd) returns the command's return code so you can use that to check whether the command worked or not.

You should redirect your output using the n> command line operator. n is 0 for stdin, 1 for stdout, 2 for stderr and & for both stdout and stderr. A trivial example might be:

RC=system("ls -alg 2> /dev/null");