title: Tokenizing a String to Pass as char * into execve()
body: <p>My knowledge of C is <em>very</em> limited. I'm trying to tokenize a String passed to a server from a client, because I want to use passed arguments to<code>execve</code>. The arguments passed via<code>buffer</code>needs to be copied to<code>*argv</code> and tokenized such that<code>buffer</code>'s tokens can be accessed with<code>argv[0]</code>, <code>argv[1]</code>, etc. Obviously I'm doing something incorrectly. </p>
  
  <pre><code>n = read(sockfd, buffer, sizeof(buffer));
  strcpy(*argv, buffer);
  printf("buffer:%s\n", buffer);
  printf("argv:%s\n", *argv);
  printf("argv[0]:%s\n", argv[0]);
  printf("argv[1]:%s\n", argv[1]);
  *argv = strtok_r(*argv, " ", argv);
  printf("argv:%s\n", *argv);
  
  i = fork();
  if (i &lt; 0) {
      //Close socket on fork error.
      perror("fork");
      exit(-1);
  } else if (i == 0) {
      //execve on input args
      execve(argv[0], &amp;argv[0], 0);
      exit(0);
  } else {
      wait(&amp;status);
      //close(sockfd);
  }
  </code></pre>
  
  <p>Passing the arguments "/bin/date -u" with the above code gives an output of:</p>
  
  <pre><code>buffer:/bin/date -u
  
  argv:/bin/date -u
  
  argv[0]:/bin/date -u
  
  argv[1]:(null)
  </code></pre>
  
  <p>What I what is an output of:</p>
  
  <pre><code>buffer:/bin/date -u
  
  argv:/bin/date -u
  
  argv[0]:/bin/date
  
  argv[1]:-u
  </code></pre>
  
  <p>I tried using<code>strtok_r()</code>, but it didn't work as I intended. The snippet I inserted was:</p>
  
  <pre><code>*argv = strtok_r(*argv, " ", argv);
  printf("argv:%s\n", *argv);
  </code></pre>
  
  <p>which give an output of <code>argv:/bin/date</code>.</p>
  
  <p>Thanks in advanced, SO.</p>
  
  <p>Edit: I don't have to explicitly tokenize<code>buffer</code>like I have above. Any way to get arguments from the client passed to the server works fine.</p>