<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Hello Professor,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
It worked with the third argument! I modified the cgi-script to print out the arguments passed to it.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
I modified my code and added some statement to printf:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include &lt;unistd.h&gt;</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include &lt;stdlib.h&gt;</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include &lt;string.h&gt;</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include &lt;errno.h&gt;</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">#include &lt;stdio.h&gt;</span></p>
<p style="font: 11px Menlo; margin: 0px; min-height: 13px; color: rgb(0, 0, 0);">
<span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"></span><br>
</p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">int</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">main(int argc, char **argv) {</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>&nbsp; &nbsp; &nbsp; &nbsp;
</span>(void) setenv(&quot;something&quot;, &quot;something&quot;, 1);</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>&nbsp; &nbsp; &nbsp; &nbsp;
</span>printf(&quot;testing\n&quot;);</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>&nbsp; &nbsp; &nbsp; &nbsp;
</span>(void) execl(&quot;/cgi-bin/something.cgi&quot;, (char *) 0);</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>&nbsp; &nbsp; &nbsp; &nbsp;
</span>printf(&quot;%s\n&quot;, strerror(errno));<span>&nbsp;</span></span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"><span>&nbsp; &nbsp; &nbsp; &nbsp;
</span>return 1;</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">}</span></p>
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
The script got &quot;testing&quot; as the first argument, the output is:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">localhost# ./a.out<span>&nbsp;</span></span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">testing</span></p>
<p style="font: 11px Menlo; margin: 0px; min-height: 13px; color: rgb(0, 0, 0);">
<span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"></span><br>
</p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">Content-Type: text/html</span></p>
<p style="font: 11px Menlo; margin: 0px; min-height: 13px; color: rgb(0, 0, 0);">
<span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures"></span><br>
</p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">ENV=/root/.shrc</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">BLOCKSIZE=1k</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">PWD=/root/programs/sws/APUE-Group</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">MAIL=/var/mail/root</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">HOME=/root</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin:/usr/X11R7/bin:/usr/local/sbin:/usr/local/bin</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">HOST=localhost</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">SSH_CONNECTION=10.0.2.2 62926 10.0.2.15 22</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">SSH_TTY=/dev/pts/1</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">TERM=xterm-256color</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">OLDPWD=/root/programs/sws</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">USER=root</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">SSH_CLIENT=10.0.2.2 62926 22</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">LOGNAME=root</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">SHELL=/bin/sh</span></p>
<p style="font: 11px Menlo; margin: 0px; color: rgb(0, 0, 0);"><span style="font-variant-ligatures: no-common-ligatures; font-variant-ligatures: no-common-ligatures">something=something</span></p>
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Similarly, I ran the code without setenv and printf, the script printed out garbage values as the first argument.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Regards.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
Hem Shah</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Jan Schaumann &lt;jschauma@stevens.edu&gt;<br>
<b>Sent:</b> Tuesday, November 26, 2019 7:48 PM<br>
<b>To:</b> Hem Shah &lt;hshah42@stevens.edu&gt;<br>
<b>Cc:</b> cs631apue@lists.stevens.edu &lt;cs631apue@lists.stevens.edu&gt;<br>
<b>Subject:</b> Re: [cs631apue] exec error</font>
<div>&nbsp;</div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hem Shah &lt;hshah42@stevens.edu&gt; wrote:<br>
&gt; int<br>
&gt; main(int argc, char **argv) {<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (void) setenv(&quot;something&quot;, &quot;something&quot;, 1);<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (void) execl(&quot;/cgi-bin/something.cgi&quot;, (char *) 0);<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%s\n&quot;, strerror(errno));<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1;<br>
&gt; }<br>
<br>
Are you compiling this program with &quot;-Wall -Werror&quot;?<br>
<br>
You do not have enough arguments in your call to<br>
execl(3); the function requires at least a third<br>
argument, per the manual page:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp; The const char *arg and subsequent ellipses in the execl(), execlp(),<br>
&nbsp;&nbsp;&nbsp;&nbsp; execlpe(), and execle() functions can be thought of as arg0, arg1, ...,<br>
&nbsp;&nbsp;&nbsp;&nbsp; argn.&nbsp; Together they describe a list of one or more pointers to NUL-<br>
&nbsp;&nbsp;&nbsp;&nbsp; terminated strings that represent the argument list available to the<br>
&nbsp;&nbsp;&nbsp;&nbsp; executed program.&nbsp; The first argument, by convention, should point to the<br>
&nbsp;&nbsp;&nbsp;&nbsp; file name associated with the file being executed.&nbsp; The list of arguments<br>
&nbsp;&nbsp;&nbsp;&nbsp; must be terminated by a NULL pointer.<br>
<br>
Use<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; execl(&quot;/cgi-bin/something.cgi&quot;, &quot;something.cgi&quot;, (char *) 0);<br>
<br>
instead, and it should work.<br>
<br>
(I don't know yet why the program works if you don't<br>
call setenv(3) or if you add a printf(3) after<br>
setenv(3), but calling execl(3) without a third<br>
argument means you have undefined behavior, so<br>
anything is fair game.)<br>
<br>
-Jan<br>
</div>
</span></font></div>
</body>
</html>