Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
Elliott Hughes | 7cf3575 | 2015-04-17 17:03:59 -0700 | [diff] [blame] | 24 | bool getcwd(std::string* s) { |
| 25 | char* cwd = getcwd(nullptr, 0); |
| 26 | if (cwd != nullptr) *s = cwd; |
| 27 | free(cwd); |
| 28 | return (cwd != nullptr); |
| 29 | } |
| 30 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 31 | bool directory_exists(const std::string& path) { |
| 32 | struct stat sb; |
| 33 | return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); |
| 34 | } |
| 35 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 36 | std::string escape_arg(const std::string& s) { |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame^] | 37 | std::string result = s; |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame^] | 39 | // Insert a \ before any ' in the string. |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 40 | for (auto it = result.begin(); it != result.end(); ++it) { |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame^] | 41 | if (*it == '\'') { |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 42 | it = result.insert(it, '\\') + 1; |
| 43 | } |
| 44 | } |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame^] | 45 | |
| 46 | // Prefix and suffix the whole string with '. |
| 47 | result.insert(result.begin(), '\''); |
| 48 | result.push_back('\''); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 49 | return result; |
| 50 | } |