<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>bragr's Feed - Programmer's Heaven</title>
    <link>http://www.programmersheaven.com/feed/User/487738/RSS.aspx</link>
    <description>Events at Programmer's Heaven related to the user bragr.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Wed, 19 Jun 2013 21:59:23 -0700</pubDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <item>
      <title>Re: Simple Shell in C++</title>
      <link>http://www.programmersheaven.com/mb/Linux/419219/423214/ReadMessage.aspx#423214</link>
      <description>&lt;p&gt;Posted a '&lt;a href="http://www.programmersheaven.com/mb/Linux/419219/423214/ReadMessage.aspx#423214"&gt;reply&lt;/a&gt; on the &lt;a href="http://www.programmersheaven.com/mb/Linux/Board.aspx"&gt;LINUX programming&lt;/a&gt; forum.&lt;/p&gt;I did but if you go to ucr, I'm not going to give you code. :)&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Linux/419219/423214/ReadMessage.aspx#423214</guid>
      <pubDate>Thu, 14 Apr 2011 20:23:00 -0700</pubDate>
    </item>
    <item>
      <title>Re: implementing software raid 5 in c</title>
      <link>http://www.programmersheaven.com/mb/Linux/414685/419320/ReadMessage.aspx#419320</link>
      <description>&lt;p&gt;Posted a '&lt;a href="http://www.programmersheaven.com/mb/Linux/414685/419320/ReadMessage.aspx#419320"&gt;reply&lt;/a&gt; on the &lt;a href="http://www.programmersheaven.com/mb/Linux/Board.aspx"&gt;LINUX programming&lt;/a&gt; forum.&lt;/p&gt;&lt;a href="http://en.wikipedia.org/wiki/Mdadm"&gt;http://en.wikipedia.org/wiki/Mdadm&lt;/a&gt;&lt;br /&gt;
/thread&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Linux/414685/419320/ReadMessage.aspx#419320</guid>
      <pubDate>Mon, 25 Oct 2010 14:51:16 -0700</pubDate>
    </item>
    <item>
      <title>Re: help choosing a language</title>
      <link>http://www.programmersheaven.com/mb/Linux/418626/419318/ReadMessage.aspx#419318</link>
      <description>&lt;p&gt;Posted a '&lt;a href="http://www.programmersheaven.com/mb/Linux/418626/419318/ReadMessage.aspx#419318"&gt;reply&lt;/a&gt; on the &lt;a href="http://www.programmersheaven.com/mb/Linux/Board.aspx"&gt;LINUX programming&lt;/a&gt; forum.&lt;/p&gt;I would recommend c++, it will still teach you the c side of things, and still let you do system programing in linux, but you will also have the object oriented features to use, which I find to be helpful and easier to use for general purpose programming.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Linux/418626/419318/ReadMessage.aspx#419318</guid>
      <pubDate>Mon, 25 Oct 2010 14:47:00 -0700</pubDate>
    </item>
    <item>
      <title>Re: Simple Shell in C++</title>
      <link>http://www.programmersheaven.com/mb/Linux/419219/419317/ReadMessage.aspx#419317</link>
      <description>&lt;p&gt;Posted a '&lt;a href="http://www.programmersheaven.com/mb/Linux/419219/419317/ReadMessage.aspx#419317"&gt;reply&lt;/a&gt; on the &lt;a href="http://www.programmersheaven.com/mb/Linux/Board.aspx"&gt;LINUX programming&lt;/a&gt; forum.&lt;/p&gt;I found that my problem was that I was secretly seg faulting in the child, which was causing the child to silently fail. The code works if you substitute the strcpy() with "arglist[x] =  strdup(tok[x].c_str());" which actually allocates memory. The pipes are only needed for commands like "ps aux | grep bragr" in which the output of one child needs to be piped to the input of another.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Linux/419219/419317/ReadMessage.aspx#419317</guid>
      <pubDate>Mon, 25 Oct 2010 14:44:12 -0700</pubDate>
    </item>
    <item>
      <title>Simple Shell in C++</title>
      <link>http://www.programmersheaven.com/mb/Linux/419219/419219/ReadMessage.aspx#419219</link>
      <description>&lt;p&gt;Posted a '&lt;a href="http://www.programmersheaven.com/mb/Linux/419219/419219/ReadMessage.aspx#419219"&gt;new message&lt;/a&gt; on the &lt;a href="http://www.programmersheaven.com/mb/Linux/Board.aspx"&gt;LINUX programming&lt;/a&gt; forum.&lt;/p&gt;I am new to linux programming, and to get my bearings, I have been writing some small programs. I have already written a ls clone that will do ls, ls -l, and ls -lR. My current project is to write a simple shell. My understanding on how to do this is to loop the following: Get some input from the user and parse it into an array of cstrings, fork the program, have the child exec the user's command. I tried this, but I found that I lost i/o with the child after the exec, which make sense considering what it does. I did some reading online, I found that I need to use pipes to connect the child stdout and stdin to the parents before the exec. I did a few experiments with the pipes, but they all either crashed or didn't work, so I'm clearly missing something.&lt;br /&gt;
