adb_utils.cpp revision 5830577bd82fdb7c39555da20a4cf585b8bb376a
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "adb_utils.h" 18 19#include <sys/stat.h> 20#include <sys/types.h> 21#include <unistd.h> 22 23bool directory_exists(const std::string& path) { 24 struct stat sb; 25 return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); 26} 27 28static bool should_escape(const char c) { 29 return (c == ' ' || c == '\'' || c == '"' || c == '\\' || c == '(' || c == ')'); 30} 31 32std::string escape_arg(const std::string& s) { 33 // Preserve empty arguments. 34 if (s.empty()) return "\"\""; 35 36 std::string result(s); 37 for (auto it = result.begin(); it != result.end(); ++it) { 38 if (should_escape(*it)) { 39 it = result.insert(it, '\\') + 1; 40 } 41 } 42 return result; 43} 44