search_path.hpp
Go to the documentation of this file.
1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 
10 #pragma once
11 
12 #include <boost/process/config.hpp>
13 #include <filesystem>
14 #include <boost/tokenizer.hpp>
15 #include <string>
16 #include <stdexcept>
17 #include <stdlib.h>
18 #include <unistd.h>
19 
20 namespace boost::process::posix
21 {
22 
23  inline std::string search_path(const std::string& filename,
24  std::string path = "")
25  {
26  if (path.empty())
27  {
28  path = ::getenv("PATH");
29  if (path.empty())
30  BOOST_PROCESS_THROW(std::runtime_error(
31  "Environment variable PATH not found"));
32  }
33 
34  std::string result;
35  using tokenizer = boost::tokenizer<boost::char_separator<char> >;
36  boost::char_separator<char> sep(":");
37  tokenizer tok(path, sep);
38  for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
39  {
40  std::filesystem::path p = *it;
41  p /= filename;
42  if (!::access(p.c_str(), X_OK))
43  {
44  result = p.string();
45  break;
46  }
47  }
48  return result;
49  }
50 
51 }
config.hpp
BOOST_PROCESS_THROW
#define BOOST_PROCESS_THROW(EX)
Definition: config.hpp:64
filename
std::string filename
Definition: VisualizationRobot.cpp:84
boost::process::posix::search_path
std::string search_path(const std::string &filename, std::string path="")
Definition: search_path.hpp:23
boost::process::posix
Definition: child.hpp:14