5

image Calling UNIX Shell Commands from C

Use the system() function to pass control to the shell environment that a C language application is running in. Here is a minimal source script listing that executes a command to list a directory into a temporary file:

#include <stdio.h> #include <stdlib.h> main(void) {   char command [80];   sprintf(command, “ls /working_dir > /tmp/xxx.txt\n");   system(command); return(0); }

Also check out the fork() and exec() function calls in the standard library if you want to create multiple processes running in the background.

Get Developing Quality Metadata now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.