annotate third_party/boost/process/self.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
rev   line source
ian@0 1 //
ian@0 2 // Boost.Process
ian@0 3 // ~~~~~~~~~~~~~
ian@0 4 //
ian@0 5 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
ian@0 6 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
ian@0 7 // Copyright (c) 2009 Boris Schaeling
ian@0 8 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
ian@0 9 //
ian@0 10 // Distributed under the Boost Software License, Version 1.0. (See accompanying
ian@0 11 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
ian@0 12 //
ian@0 13
ian@0 14 /**
ian@0 15 * \file boost/process/self.hpp
ian@0 16 *
ian@0 17 * Includes the declaration of the self class.
ian@0 18 */
ian@0 19
ian@0 20 #ifndef BOOST_PROCESS_SELF_HPP
ian@0 21 #define BOOST_PROCESS_SELF_HPP
ian@0 22
ian@0 23 #include <boost/process/config.hpp>
ian@0 24
ian@0 25 #if defined(BOOST_POSIX_API)
ian@0 26 # include <boost/scoped_array.hpp>
ian@0 27 # include <errno.h>
ian@0 28 # include <unistd.h>
ian@0 29 # include <limits.h>
ian@0 30 # if defined(__APPLE__)
ian@0 31 # include <crt_externs.h>
ian@0 32 # endif
ian@0 33 #elif defined(BOOST_WINDOWS_API)
ian@0 34 # include <windows.h>
ian@0 35 #else
ian@0 36 # error "Unsupported platform."
ian@0 37 #endif
ian@0 38
ian@0 39 #include <boost/process/process.hpp>
ian@0 40 #include <boost/process/environment.hpp>
ian@0 41 #include <boost/noncopyable.hpp>
ian@0 42 #include <boost/assert.hpp>
ian@0 43 #include <string>
ian@0 44
ian@0 45 #if defined(BOOST_POSIX_API)
ian@0 46 extern "C"
ian@0 47 {
ian@0 48 extern char **environ;
ian@0 49 }
ian@0 50 #endif
ian@0 51
ian@0 52 namespace boost {
ian@0 53 namespace process {
ian@0 54
ian@0 55 /**
ian@0 56 * The self class provides access to the process itself.
ian@0 57 */
ian@0 58 class self : public process, public boost::noncopyable
ian@0 59 {
ian@0 60 public:
ian@0 61 /**
ian@0 62 * Returns the self instance representing the caller's process.
ian@0 63 */
ian@0 64 static self &get_instance()
ian@0 65 {
ian@0 66 static self *instance = 0;
ian@0 67 if (!instance)
ian@0 68 instance = new self;
ian@0 69 return *instance;
ian@0 70 }
ian@0 71
ian@0 72 /**
ian@0 73 * Returns the current environment.
ian@0 74 *
ian@0 75 * Returns the current process environment variables. Modifying the
ian@0 76 * returned object has no effect on the current environment.
ian@0 77 */
ian@0 78 static environment get_environment()
ian@0 79 {
ian@0 80 environment e;
ian@0 81
ian@0 82 #if defined(BOOST_POSIX_API)
ian@0 83 # if defined(__APPLE__)
ian@0 84 char **env = *_NSGetEnviron();
ian@0 85 # else
ian@0 86 char **env = environ;
ian@0 87 # endif
ian@0 88
ian@0 89 while (*env)
ian@0 90 {
ian@0 91 std::string s = *env;
ian@0 92 std::string::size_type pos = s.find('=');
ian@0 93 e.insert(environment::value_type(s.substr(0, pos),
ian@0 94 s.substr(pos + 1)));
ian@0 95 ++env;
ian@0 96 }
ian@0 97 #elif defined(BOOST_WINDOWS_API)
ian@0 98 # ifdef GetEnvironmentStrings
ian@0 99 # undef GetEnvironmentStrings
ian@0 100 # endif
ian@0 101
ian@0 102 char *ms_environ = GetEnvironmentStrings();
ian@0 103 if (!ms_environ)
ian@0 104 BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
ian@0 105 "GetEnvironmentStrings() failed");
ian@0 106 try
ian@0 107 {
ian@0 108 char *env = ms_environ;
ian@0 109 while (*env)
ian@0 110 {
ian@0 111 std::string s = env;
ian@0 112 std::string::size_type pos = s.find('=');
ian@0 113 e.insert(environment::value_type(s.substr(0, pos),
ian@0 114 s.substr(pos + 1)));
ian@0 115 env += s.size() + 1;
ian@0 116 }
ian@0 117 }
ian@0 118 catch (...)
ian@0 119 {
ian@0 120 FreeEnvironmentStringsA(ms_environ);
ian@0 121 throw;
ian@0 122 }
ian@0 123 FreeEnvironmentStringsA(ms_environ);
ian@0 124 #endif
ian@0 125
ian@0 126 return e;
ian@0 127 }
ian@0 128
ian@0 129 /**
ian@0 130 * Returns the current work directory.
ian@0 131 */
ian@0 132 static std::string get_work_dir()
ian@0 133 {
ian@0 134 #if defined(BOOST_POSIX_API)
ian@0 135 #if defined(PATH_MAX)
ian@0 136 char buffer[PATH_MAX];
ian@0 137 char *cwd = buffer;
ian@0 138 long size = PATH_MAX;
ian@0 139 #elif defined(_PC_PATH_MAX)
ian@0 140 errno = 0;
ian@0 141 long size = pathconf("/", _PC_PATH_MAX);
ian@0 142 if (size == -1 && errno)
ian@0 143 BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("pathconf(2) failed");
ian@0 144 else if (size == -1)
ian@0 145 size = BOOST_PROCESS_POSIX_PATH_MAX;
ian@0 146 BOOST_ASSERT(size > 0);
ian@0 147 boost::scoped_array<char> buffer(new char[size]);
ian@0 148 char *cwd = buffer.get();
ian@0 149 #else
ian@0 150 char buffer[BOOST_PROCESS_POSIX_PATH_MAX];
ian@0 151 char *cwd = buffer;
ian@0 152 long size = BOOST_PROCESS_POSIX_PATH_MAX;
ian@0 153 #endif
ian@0 154 if (!getcwd(cwd, size))
ian@0 155 BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR("getcwd(2) failed");
ian@0 156 BOOST_ASSERT(cwd[0] != '\0');
ian@0 157 return cwd;
ian@0 158 #elif defined(BOOST_WINDOWS_API)
ian@0 159 BOOST_ASSERT(MAX_PATH > 0);
ian@0 160 char cwd[MAX_PATH];
ian@0 161 if (!GetCurrentDirectoryA(sizeof(cwd), cwd))
ian@0 162 BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(
ian@0 163 "GetCurrentDirectory() failed");
ian@0 164 BOOST_ASSERT(cwd[0] != '\0');
ian@0 165 return cwd;
ian@0 166 #endif
ian@0 167 }
ian@0 168
ian@0 169 private:
ian@0 170 /**
ian@0 171 * Constructs a new self object.
ian@0 172 *
ian@0 173 * Creates a new self object that represents the current process.
ian@0 174 */
ian@0 175 self() :
ian@0 176 #if defined(BOOST_POSIX_API)
ian@0 177 process(getpid())
ian@0 178 #elif defined(BOOST_WINDOWS_API)
ian@0 179 process(GetCurrentProcessId())
ian@0 180 #endif
ian@0 181 {
ian@0 182 }
ian@0 183 };
ian@0 184
ian@0 185 }
ian@0 186 }
ian@0 187
ian@0 188 #endif