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