MCPcopy Create free account
hub / github.com/Icinga/icinga2 / EscapeShellCmd

Method EscapeShellCmd

lib/base/utility.cpp:1183–1227  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1181}
1182
1183String Utility::EscapeShellCmd(const String& s)
1184{
1185 String result;
1186 size_t prev_quote = String::NPos;
1187 int index = -1;
1188
1189 for (char ch : s) {
1190 bool escape = false;
1191
1192 index++;
1193
1194#ifdef _WIN32
1195 if (ch == '%' || ch == '"' || ch == '\'')
1196 escape = true;
1197#else /* _WIN32 */
1198 if (ch == '"' || ch == '\'') {
1199 /* Find a matching closing quotation character. */
1200 if (prev_quote == String::NPos && (prev_quote = s.FindFirstOf(ch, index + 1)) != String::NPos)
1201 ; /* Empty statement. */
1202 else if (prev_quote != String::NPos && s[prev_quote] == ch)
1203 prev_quote = String::NPos;
1204 else
1205 escape = true;
1206 }
1207#endif /* _WIN32 */
1208
1209 if (ch == '#' || ch == '&' || ch == ';' || ch == '`' || ch == '|' ||
1210 ch == '*' || ch == '?' || ch == '~' || ch == '<' || ch == '>' ||
1211 ch == '^' || ch == '(' || ch == ')' || ch == '[' || ch == ']' ||
1212 ch == '{' || ch == '}' || ch == '$' || ch == '\\' || ch == '\x0A' ||
1213 ch == '\xFF')
1214 escape = true;
1215
1216 if (escape)
1217#ifdef _WIN32
1218 result += '^';
1219#else /* _WIN32 */
1220 result += '\\';
1221#endif /* _WIN32 */
1222
1223 result += ch;
1224 }
1225
1226 return result;
1227}
1228
1229String Utility::EscapeShellArg(const String& s)
1230{

Callers

nothing calls this directly

Calls 1

FindFirstOfMethod · 0.80

Tested by

no test coverage detected