&lt;br /&gt;
Is my understanding of what I need to do correct? Am I missing some small, but important detail? Is it even possible to connect out pipe to the parent's stdout so the child's output is automatically output to the console, or do I need to manually read the pipe and output its contents. Can you give me some examples of using pipes in this context, preferably in C++? Thanks in advance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
My code so far:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;string&amp;gt;
#include &amp;lt;sstream&amp;gt;
#include &amp;lt;cstdlib&amp;gt;
#include &amp;lt;cstring&amp;gt;
#include &amp;lt;unistd.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;sys/wait.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;

const unsigned int MAX_ARGS = 128;

using namespace std;

// Execute a parsed command line returning the command's exit code
int doit(const vector&amp;lt;string&amp;gt;&amp;amp; tok)
{
    if (!tok.size() || tok[0] == "") return 0;
    // If user entered cd, change to specified dir. If no dir specified, change to the users home dir
    else if (tok[0] == "cd")
    {
        if (tok.size() &amp;gt; 1) chdir(tok[1].c_str());
        else chdir(getenv("HOME"));
        return 0;
    }

    //Else execute the command specified by the user
    if (pid_t kidpid = fork())
    {
        //Parent
        int status = 0;
        if (tok.back().at(tok.back().size() - 1) != '&amp;amp;')
        {
            waitpid(kidpid, &amp;amp;status, 0);

            #ifdef _INSPECT_EXIT_STATUS
                cout &amp;lt;&amp;lt; "exit status=" &amp;lt;&amp;lt; status &amp;lt;&amp;lt; endl;
            #endif //_INSPECT_EXIT_STATUS
        }
        return status;
    }
    //Child - execute the command
    char* arglist[MAX_ARGS];
    for (unsigned int x = 0; x &amp;lt; tok.size() &amp;amp;&amp;amp; x &amp;lt; MAX_ARGS - 1; x++)
        strcpy(arglist[x], tok[x].c_str());
    arglist[tok.size()] = NULL;

    execvp(tok[0].c_str(), arglist);

    //Program will never reach here unless execvp failed
    cerr &amp;lt;&amp;lt; "execpv failed: " &amp;lt;&amp;lt; strerror(errno) &amp;lt;&amp;lt; endl;
    exit(errno);
}

int main(int argc, char* argv[], char* envp[])
{
    while (!cin.eof())
    {
        cout &amp;lt;&amp;lt; "? ";
        string temp;
        getline(cin, temp);
        if (temp == "exit") break;

        vector&amp;lt;string&amp;gt; v;
        //Break string into separate strings on whitespace
        {
            stringstream foo(temp);
            string s;
            while (foo &amp;gt;&amp;gt; s)
            {
                if (s[0]=='~') s = getenv("HOME") + s.substr(1);
                v.push_back(s);
            }
        }
        doit(v);
    }
    cout &amp;lt;&amp;lt; "exit" &amp;lt;&amp;lt; endl;
    return 0;
}&lt;/pre&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/Linux/419219/419219/ReadMessage.aspx#419219</guid>
      <pubDate>Tue, 19 Oct 2010 14:02:53 -0700</pubDate>
    </item>
  </channel>
</rss>