Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(589)

Side by Side Diff: runtime/bin/platform_fuchsia.cc

Issue 2941873002: [Fuchsia] Fix Platform.executable and Platform.resolvedExecutable (Closed)
Patch Set: Format Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/platform_android.cc ('k') | runtime/bin/platform_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(HOST_OS_FUCHSIA) 6 #if defined(HOST_OS_FUCHSIA)
7 7
8 #include "bin/platform.h" 8 #include "bin/platform.h"
9 9
10 #include <string.h> // NOLINT 10 #include <magenta/process.h>
11 #include <unistd.h> // NOLINT 11 #include <magenta/status.h>
12 #include <magenta/syscalls.h>
13 #include <string.h>
14 #include <unistd.h>
12 15
13 #include "bin/dartutils.h" 16 #include "bin/dartutils.h"
14 #include "bin/fdutils.h" 17 #include "bin/fdutils.h"
15 #include "bin/file.h" 18 #include "bin/file.h"
16 19
17 namespace dart { 20 namespace dart {
18 namespace bin { 21 namespace bin {
19 22
20 const char* Platform::executable_name_ = NULL; 23 const char* Platform::executable_name_ = NULL;
21 char* Platform::resolved_executable_name_ = NULL; 24 char* Platform::resolved_executable_name_ = NULL;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 *count = i; 75 *count = i;
73 char** result; 76 char** result;
74 result = reinterpret_cast<char**>(Dart_ScopeAllocate(i * sizeof(*result))); 77 result = reinterpret_cast<char**>(Dart_ScopeAllocate(i * sizeof(*result)));
75 for (intptr_t current = 0; current < i; current++) { 78 for (intptr_t current = 0; current < i; current++) {
76 result[current] = environ[current]; 79 result[current] = environ[current];
77 } 80 }
78 return result; 81 return result;
79 } 82 }
80 83
81 84
85 const char* Platform::GetExecutableName() {
86 if (executable_name_ != NULL) {
87 return executable_name_;
88 }
89 char* name = DartUtils::ScopedCString(MX_MAX_NAME_LEN);
90 mx_status_t status = mx_object_get_property(mx_process_self(), MX_PROP_NAME,
91 name, MX_MAX_NAME_LEN);
92 if (status != MX_OK) {
93 return NULL;
94 }
95 return name;
96 }
97
98
82 const char* Platform::ResolveExecutablePath() { 99 const char* Platform::ResolveExecutablePath() {
83 // The string used on the command line to spawn the executable is in argv_[0]. 100 const char* executable_name = Platform::GetExecutableName();
84 // If that string is a relative or absolute path, i.e. it contains a '/', then 101 if (executable_name == NULL) {
85 // we make the path absolute if it is not already and return it. If argv_[0] 102 return NULL;
86 // does not contain a '/', we assume it is a program whose location is 103 }
87 // resolved via the PATH environment variable, and search for it using the 104 if ((executable_name[0] == '/') && File::Exists(executable_name)) {
88 // paths found there. 105 return File::GetCanonicalPath(executable_name);
89 const char* path = getenv("PATH"); 106 }
90 if ((strchr(argv_[0], '/') != NULL) || (path == NULL)) { 107 if (strchr(executable_name, '/') != NULL) {
91 if (argv_[0][0] == '/') { 108 const char* result = File::GetCanonicalPath(executable_name);
92 return File::GetCanonicalPath(argv_[0]); 109 if (File::Exists(result)) {
93 } else { 110 return result;
94 char* result = DartUtils::ScopedCString(PATH_MAX + 1);
95 char* cwd = DartUtils::ScopedCString(PATH_MAX + 1);
96 getcwd(cwd, PATH_MAX);
97 snprintf(result, PATH_MAX, "%s/%s", cwd, argv_[0]);
98 result[PATH_MAX] = '\0';
99 ASSERT(File::Exists(result));
100 return File::GetCanonicalPath(result);
101 } 111 }
102 } else { 112 } else {
113 const char* path = getenv("PATH");
114 if (path == NULL) {
115 // If PATH isn't set, make some guesses about where we should look.
116 path = "/system/bin:/system/apps:/boot/bin";
117 }
103 char* pathcopy = DartUtils::ScopedCopyCString(path); 118 char* pathcopy = DartUtils::ScopedCopyCString(path);
104 char* result = DartUtils::ScopedCString(PATH_MAX + 1); 119 char* result = DartUtils::ScopedCString(PATH_MAX + 1);
105 char* save = NULL; 120 char* save = NULL;
106 while ((pathcopy = strtok_r(pathcopy, ":", &save)) != NULL) { 121 while ((pathcopy = strtok_r(pathcopy, ":", &save)) != NULL) {
107 snprintf(result, PATH_MAX, "%s/%s", pathcopy, argv_[0]); 122 snprintf(result, PATH_MAX, "%s/%s", pathcopy, executable_name);
108 result[PATH_MAX] = '\0'; 123 result[PATH_MAX] = '\0';
109 if (File::Exists(result)) { 124 if (File::Exists(result)) {
110 return File::GetCanonicalPath(result); 125 return File::GetCanonicalPath(result);
111 } 126 }
112 pathcopy = NULL; 127 pathcopy = NULL;
113 } 128 }
114 // Couldn't find it. This causes null to be returned for
115 // Platform.resovledExecutable.
116 return NULL;
117 } 129 }
130 // Couldn't find it. This causes null to be returned for
131 // Platform.resovledExecutable.
132 return NULL;
118 } 133 }
119 134
120 135
121 void Platform::Exit(int exit_code) { 136 void Platform::Exit(int exit_code) {
122 exit(exit_code); 137 exit(exit_code);
123 } 138 }
124 139
125 } // namespace bin 140 } // namespace bin
126 } // namespace dart 141 } // namespace dart
127 142
128 #endif // defined(HOST_OS_FUCHSIA) 143 #endif // defined(HOST_OS_FUCHSIA)
OLDNEW
« no previous file with comments | « runtime/bin/platform_android.cc ('k') | runtime/bin/platform_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698