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/detail/posix_helpers.hpp
|
ian@0
|
16 *
|
ian@0
|
17 * Includes the declaration of helper functions for POSIX systems.
|
ian@0
|
18 */
|
ian@0
|
19
|
ian@0
|
20 #ifndef BOOST_PROCESS_POSIX_HELPERS_HPP
|
ian@0
|
21 #define BOOST_PROCESS_POSIX_HELPERS_HPP
|
ian@0
|
22
|
ian@0
|
23 #include <boost/process/config.hpp>
|
ian@0
|
24 #include <boost/process/environment.hpp>
|
ian@0
|
25 #include <string>
|
ian@0
|
26 #include <utility>
|
ian@0
|
27 #include <cstring>
|
ian@0
|
28 #include <cstddef>
|
ian@0
|
29
|
ian@0
|
30 namespace boost {
|
ian@0
|
31 namespace process {
|
ian@0
|
32 namespace detail {
|
ian@0
|
33
|
ian@0
|
34 /**
|
ian@0
|
35 * Converts an environment to a char** table as used by execve().
|
ian@0
|
36 *
|
ian@0
|
37 * Converts the environment's contents to the format used by the
|
ian@0
|
38 * execve() system call. The returned char** array is allocated
|
ian@0
|
39 * in dynamic memory; the caller must free it when not used any
|
ian@0
|
40 * more. Each entry is also allocated in dynamic memory and is a
|
ian@0
|
41 * NULL-terminated string of the form var=value; these must also be
|
ian@0
|
42 * released by the caller.
|
ian@0
|
43 *
|
ian@0
|
44 * This operation is only available on POSIX systems.
|
ian@0
|
45 *
|
ian@0
|
46 * \return The first argument of the pair is an integer that indicates
|
ian@0
|
47 * how many strings are stored in the second argument. The
|
ian@0
|
48 * second argument is a NULL-terminated, dynamically allocated
|
ian@0
|
49 * array of dynamically allocated strings representing the
|
ian@0
|
50 * enviroment's content. Each array entry is a NULL-terminated
|
ian@0
|
51 * string of the form var=value. The caller is responsible for
|
ian@0
|
52 * freeing them.
|
ian@0
|
53 */
|
ian@0
|
54 inline std::pair<std::size_t, char**> environment_to_envp(const environment
|
ian@0
|
55 &env)
|
ian@0
|
56 {
|
ian@0
|
57 std::size_t nargs = env.size();
|
ian@0
|
58 char **envp = new char*[nargs + 1];
|
ian@0
|
59 environment::size_type i = 0;
|
ian@0
|
60 for (environment::const_iterator it = env.begin(); it != env.end(); ++it)
|
ian@0
|
61 {
|
ian@0
|
62 std::string s = it->first + "=" + it->second;
|
ian@0
|
63 envp[i] = new char[s.size() + 1];
|
ian@0
|
64 std::strncpy(envp[i], s.c_str(), s.size() + 1);
|
ian@0
|
65 ++i;
|
ian@0
|
66 }
|
ian@0
|
67 envp[i] = 0;
|
ian@0
|
68 return std::pair<std::size_t, char**>(nargs, envp);
|
ian@0
|
69 }
|
ian@0
|
70
|
ian@0
|
71 /**
|
ian@0
|
72 * Converts the command line to an array of C strings.
|
ian@0
|
73 *
|
ian@0
|
74 * Converts the command line's list of arguments to the format expected
|
ian@0
|
75 * by the \a argv parameter in the POSIX execve() system call.
|
ian@0
|
76 *
|
ian@0
|
77 * This operation is only available on POSIX systems.
|
ian@0
|
78 *
|
ian@0
|
79 * \return The first argument of the pair is an integer that indicates
|
ian@0
|
80 * how many strings are stored in the second argument. The
|
ian@0
|
81 * second argument is a NULL-terminated, dynamically allocated
|
ian@0
|
82 * array of dynamically allocated strings holding the arguments
|
ian@0
|
83 * to the executable. The caller is responsible for freeing them.
|
ian@0
|
84 */
|
ian@0
|
85 template <class Arguments>
|
ian@0
|
86 inline std::pair<std::size_t, char**> collection_to_argv(const Arguments &args)
|
ian@0
|
87 {
|
ian@0
|
88 std::size_t nargs = args.size();
|
ian@0
|
89 char **argv = new char*[nargs + 1];
|
ian@0
|
90 typename Arguments::size_type i = 0;
|
ian@0
|
91 for (typename Arguments::const_iterator it = args.begin(); it != args.end();
|
ian@0
|
92 ++it)
|
ian@0
|
93 {
|
ian@0
|
94 argv[i] = new char[it->size() + 1];
|
ian@0
|
95 std::strncpy(argv[i], it->c_str(), it->size() + 1);
|
ian@0
|
96 ++i;
|
ian@0
|
97 }
|
ian@0
|
98 argv[nargs] = 0;
|
ian@0
|
99 return std::pair<std::size_t, char**>(nargs, argv);
|
ian@0
|
100 }
|
ian@0
|
101
|
ian@0
|
102 }
|
ian@0
|
103 }
|
ian@0
|
104 }
|
ian@0
|
105
|
ian@0
|
106 #endif
|