Executing Shell Commands from a C program
Reference: Sections 14.3, 14.4, Advanced Programming in the UNIX Environment, by W. Richard Stevens.

If you want to run shell commands from within a C program there are several options:

If you elect to bypass the shell, it can be handy to have buffered I/O. STDIO functions are available on a file descriptor with:
fdopen - associate a stream with a file descriptor

FILE *fdopen(int fd, const char *mode);
After this call, STDIO buffered I/O and standard I/O functions such as scanf( ) and printf( ) can be used on fd.

system - run shell command

int system(const char *command);
The command is run noninteractively in a shell, output is to the terminal and your program waits for command completion. The main disadvantage with system( ) is that I/O bypasses your program unless you take steps to catch it.

popen - pipe to/from shell

FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
Typically usage is:
     FILE *fp;
     char *command;

     /* command contains the command string (a character array) */

     /* If you want to read output from command */
     fp = popen(command,"r"); 
          /* read output from command */
           fscanf(fp,....);   /* or other STDIO input functions */
     
     fclose(fp);

     /* If you want to send input to command */
     fp = popen(command,"w"); 
          /* write to command */
           fprintf(fp,....);   /* or other STDIO output functions */
     
     fclose(fp);
The program cannot both send input to the command and read its output. The file fp behaves like an ordinary STDIO file.
 
Shell as a coprocess
The shell can be forked off as a child of the program and then commands can be both sent to it and the results can be read. More generally any other program could be run this way. There are a number of problems with this approach: There are some alternatives. For example, a shell-like utility called expect can be used to programatically control a coprocess.

Last update: 2000 December 29