ATLAS Offline Software
Loading...
Searching...
No Matches
ShellExec.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7//
8// includes
9//
10
12
13#include <cstdio>
14#include <vector>
15#include <TSystem.h>
17
18//
19// method implementations
20//
21
22namespace RCU
23{
24 namespace Shell
25 {
26 void exec (const std::string& cmd)
27 {
28 if (gSystem->Exec (cmd.c_str()) != 0)
29 RCU_THROW_MSG ("command failed: " + cmd);
30 }
31
32
33
34 std::string exec_read (const std::string& cmd)
35 {
36 int rc = 0;
37 std::string result = exec_read (cmd, rc);
38 if (rc != 0)
39 RCU_THROW_MSG ("command failed: " + cmd + "\nwith output:\n" + result);
40 return result;
41 }
42
43
44
45 std::string exec_read (const std::string& cmd, int& rc)
46 {
47 std::string result;
48 FILE *pipe = 0;
49 try
50 {
51 std::vector<char> buffer (1024);
52 size_t read;
53
54 pipe = popen (cmd.c_str(), "r");
55 while ((read = fread (&buffer[0], 1, buffer.size(), pipe)) > 0)
56 {
57 result.append (&buffer[0], read);
58 }
59 rc = pclose (pipe);
60 pipe = nullptr;
61 return result;
62 } catch (...)
63 {
64 if (pipe)
65 rc = pclose (pipe);
66 throw;
67 }
68 }
69
70
71
72 std::string quote (const std::string& name)
73 {
74 std::string result;
75 for (std::string::const_iterator iter = name.begin(),
76 end = name.end(); iter != end; ++ iter)
77 {
78 if (!isalnum (*iter) && *iter != '/' && *iter != '.' &&
79 *iter != '-')
80 result += '\\';
81 result += *iter;
82 };
83 if (result.empty())
84 result = "\"\"";
85 return result;
86 }
87 }
88}
static Double_t rc
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:53
std::string exec_read(const std::string &cmd)
effects: execute the given command and return the output returns: the output of the command guarantee...
Definition ShellExec.cxx:34
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
Definition ShellExec.cxx:26
std::string quote(const std::string &name)
effects: quote the given name to protect it from the shell returns: the quoted name guarantee: strong...
Definition ShellExec.cxx:72
This module defines a variety of assert style macros.
Definition Assert.cxx:23
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)