1

I have a C program I am working on that takes a certain number of inputs and runs them as system commands. The rest get passed on to the shell for execution. It was suggested however, that I should try to make use of fork and exec in order to run commands. I'm stumped on how to make this happen though.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MAX_BUFFER 1024                        // max line buffer
#define MAX_ARGS 64                            // max # args
#define SEPARATORS " \t\n"                     // token sparators

extern char **environ;
/*******************************************************************/

int main (int argc, char ** argv)
{
char linebuf[MAX_BUFFER];                  // line buffer
char cmndbuf[MAX_BUFFER];                  // command buffer
char * args[MAX_ARGS];                     // pointers to arg strings
char ** arg;                               // working pointer thru args
char * prompt = "==>" ;                    // shell prompt

// keep reading input until "quit" command or eof of redirected input 

while (!feof(stdin)) { 

// get command line from input


    fputs (prompt, stdout);                // write prompt
    fflush(stdout);
    if (fgets(linebuf, MAX_BUFFER, stdin )) { // read a line

// tokenize the input into args array

        arg = args;
        *arg++ = strtok(linebuf,SEPARATORS);   // tokenize input
        while ((*arg++ = strtok(NULL,SEPARATORS)));
                                           // last entry will be NULL 

        if (args[0]) {                     // if there's anything there

            cmndbuf[0] = 0;                // set zero-length command string

// check for internal/external command 

            if (!strcmp(args[0],"clr")) {  // "clr" command
                strcpy(cmndbuf, "clear");
            } else
            if (!strcmp(args[0],"cd"))
            {
                int ret;
                if (!args[1])
                    strcpy(cmndbuf, "pwd");
                ret = chdir(args[1]);
                strcpy(cmndbuf, "pwd");


            }else
            if (!strcmp(args[0],"dir")) {  // "dir" command
                strcpy(cmndbuf, "ls -al ");
                if (!args[1])
                    args[1] = ".";         // if no arg set current directory
                strcat(cmndbuf, args[1]);
            } else
            if (!strcmp(args[0],"environ")) { // "environ" command
                char ** envstr = environ;
                while (*envstr) {            // print out environment
                    printf("%s\n",*envstr);
                    envstr++;
                }                            // (no entry in cmndbuf)
            } else
            if (!strcmp(args[0],"quit")) {   // "quit" command
                break;
            } else {                         // pass command on to OS shell
                int i = 1;
                strcpy(cmndbuf, args[0]);
                while (args[i]) {
                    strcat(cmndbuf, " ");
                    strcat(cmndbuf, args[i++]);
                }
            }

// pass any command onto OS

            if (cmndbuf[0])
                system(cmndbuf);
        }
    }
}
return 0; 
}
Ryan
  • 193
  • 2
  • 2
  • 14

2 Answers2

1

I am assuming you have a POSIX system, eg GNU/Linux.

This is a very common question. The first answer would be to study the implementation of system function inside free C libraries like GNU Libc. Also many good books on Unix cover this question.

As a clue, system works with fork-ing and then in the child process execve of the shell /bin/sh

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

An example of "system" function implementation from The UNIX Programming Environment by Brian Kernighan and Rob Pike.

#include <signal.h>
system(s) /* выполнить командную строку s */
char *s;
{
  int status, pid, w, tty;
  int (*istat)(), (*qstat)();
  extern char *progname;
  fflush(stdout);
  tty = open("/dev/tty", 2);
  if (tty == 1) {
    fprintf(stderr, "%s: can't open /dev/tty\n", progname);
    return 1;
  }
  if ((pid = fork()) == 0) {
    close(0); dup(tty);
    close(1); dup(tty);
    close(2); dup(tty);
    close(tty);
    execlp("sh", "sh", " c", s, (char *) 0);
    exit(127);
  }
  close(tty);
  istat = signal(SIGINT, SIG_IGN);
  qstat = signal(SIGQUIT, SIG_IGN);

  while ((w = wait(&status)) != pid && w != 1);
  if (w == 1)
    status = 1;
  signal(SIGINT, istat);
  signal(SIGQUIT, qstat);
  return status;
}

Note that the book was written before the C standard was finalized, so it does not use prototypes.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
VinS
  • 194
  • 8