Leosac  0.7.0
OpenSourceAccessControl
unixshellscript.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #include "unixshellscript.hpp"
27 
28 extern "C" {
29 #include <stdio.h>
30 }
31 
32 #include <sstream>
33 
35 #include "tools/log.hpp"
36 #include "unixfs.hpp"
37 #include "unixsyscall.hpp"
38 
39 using namespace Leosac::Tools;
40 
41 UnixShellScript::UnixShellScript(const std::string &script)
42  : _script(script)
43 {
44 }
45 
46 int UnixShellScript::run(const std::string &args)
47 {
48  FILE *stream;
49  std::ostringstream oss;
50  char buffer[BufferSize];
51  std::string line = _script + ' ' + args;
52  int ret;
53 
54  INFO("CmdLine: " << line);
55  if (!(stream = popen(line.c_str(), "r")))
56  throw(ScriptException(UnixSyscall::getErrorString("popen", errno) +
57  " command: '" + line + '\''));
58  while (fgets(buffer, BufferSize, stream))
59  oss << buffer;
60  _output = oss.str();
61  if ((ret = pclose(stream)) == -1 && errno)
62  throw(ScriptException(UnixSyscall::getErrorString("pclose", errno)));
63  return (ret);
64 }
65 
66 const std::string &UnixShellScript::getOutput() const
67 {
68  return (_output);
69 }
static std::string getErrorString(const std::string &func, int errNo)
Definition: unixsyscall.cpp:32
int run(const std::string &args=std::string())
Definition: log.hpp:37
static const std::size_t BufferSize
UnixShellScript class declaration.
unix syscall helper functions
unix filesystem helper functions
const std::string & getOutput() const
UnixShellScript(const std::string &script)
Exception class for Script related errors.