title: Determine program path from leafname and PATH environment variable in C++
body: <p>Today I had the need to implement a task in my C++ program which is easy from the shell, to whit:</p>
  
  <pre><code>% which foo
  /usr/local/bin/foo
  </code></pre>
  
  <p>Here is a sketch of my solution:</p>
  
  <pre><code>QString leafName("foo");
  
  QString resolvedPath;
  QString paths(qgetenv("PATH"));
  QStringList components = paths.split(':');
  QStringList::iterator iter, e_iter = components.end();
  for (iter = components.begin(); iter != e_iter; ++iter)
  {
     QString aPath(*iter);
     aPath += "/";
     aPath += leafName;
  
     QFileInfo fInfo(aPath);
     if (fInfo.exists())
     {
        resolvedPath = fInfo.canonicalFilePath();
        break;
     }
  }
  if (resolvedPath.isEmpty()) {
     resolvedPath = leafName;
  }
  // inform user of path we intend to execute from
  </code></pre>
  
  <p>I only wrote this code after searching in vain for a canned version in Boost filesystem and Qt, as well as Googling. The Google matches all thought I was asking for the full path of my own running program.</p>
  
  <p>This seems like a reasonable atom of functionality, such that it is silly to write it myself. Does it exist?</p>