you want to start writing the structure of the CLI on a pattern like this one. Then it's all easy.
Code:
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#define SIZE 255
/*#define DEBUG*/
int main(int argc, char *argv[])
{
char inputLine[ SIZE ];
int pid;
while( 1 ) {
if ( fgets( inputLine, SIZE-1, stdin ) == NULL ) {
fprintf( stderr, "fgets error.\n");
exit(1);
}
inputLine[ strlen(inputLine)-1 ] = 0; /* remove \n */
#ifdef DEBUG
/* some debug informations */
fprintf( stdout, "read cmd line: %s\n", inputLine );
#endif
if ( strcasecmp( inputLine, "exit" ) == 0 ) {
/* by typing "exit", the users exits */
fprintf( stdout, "exiting\n");
break;
}
/* fork to exec the command line */
if ( (pid = fork()) < 0 ) {
fprintf( stderr, "fork error, errno = %d\n", errno );
} else if ( pid == 0 ) {
#ifdef DEBUG
fprintf( stdout, "child %d will execute %s\n", getpid(), inputLine );
#endif
system( inputLine );
exit(0);
} else {
#ifdef DEBUG
fprintf( stdout, "parent started child %d\n", pid);
#endif
}
}
fprintf( stdout, "bye bye..\n");
return 0;
}
that's just one simple example. All the command are executed in the background, due to the fork.