title: String tokens do not appear to be either stored or accessed right in my C program
body: <p>I am trying to build a simple shell program in C.  I have posted recently about this, but my program has changed a bit since then.  I can edit my previous question if need be, but I really just want to understand what I'm doing wrong here.</p>
  <p>I'm kind of embarrassed at the way my code looks.  It feels bulky and convoluted but I just don't know what to do to make it any smaller, but I can't even get it right as it currently is.</p>
  <p>I want to take user input, and break it up into shell arguments that can be passed to execvp eventually.  I need to be able to differentiate between input with no '|' and '&gt;' chars, and input with them.  At the moment, it appears that something I'm doing is causing a seg-fault.  But I'm not sure what to make of the error message gdb gives me.</p>
  <p>Any criticism or tips would be greatly appreciated here.  I have been beating my head over this thing for awhile now and just really want to understand.</p>
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65
  65      ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
  (gdb) bt
  #0  __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65
  #1  0x00007ffff7e4b5b4 in __GI__IO_puts (str=0x0) at ioputs.c:35
  #2  0x000055555555573a in tokenizeInput ()
  #3  0x0000555555555471 in main ()
  </code></pre>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  
  #define MAX_BUF_SZ 1024
  
  void checkForPipe(char *string, bool *pipe_bool);
  void checkForRedirect(char *string, bool *redirect_bool);
  void tokenizeInput(char *string,
                                       char **argz,
                                       bool *pipe,
                                       bool *redirect,
                                       int *stats);
  void executeCommand(int *stats, char **argz);
  
  int main()
  {
      char *ptr;
      bool is_pipe = false;
      bool is_redirect_out = false;
      bool is_exit = false;
      int status = 0;           //Will determine how to execute execvp()
      char *args[100] = {NULL};
  
      ptr = (char*)malloc(MAX_BUF_SZ);
  
      while(!is_exit)
      {
          // Diplay prompt
          char cur_dir[MAX_BUF_SZ];
          getcwd(cur_dir, MAX_BUF_SZ);
          printf(&quot;SHELL:%s$ &quot;, cur_dir);
          printf(&quot;%d\n&quot;, status);
  
          fgets(ptr, MAX_BUF_SZ, stdin);
          checkForPipe(ptr, &amp;is_pipe);
          checkForRedirect(ptr, &amp;is_redirect_out);
          printf(&quot;pipe flag = %d\n&quot;, is_pipe);
          printf(&quot;redirect flag = %d\n&quot;, is_redirect_out);
          if(strcmp(ptr, &quot;exit\n&quot;) == 0)
          {
              is_exit = true;
          }
          tokenizeInput(ptr, args, &amp;is_pipe, &amp;is_redirect_out, &amp;status);
          printf(&quot;is token being called?\n&quot;);
          executeCommand(&amp;status, args);
          printf(&quot;is execute being called?\n&quot;);
          printf(&quot;%d\n&quot;, status);
  
      }
  
      return 0;
  
  }
  
  void checkForPipe(char *string, bool *pipe_bool)
  {
      char *check_for_pipes;
      char *clean_compare;
      check_for_pipes = (char*)malloc(MAX_BUF_SZ);
      clean_compare = (char*)malloc(MAX_BUF_SZ);
  
      strcpy(check_for_pipes, string);
      strcpy(clean_compare, string);
  
      char * token = strtok(check_for_pipes, &quot;|&quot;);
  
      if(strcmp(token, clean_compare) == 0)
          {
           free(clean_compare);
           free(check_for_pipes);
          }
  
      else
      {
          *pipe_bool = 1;
          free(clean_compare);
          free(check_for_pipes);
      }
  }
  
  void checkForRedirect(char *string, bool *redirect_bool)
  {
      char *check_for_redirects;
      char *clean_compare;
      check_for_redirects = (char*)malloc(MAX_BUF_SZ);
      clean_compare = (char*)malloc(MAX_BUF_SZ);
  
      strcpy(check_for_redirects, string);
      strcpy(clean_compare, string);
  
      char * token = strtok(check_for_redirects, &quot;&gt;&quot;);
  
      if(strcmp(token, clean_compare) == 0)
          {
           free(clean_compare);
           free(check_for_redirects);
          }
  
      else
      {
          *redirect_bool = 1;
          free(clean_compare);
          free(check_for_redirects);
      }
  
  }
  
  void tokenizeInput(char *string,
                                       char **argz,
                                     bool *pipe,
                                       bool *redirect,
                                       int *stats)
  {
      int i = 0;
          if(*pipe == false &amp;&amp; *redirect == false)
          {
              char *token = strtok(string, &quot; \n&quot;);
              argz[i] = token;
  
              while (token != NULL)
              {
                  token = strtok(NULL, &quot; \n&quot;);
                  i++;
                  argz[i] = token;
                  
                  printf(&quot;%s\n&quot;, argz[i]);
              }
              i = 0;
              *stats = 1; //1 indicates no pipe or redirect
          }
  
          else if(*pipe == true &amp;&amp; *redirect == false)
          {
              char *token = strtok(string, &quot;|\n&quot;);
              argz[i] = token;
  
              while (token != NULL)
              {
                  token = strtok(NULL, &quot;|\n&quot;);
                  i++;
                  argz[i] = token;
              }
              *stats = 2; // 2 indicates a sole pipe
              *pipe = false;
          }
  
          else if(*pipe == false &amp;&amp; *redirect == true)
          {
              char *token = strtok(string, &quot;&gt;\n&quot;);
              argz[i] = token;
  
              while (token != NULL)
              {
                  token = strtok(NULL, &quot;&gt;\n&quot;);
                  i++;
                  argz[i] = token;
              }
              *stats = 3; // 3 indicates a sole redirect
              *redirect = false;
          }
  
          else if(*pipe == true &amp;&amp; *redirect == true)
          {
              char *token = strtok(string, &quot;|&gt;\n&quot;);
              argz[i] = token;
  
              while (token != NULL)
              {
                  token = strtok(NULL, &quot;|&gt;\n&quot;);
                  i++;
                  argz[i] = token;
              }
              *stats = 4; // 4 indicates a pipe and a redirect
              *pipe = false;
              *redirect = false;
          }
  }
  
  void executeCommand(int *stats, char **argz)
  {
      if(*stats == 1)
      {
                  printf(&quot;this is the value stored in arg 0 %s\n&quot;, argz[0]);
                  printf(&quot;this is the value stored in arg 1 %s\n&quot;, argz[1]);
                  pid_t child_pid = fork();
  
                  if(child_pid &lt; 0)
                  {
                      //Forking Error!
                      printf(&quot;Forking Error Occured!\n&quot;);
                      exit(0);
                  }
                  else if(child_pid == 0)
                  {
                      printf(&quot;%s\n&quot;, argz[0]);
                      if(execvp(argz[0],argz) == -1)
                      {
                          printf(&quot;Error running command\n&quot;);
                          exit(0);
                      }
                  }
                  else
                  {
                      wait(NULL);
                  }
  
      }
  }
  </code></pre>