blob: f10c143ac4b00a3aeb11bd91f06b01f22cef56c6 [file] [log] [blame]
Elliott Hughes58305772015-04-17 13:57:15 -07001/*
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 Hughesa7090b92015-04-17 17:03:59 -070019#include <stdlib.h>
Elliott Hughes58305772015-04-17 13:57:15 -070020#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
Elliott Hughes53daee62015-04-19 13:17:01 -070024#include "sysdeps.h"
25
Elliott Hughesa7090b92015-04-17 17:03:59 -070026bool getcwd(std::string* s) {
27 char* cwd = getcwd(nullptr, 0);
28 if (cwd != nullptr) *s = cwd;
29 free(cwd);
30 return (cwd != nullptr);
31}
32
Elliott Hughes58305772015-04-17 13:57:15 -070033bool directory_exists(const std::string& path) {
34 struct stat sb;
35 return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode);
36}
37
Elliott Hughes58305772015-04-17 13:57:15 -070038std::string escape_arg(const std::string& s) {
Elliott Hughes5498ade2015-04-17 20:50:11 -070039 std::string result = s;
Elliott Hughes58305772015-04-17 13:57:15 -070040
Elliott Hughes5498ade2015-04-17 20:50:11 -070041 // Insert a \ before any ' in the string.
Elliott Hughes58305772015-04-17 13:57:15 -070042 for (auto it = result.begin(); it != result.end(); ++it) {
Elliott Hughes5498ade2015-04-17 20:50:11 -070043 if (*it == '\'') {
Elliott Hughes58305772015-04-17 13:57:15 -070044 it = result.insert(it, '\\') + 1;
45 }
46 }
Elliott Hughes5498ade2015-04-17 20:50:11 -070047
48 // Prefix and suffix the whole string with '.
49 result.insert(result.begin(), '\'');
50 result.push_back('\'');
Elliott Hughes58305772015-04-17 13:57:15 -070051 return result;
52}