adb_utils.cpp revision a7090b94c181f3efe5b53d2c8367b78d99074dfe
15830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes/*
25830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * Copyright (C) 2015 The Android Open Source Project
35830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes *
45830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
55830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * you may not use this file except in compliance with the License.
65830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * You may obtain a copy of the License at
75830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes *
85830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
95830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes *
105830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * Unless required by applicable law or agreed to in writing, software
115830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
125830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * See the License for the specific language governing permissions and
145830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * limitations under the License.
155830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes */
165830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes
175830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include "adb_utils.h"
185830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes
19a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes#include <stdlib.h>
205830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include <sys/stat.h>
215830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include <sys/types.h>
225830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include <unistd.h>
235830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes
24a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughesbool getcwd(std::string* s) {
25a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes  char* cwd = getcwd(nullptr, 0);
26a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes  if (cwd != nullptr) *s = cwd;
27a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes  free(cwd);
28a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes  return (cwd != nullptr);
29a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes}
30a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes
315830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughesbool directory_exists(const std::string& path) {
325830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  struct stat sb;
335830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode);
345830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes}
355830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes
365830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughesstatic bool should_escape(const char c) {
375830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  return (c == ' ' || c == '\'' || c == '"' || c == '\\' || c == '(' || c == ')');
385830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes}
395830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes
405830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughesstd::string escape_arg(const std::string& s) {
415830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  // Preserve empty arguments.
425830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  if (s.empty()) return "\"\"";
435830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes
445830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  std::string result(s);
455830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  for (auto it = result.begin(); it != result.end(); ++it) {
465830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes      if (should_escape(*it)) {
475830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes          it = result.insert(it, '\\') + 1;
485830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes      }
495830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  }
505830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes  return result;
515830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes}
52