title: Forking with command line arguments
body: <p>I am building a Linux Shell, and my current headache is passing command line arguments to forked/exec'ed programs and system functions. </p>
  
  <p>Currently all input is tokenized on spaces and new lines, in a global variable char * parsed_arguments. For example, the input <strong>dir /usa/folderb</strong> would be tokenized as:</p>
  
  <pre><code>parsed_arguments[0] = dir
  parsed_arguments[1] = /usa/folderb
  </code></pre>
  
  <p>parsed_arguments tokenizes everything perfectly; My issue now is that i wish to only take a subset of parsed_arguments, which excludes the command/ first argument/path to executable to run in the shell, and store them in a new array, called passed_arguments.</p>
  
  <p>so in the previous example <strong>dir /usa/folderb</strong></p>
  
  <pre><code>parsed_arguments[0] = dir
  parsed_arguments[1] = /usa/folderb
  
  passed_arguments[0] = /usa/folderb
  passed_arguments[1] = etc....
  </code></pre>
  
  <p>Currently I am not having any luck with this so I'm hoping someone could help me with this. Here is some code of what I have working so far:</p>
  
  <p>How I'm trying to copy arguments:</p>
  
  <pre><code>void  command_Line()
  {
  
    int i = 1;
    for(i;parsed_arguments[i]!=NULL;i++)
      printf("%s",parsed_arguments[i]);
  
  }
  </code></pre>
  
  <p>Function to read commands:</p>
  
  <pre><code>void readCommand(char newcommand[]){
  
  printf("readCommand: %s\n", newcommand);
  
  
  //parsed_arguments =  (char* malloc(MAX_ARGS));
  //  strcpy(newcommand,inputstring);
    parsed =  parsed_arguments;
    *parsed++ = strtok(newcommand,SEPARATORS);   // tokenize input
      while ((*parsed++ = strtok(NULL,SEPARATORS)))
        //printf("test1\n"); // last entry will be NULL
  
        //passed_arguments=parsed_arguments[1];
  
      if(parsed[0]){  
        char *initial_command =parsed[0];
  
    parsed= parsed_arguments;
    while (*parsed) fprintf(stdout,"%s\n ",*parsed++);
    // free (parsed);
    // free(parsed_arguments);
  
      }//end of if
  
  
    command_Line();
  
  }//end of ReadCommand
  </code></pre>
  
  <p>Forking function:</p>
  
  <pre><code>else if(strstr(parsed_arguments[0],"./")!=NULL)
      {
        int pid;
        switch(pid=fork()){
        case -1:
         printf("Fork error, aborting\n");
         abort();
        case 0:
          execv(parsed_arguments[0],passed_arguments);
  
        }
  
      }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/YjToQ.png" alt="enter image description here"></p>
  
  <p>This is what my shell currently outputs. The first time I run it, it outputs something close to what I want, but every subsequent call breaks the program. In addition, each additional call appends the parsed arguments to the output.</p>
  
  <p><img src="https://i.stack.imgur.com/a9FBq.png" alt=""></p>
  
  <p>This is what the original shell produces. Again it's close to what I want, but not quite. I want to omit the command (i.e. "./testline").</p>