<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>DaniWeb IT Discussion Community</title>
		<link>http://www.daniweb.com/forums/</link>
		<description>Tech support, programming, web development, and internet marketing community. Forums to get free computer help and support.</description>
		<language>en-US</language>
		<lastBuildDate>Fri, 18 Dec 2009 10:16:00 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.daniweb.com/alphaimages/misc/rss.jpg</url>
			<title>DaniWeb IT Discussion Community</title>
			<link>http://www.daniweb.com/forums/</link>
		</image>
		<item>
			<title>building my own primitive shell</title>
			<link>http://www.daniweb.com/forums/thread240754.html</link>
			<pubDate>Mon, 23 Nov 2009 17:21:18 GMT</pubDate>
			<description><![CDATA[Hi all, 
 
I'm helping my girl out with her hw, (/me not knowing C at all, but someone has to help, right?) 
I am supposed to read user input, and then try to execute a given command plus the arguments in every given $PATH.  
 
I managed it all, but I get puzzled by the execv function, which I am...]]></description>
			<content:encoded><![CDATA[<div>Hi all,<br />
<br />
I'm helping my girl out with her hw, (/me not knowing C at all, but someone has to help, right?)<br />
I am supposed to read user input, and then try to execute a given command plus the arguments in every given $PATH. <br />
<br />
I managed it all, but I get puzzled by the execv function, which I am supposed to be using, it doesn't  produce any stdio, and I can't seem to find a way to make it work<br />
<br />
a basic command <br />
 <pre style="margin:20px; line-height:13px">execv (&quot;/bin/touch&quot;, &quot;/tmp/test123&quot;, NULL);</pre>doesn't do anything, neither do I see an exit code. <br />
How do I use it?<br />
<br />
Here's what I've got so far:<br />
 <pre style="margin:20px; line-height:13px">void get_path_exec(char cmd[50])<br />
{<br />
&nbsp; &nbsp; char command[50] = {0} ; //full command with path included<br />
&nbsp; &nbsp; char *tempcmd = NULL;<br />
&nbsp; &nbsp; char *result = NULL; //path iterator<br />
&nbsp; &nbsp; char *com[] = {0}; //command with args, no path<br />
&nbsp; &nbsp; char *arg = NULL; //list of arguments, path and command excluded<br />
&nbsp; &nbsp; char delims[] = &quot;:&quot;; //path iteration delimiter<br />
&nbsp; &nbsp; char delims1[] = &quot; &quot;; //<br />
&nbsp; &nbsp; char * path; //variable to hold the full path delimited by ':'<br />
&nbsp; &nbsp; int pid, status; //process flow control<br />
&nbsp; &nbsp; int argcount = 0; //argumetn counter, where 0 is the command<br />
&nbsp;  <br />
&nbsp; &nbsp; tempcmd=cmd; //assign to temp variable, so strtok doesn't ruin cmd<br />
&nbsp; &nbsp; path=getenv(&quot;PATH&quot;);<br />
&nbsp; &nbsp; if (path != NULL)<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;Path is %s \n&quot;, path);<br />
&nbsp; &nbsp; else exit(1);&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; arg = strtok( tempcmd, delims1 ); <br />
&nbsp; &nbsp; com[0] = arg; //place the command into com[0]<br />
&nbsp; &nbsp; while( arg )<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; argcount++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arg = strtok(NULL,delims1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; com[argcount] = arg;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;arg is %s \n&quot;, arg);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; argcount++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; com[argcount]=NULL; //last value in args should be NULL, right?<br />
<br />
&nbsp; &nbsp; result = strtok( path, delims ); <br />
&nbsp; &nbsp; while( result ) <br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp;  strcpy(command, &quot;&quot;);<br />
&nbsp; &nbsp; &nbsp;  strcat(command, result);<br />
&nbsp; &nbsp; &nbsp;  strcat(command, &quot;/&quot;);<br />
&nbsp; &nbsp; &nbsp;  strcat(command,cmd);<br />
&nbsp; &nbsp; &nbsp;  strcat(command,&quot;\0&quot;); //gather all parts of full path'd command - path, &quot;/&quot; and cmdname<br />
//&nbsp; &nbsp; &nbsp;  printf(&quot;full command: %s \n\n&quot;, command);<br />
&nbsp; &nbsp; &nbsp;  <br />
&nbsp; &nbsp; &nbsp;  if (( pid = fork()) == 0 ) //create a new process<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; execv(command, com);<br />
//below is a point I don't get<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;Something happened here, but what? %s \n Status is %d \n\n&quot;, result, status);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit(1);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; else <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  wait(&amp;status);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  printf(&quot;Something else happened here %s \n Status after wait is %d&quot;, command, status);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  if (status != 0)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;And something happened again? \n&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;  <br />
&nbsp; &nbsp; &nbsp; &nbsp; result = strtok( NULL, delims );<br />
&nbsp; &nbsp; } <br />
}</pre><br />
The entire pid-fork-&amp;status structure is just too vague for me, can anyone explain what happens on every step of the way? <br />
<br />
Using execl, by the way, worked for me, but I have to use execv.<br />
<br />
<br />
Thanks</div> ]]></content:encoded>
			<category domain="http://www.daniweb.com/forums/forum118.html">C</category>
			<dc:creator>DimaYasny</dc:creator>
			<guid isPermaLink="true">http://www.daniweb.com/forums/thread240754.html</guid>
		</item>
	</channel>
</rss>
