ian@0: // ian@0: // Boost.Process ian@0: // ~~~~~~~~~~~~~ ian@0: // ian@0: // Copyright (c) 2006, 2007 Julio M. Merino Vidal ian@0: // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling ian@0: // Copyright (c) 2009 Boris Schaeling ian@0: // Copyright (c) 2010 Felipe Tanus, Boris Schaeling ian@0: // ian@0: // Distributed under the Boost Software License, Version 1.0. (See accompanying ian@0: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ian@0: // ian@0: ian@0: /** ian@0: * \file boost/process/detail/posix_helpers.hpp ian@0: * ian@0: * Includes the declaration of helper functions for POSIX systems. ian@0: */ ian@0: ian@0: #ifndef BOOST_PROCESS_POSIX_HELPERS_HPP ian@0: #define BOOST_PROCESS_POSIX_HELPERS_HPP ian@0: ian@0: #include ian@0: #include ian@0: #include ian@0: #include ian@0: #include ian@0: #include ian@0: ian@0: namespace boost { ian@0: namespace process { ian@0: namespace detail { ian@0: ian@0: /** ian@0: * Converts an environment to a char** table as used by execve(). ian@0: * ian@0: * Converts the environment's contents to the format used by the ian@0: * execve() system call. The returned char** array is allocated ian@0: * in dynamic memory; the caller must free it when not used any ian@0: * more. Each entry is also allocated in dynamic memory and is a ian@0: * NULL-terminated string of the form var=value; these must also be ian@0: * released by the caller. ian@0: * ian@0: * This operation is only available on POSIX systems. ian@0: * ian@0: * \return The first argument of the pair is an integer that indicates ian@0: * how many strings are stored in the second argument. The ian@0: * second argument is a NULL-terminated, dynamically allocated ian@0: * array of dynamically allocated strings representing the ian@0: * enviroment's content. Each array entry is a NULL-terminated ian@0: * string of the form var=value. The caller is responsible for ian@0: * freeing them. ian@0: */ ian@0: inline std::pair environment_to_envp(const environment ian@0: &env) ian@0: { ian@0: std::size_t nargs = env.size(); ian@0: char **envp = new char*[nargs + 1]; ian@0: environment::size_type i = 0; ian@0: for (environment::const_iterator it = env.begin(); it != env.end(); ++it) ian@0: { ian@0: std::string s = it->first + "=" + it->second; ian@0: envp[i] = new char[s.size() + 1]; ian@0: std::strncpy(envp[i], s.c_str(), s.size() + 1); ian@0: ++i; ian@0: } ian@0: envp[i] = 0; ian@0: return std::pair(nargs, envp); ian@0: } ian@0: ian@0: /** ian@0: * Converts the command line to an array of C strings. ian@0: * ian@0: * Converts the command line's list of arguments to the format expected ian@0: * by the \a argv parameter in the POSIX execve() system call. ian@0: * ian@0: * This operation is only available on POSIX systems. ian@0: * ian@0: * \return The first argument of the pair is an integer that indicates ian@0: * how many strings are stored in the second argument. The ian@0: * second argument is a NULL-terminated, dynamically allocated ian@0: * array of dynamically allocated strings holding the arguments ian@0: * to the executable. The caller is responsible for freeing them. ian@0: */ ian@0: template ian@0: inline std::pair collection_to_argv(const Arguments &args) ian@0: { ian@0: std::size_t nargs = args.size(); ian@0: char **argv = new char*[nargs + 1]; ian@0: typename Arguments::size_type i = 0; ian@0: for (typename Arguments::const_iterator it = args.begin(); it != args.end(); ian@0: ++it) ian@0: { ian@0: argv[i] = new char[it->size() + 1]; ian@0: std::strncpy(argv[i], it->c_str(), it->size() + 1); ian@0: ++i; ian@0: } ian@0: argv[nargs] = 0; ian@0: return std::pair(nargs, argv); ian@0: } ian@0: ian@0: } ian@0: } ian@0: } ian@0: ian@0: #endif