Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>Forgive me for diverting from the matter at hand in my previous answer (by suggesting the use of threads). Since I'm going in a completely new direction here, I feel compelled to add this as a separate answer.</em></p> <p><strong>Short version:</strong></p> <p>Please make the following changes in your program:</p> <pre><code>1. length = argc; // in place of length = sizeof(argv); 2. execl(argv[i],argv[i],0); // in place of execvp(argv[i],0); 3. #include &lt;unistd.h&gt; // if you haven't already </code></pre> <p><strong>Long version:</strong></p> <p><strong>(1)</strong> By the variable <code>length</code>, I presume you want to get the total number of arguments. <code>argv</code> is a <code>pointer-to-char-pointer</code>, and as such is simply a memory address. If you print out the length in your program, you will notice it is always 4 (or whatever is the size of a memory address in your system).</p> <p>So this:</p> <pre><code>length = sizeof(argv); </code></pre> <p>Should really be this:</p> <pre><code>length = argc; </code></pre> <p><code>argc</code> holds the total number of arguments passed when executing the process. For example,</p> <pre><code>./a.out /bin/ps /bin/ls </code></pre> <p>gives: argc = <strong>3</strong> (and not <strong>2</strong>, a very common pitfall)</p> <p><strong>(2)</strong> Another issue with your program, is the <code>execvp</code> call.</p> <p>The prototpye for the execvp is as follows:</p> <pre><code>int execvp(const char *file, char *const argv[]); </code></pre> <p>where, argv is the list of arguments passed to the new command, very similar to the argv in your own program.</p> <p>What you use in your program is:</p> <pre><code>execvp(argv[i],0); </code></pre> <p>Suppose <code>i=1</code> and <code>argv[1] = "/bin/ls"</code>. What this command does is look for the <code>/bin/ls</code> executable &amp; pass a NULL pointer (<code>0</code>) to it. This may lead to the following runtime error:</p> <pre><code>A NULL argv[0] was passed through an exec system call. </code></pre> <p>Referring to the exec man page,</p> <blockquote> <p>The first argument, by convention, should point to the filename associated with the file being executed.</p> </blockquote> <p>Though it is not mandatory to pass the filename again, you certainly shouldn't pass a NULL pointer. Since you don't want to pass any arguments, I suggest you use the following <code>execl</code> call, instead:</p> <pre><code>execl(argv[i],argv[i],0); </code></pre> <p>Remember that all such calls are finally converted to <code>execve()</code> finally &amp; then executed, making them equivalent eventually. </p> <p>I encourage you to read more about the <a href="http://linux.die.net/man/3/exec" rel="nofollow">exec</a> family of functions using <code>man</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload