title: batch processing in a simple shell
body: <p>Hey folks I am making a batch command function in a shell that reads from a txt file and pipes it to a child to exec. </p>
  
  <p>I'm having an issue with exec. I suspect it is something with the null terminator. If I execl with an L and an explicit (char*)NULL the exec runs. If I execvp(argIn[0],argIn) nothing runs and returns a -1. If I execvp with an explicit (char*)NULL I get an error cannot convert char* to char* constant*. I read somewhere that it might be the g++ compiler giving me the error but the gcc compiler wouldn't give the error. Right now it won't compile with gcc though so I'm not sure if that's true. But it shouldn't need the explicit terminator anyway. I'm not sure if the '\0' I have stored is being passed to the exec right. It checks out when I pass it to other functions though so maybe that's not the solution.</p>
  
  <p>Second, my for loop won't exec more than once which I think is more to do with the first solution. I can get the execl to fork with an index but I can't increment the index to point to the right token the next time through because the child should be wiping out my index right?</p>
  
  <p>Anyway it's been 3 weeks of digging to figure out what's wrong. I failed the assignment. Probably going to fail the class. I don't know what else to try. So any help I would appreciate. </p>
  
  <p>My question is why would the exec function not execute the program? I'm passing execvp(program name, program name, option, option, '\0') and not getting a result. </p>
  
  <p>or </p>
  
  <p>execl(program name, program name[index], option[index+1], option[index+1], (char*)NULL) and getting a result. They both seem to be following the parameters but only one is giving me a result. </p>
  
  <pre><code>#include&lt;string.h&gt;
  #include&lt;iostream&gt;
  #include&lt;ctype.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/wait.h&gt;
  #include&lt;unistd.h&gt;
  
  using namespace std;
  int makearg(char s[], char**args[]);
  
  int main(int argc, char *argv[])
  {
      char **argIn;
      int argCount;
      int pos = 0;
      char str[500];
      pid_t pid = fork();
  
      for(int i = 0; i &lt; 4; i++)
      {
              if(pid == 0)
              {
                  while(fgets(str, 500, stdin) != NULL)
                      {
                          cout &lt;&lt; "String loaded: " &lt;&lt; str;
                          argCount = makearg(str, &amp;argIn);
                  execvp(argIn[0],argIn); //nothing exec
  //              execl(argIn[0],argIn[0],argIn[1],argIn[2],(char*)NULL); //exec but requires index. 
          else if(pid &lt; 0)
          {
                     perror("fork() error");
                     exit(-1);
          }
          else if(pid &gt; 0)
          {
              cout &lt;&lt; "Parent waiting" &lt;&lt; endl;
              wait(NULL);
          }
      }
      return 0;
  }
  
  
  
  int makearg(char s[], char**args[])
  {
      int counter = 1;
      int tokenLen = 1;
      int i = 0;
      int j = 0;
      int k = 0;
      char arg1[50];
      char * arg2;
  
      strcpy(arg1, s);
      //Count white space.
      while (arg1[j] != '\0')
      {
          if (arg1[j] == ' ' || arg1[j] == '\0' || arg1[j] == '\n')
          {
              counter++;
          }
          j++;
      }
  
      //Allocate the number of rows to be pointed to.
      args[0] = (char**) malloc(counter + 1);
      if(args[0] == NULL)
          exit(1);
  
  
      //Allocate the size of the c string arrays
      j = 0;
      while(arg1[j] != '\0')
      {
          if (arg1[j] == ' ' || arg1[j] == '\0' || arg1[j] == '\n')
          {
              (*args)[i] = (char*)(malloc(tokenLen));
              if((*args)[i] == NULL)
                  exit(1);
  
              tokenLen = 0;
              i++;
          }
  
          j++;
          tokenLen++;
      }
  
      (*args)[i] = (char*)(malloc(tokenLen));
      if ((*args)[i] == NULL)
          exit(1);
  
      //reset values
      i = 0;
      j = 0;
  
      //Set arg2 to point to args row head. Transfer values from arg1 to arg2.
      arg2 = ((*args)[i]);
      while(arg1[j] != '\0')
      {
          if (arg1[j] != ' ' &amp;&amp; arg1[j] != '\0' &amp;&amp; arg1[j] != '\n')
          {
              arg2[k] = arg1[j];
              k++;
          }
          else
          {
              arg2[k] = '\0';
              i++;
              k = 0;
              arg2 = ((*args)[i]);
          }
          j++;
  
      }
  
      arg2[k] = '\0';
  
      if (counter &lt; 1)
      {
          return -1;
      }
  
      return counter;
  }
  </code></pre>