blob: 604bd57bf24705aeb6334a4a0643aee17cd8ad42 [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
Elliott Hughese67f1f82015-04-30 17:32:03 -070017#define TRACE_TAG TRACE_ADB
18
Elliott Hughes58305772015-04-17 13:57:15 -070019#include "adb_utils.h"
20
Elliott Hughesa7090b92015-04-17 17:03:59 -070021#include <stdlib.h>
Elliott Hughes58305772015-04-17 13:57:15 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25
Elliott Hughese67f1f82015-04-30 17:32:03 -070026#include <algorithm>
27
28#include <base/stringprintf.h>
29
30#include "adb_trace.h"
Elliott Hughes53daee62015-04-19 13:17:01 -070031#include "sysdeps.h"
32
Elliott Hughesa7090b92015-04-17 17:03:59 -070033bool getcwd(std::string* s) {
34 char* cwd = getcwd(nullptr, 0);
35 if (cwd != nullptr) *s = cwd;
36 free(cwd);
37 return (cwd != nullptr);
38}
39
Elliott Hughes58305772015-04-17 13:57:15 -070040bool directory_exists(const std::string& path) {
41 struct stat sb;
42 return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode);
43}
44
Elliott Hughes58305772015-04-17 13:57:15 -070045std::string escape_arg(const std::string& s) {
Elliott Hughes5498ade2015-04-17 20:50:11 -070046 std::string result = s;
Elliott Hughes58305772015-04-17 13:57:15 -070047
Elliott Hughes84b0bf22015-05-15 12:06:00 -070048 // Escape any ' in the string (before we single-quote the whole thing).
49 // The correct way to do this for the shell is to replace ' with '\'' --- that is,
50 // close the existing single-quoted string, escape a single single-quote, and start
51 // a new single-quoted string. Like the C preprocessor, the shell will concatenate
52 // these pieces into one string.
53 for (size_t i = 0; i < s.size(); ++i) {
54 if (s[i] == '\'') {
55 result.insert(i, "'\\'");
56 i += 2;
57 }
Elliott Hughes58305772015-04-17 13:57:15 -070058 }
Elliott Hughes5498ade2015-04-17 20:50:11 -070059
60 // Prefix and suffix the whole string with '.
61 result.insert(result.begin(), '\'');
62 result.push_back('\'');
Elliott Hughes58305772015-04-17 13:57:15 -070063 return result;
64}
Elliott Hughese67f1f82015-04-30 17:32:03 -070065
66void dump_hex(const void* data, size_t byte_count) {
67 byte_count = std::min(byte_count, size_t(16));
68
69 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
70
71 std::string line;
72 for (size_t i = 0; i < byte_count; ++i) {
73 android::base::StringAppendF(&line, "%02x", p[i]);
74 }
75 line.push_back(' ');
76
77 for (size_t i = 0; i < byte_count; ++i) {
78 int c = p[i];
79 if (c < 32 || c > 127) {
80 c = '.';
81 }
82 line.push_back(c);
83 }
84
85 DR("%s\n", line.c_str());
86}