COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Cosmos::Support::Command Namespace Reference

Functions

int32_t Shell (string command_line, string outpath, string inpath, string errpath)
 

Function Documentation

int32_t Cosmos::Support::Command::Shell ( string  command_line,
string  outpath,
string  inpath,
string  errpath 
)
44  {
45  int32_t iretn=0;
46  int devin, devout, deverr;
47  int prev_stdin, prev_stdout, prev_stderr;
48 
49  if (command_line.empty())
50  {
51  return GENERAL_ERROR_EMPTY;
52  }
53 
54  if (outpath.empty())
55  {
56  devout = dup(STDOUT_FILENO);
57  }
58  else
59  {
60  devout = open(outpath.c_str(), O_CREAT|O_WRONLY|O_APPEND, 00666);
61  }
62  // Redirect.
63  prev_stdout = dup(STDOUT_FILENO);
64  dup2(devout, STDOUT_FILENO);
65  close(devout);
66 
67  if (inpath.empty())
68  {
69  devin = open("/dev/null", O_RDWR);
70  }
71  else
72  {
73  devin = open(inpath.c_str(), O_RDONLY, 00666);
74  }
75  prev_stdin = dup(STDIN_FILENO);
76  dup2(devin, STDIN_FILENO);
77  close(devin);
78 
79  prev_stderr = dup(STDERR_FILENO);
80  if (errpath.empty())
81  {
82  deverr = devout;
83  }
84  else
85  {
86  deverr = open(errpath.c_str(), O_CREAT|O_WRONLY|O_APPEND, 00666);
87  }
88  dup2(deverr, STDERR_FILENO);
89  close(deverr);
90 
91  // Execute the command.
92  iretn = system(command_line.c_str());
93 
94  // Reset standard file handles
95  dup2(prev_stdin, STDIN_FILENO);
96  dup2(prev_stdout, STDOUT_FILENO);
97  dup2(prev_stderr, STDERR_FILENO);
98  close(prev_stdin);
99  close(prev_stdout);
100  close(prev_stderr);
101 
102  return iretn;
103  }
int iretn
Definition: rw_test.cpp:37
#define GENERAL_ERROR_EMPTY
Definition: cosmos-errno.h:288