cmdline.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * libtest
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <libtest/common.h>
  22. using namespace libtest;
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <string>
  26. #include <sstream>
  27. extern "C" {
  28. static int exited_successfully(int status)
  29. {
  30. if (status == 0)
  31. {
  32. return EXIT_SUCCESS;
  33. }
  34. if (WIFEXITED(status) == true)
  35. {
  36. return WEXITSTATUS(status);
  37. }
  38. else if (WIFSIGNALED(status) == true)
  39. {
  40. return WTERMSIG(status);
  41. }
  42. return -1;
  43. }
  44. }
  45. namespace libtest {
  46. int exec_cmdline(const std::string& executable, const char *args[])
  47. {
  48. std::stringstream arg_buffer;
  49. arg_buffer << libtool();
  50. if (getenv("PWD"))
  51. {
  52. arg_buffer << getenv("PWD");
  53. arg_buffer << "/";
  54. }
  55. arg_buffer << executable;
  56. for (const char **ptr= args; *ptr; ++ptr)
  57. {
  58. arg_buffer << " " << *ptr;
  59. }
  60. int ret= system(arg_buffer.str().c_str());
  61. return exited_successfully(ret);
  62. }
  63. const char *gearmand_binary()
  64. {
  65. return GEARMAND_BINARY;
  66. }
  67. } // namespace exec_cmdline