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 <memory>
15#include <stdexcept>
16#include <vector>
17#include <TSystem.h>
18
19//
20// method implementations
21//
22
23namespace RCU
24{
25 namespace Shell
26 {
27 void exec (const std::string& cmd)
28 {
29 if (gSystem->Exec (cmd.c_str()) != 0)
30 throw std::runtime_error ("command failed: " + cmd);
31 }
32
33
34
35 std::string exec_read (const std::string& cmd)
36 {
37 int rc = 0;
38 std::string result = exec_read (cmd, rc);
39 if (rc != 0)
40 throw std::runtime_error ("command failed: " + cmd + "\nwith output:\n" + result);
41 return result;
42 }
43
44
45
46 std::string exec_read (const std::string& cmd, int& rc)
47 {
48 std::unique_ptr<FILE, int (*)(FILE*)> pipe (popen (cmd.c_str(), "r"), pclose);
49 if (pipe == nullptr)
50 throw std::runtime_error ("failed to run command: " + cmd);
51
52 std::string result;
53 std::vector<char> buffer (1024);
54 size_t read;
55 while ((read = fread (&buffer[0], 1, buffer.size(), pipe.get())) > 0)
56 {
57 result.append (&buffer[0], read);
58 }
59 rc = pclose (pipe.release());
60 return result;
61 }
62
63
64
65 std::string quote (const std::string& name)
66 {
67 // wrap the argument in single quotes, which protects every
68 // character from the shell (including newlines); an embedded
69 // single quote is emitted as the standard '\'' sequence
70 std::string result = "'";
71 for (char c : name)
72 {
73 if (c == '\'')
74 result += "'\\''";
75 else
76 result += c;
77 }
78 result += "'";
79 return result;
80 }
81 }
82}
static Double_t rc
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:35
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
Definition ShellExec.cxx:27
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:65
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)