Chromium Code Reviews| OLD | NEW |
|---|---|
| 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(TARGET_OS_FUCHSIA) | 6 #if defined(TARGET_OS_FUCHSIA) |
| 7 | 7 |
| 8 #include "bin/platform.h" | 8 #include "bin/platform.h" |
| 9 | 9 |
| 10 #include <string.h> // NOLINT | 10 #include <string.h> // NOLINT |
| 11 #include <unistd.h> // NOLINT | 11 #include <unistd.h> // NOLINT |
| 12 | 12 |
| 13 #include "bin/dartutils.h" | |
| 13 #include "bin/fdutils.h" | 14 #include "bin/fdutils.h" |
| 14 #include "bin/file.h" | 15 #include "bin/file.h" |
| 15 | 16 |
| 16 namespace dart { | 17 namespace dart { |
| 17 namespace bin { | 18 namespace bin { |
| 18 | 19 |
| 19 const char* Platform::executable_name_ = NULL; | 20 const char* Platform::executable_name_ = NULL; |
| 20 char* Platform::resolved_executable_name_ = NULL; | 21 char* Platform::resolved_executable_name_ = NULL; |
| 21 int Platform::script_index_ = 1; | 22 int Platform::script_index_ = 1; |
| 22 char** Platform::argv_ = NULL; | 23 char** Platform::argv_ = NULL; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 45 return "so"; | 46 return "so"; |
| 46 } | 47 } |
| 47 | 48 |
| 48 | 49 |
| 49 bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) { | 50 bool Platform::LocalHostname(char* buffer, intptr_t buffer_length) { |
| 50 return gethostname(buffer, buffer_length) == 0; | 51 return gethostname(buffer, buffer_length) == 0; |
| 51 } | 52 } |
| 52 | 53 |
| 53 | 54 |
| 54 char** Platform::Environment(intptr_t* count) { | 55 char** Platform::Environment(intptr_t* count) { |
| 55 char** result = | 56 // Using environ directly is only safe as long as we do not |
| 56 reinterpret_cast<char**>(Dart_ScopeAllocate(1 * sizeof(*result))); | 57 // provide access to modifying environment variables. |
| 57 result[0] = NULL; | 58 intptr_t i = 0; |
| 59 char** tmp = environ; | |
| 60 while (*(tmp++) != NULL) { | |
| 61 i++; | |
| 62 } | |
| 63 *count = i; | |
| 64 char** result; | |
| 65 result = reinterpret_cast<char**>(Dart_ScopeAllocate(i * sizeof(*result))); | |
| 66 for (intptr_t current = 0; current < i; current++) { | |
| 67 result[current] = environ[current]; | |
| 68 } | |
| 58 return result; | 69 return result; |
| 59 } | 70 } |
| 60 | 71 |
| 61 | 72 |
| 62 const char* Platform::ResolveExecutablePath() { | 73 const char* Platform::ResolveExecutablePath() { |
| 63 return "dart"; | 74 // The string used on the command line to spawn the executable is in argv_[0]. |
| 75 // If that string is a relative or absolute path, i.e. it contains a '/', then | |
| 76 // we make the path absolute if it is not already and return it. If argv_[0] | |
| 77 // does not contain a '/', we assume it is a program whose location is | |
| 78 // resolved via the PATH environment variable, and search for it using the | |
| 79 // paths found there. | |
| 80 const char* path = getenv("PATH"); | |
| 81 if ((strchr(argv_[0], '/') != NULL) || (path == NULL)) { | |
| 82 if (argv_[0][0] == '/') { | |
| 83 return File::GetCanonicalPath(argv_[0]); | |
| 84 } else { | |
| 85 char* result = DartUtils::ScopedCString(PATH_MAX + 1); | |
| 86 char* cwd = DartUtils::ScopedCString(PATH_MAX + 1); | |
| 87 getcwd(cwd, PATH_MAX); | |
| 88 snprintf(result, PATH_MAX, "%s/%s", cwd, argv_[0]); | |
| 89 result[PATH_MAX] = '\0'; | |
| 90 ASSERT(File::Exists(result)); | |
| 91 return File::GetCanonicalPath(result); | |
| 92 } | |
| 93 } else { | |
| 94 char* pathcopy = DartUtils::ScopedCopyCString(path); | |
| 95 char* result = DartUtils::ScopedCString(PATH_MAX + 1); | |
| 96 char* save = NULL; | |
| 97 while ((pathcopy = strtok_r(pathcopy, ":", &save)) != NULL) { | |
| 98 snprintf(result, PATH_MAX, "%s/%s", pathcopy, argv_[0]); | |
| 99 result[PATH_MAX] = '\0'; | |
| 100 if (File::Exists(result)) { | |
| 101 return File::GetCanonicalPath(result); | |
| 102 } | |
| 103 pathcopy = NULL; | |
| 104 } | |
| 105 // Couldn't find it. This causes null to be returned for | |
| 106 // Platform.resovledExecutable. | |
|
rmacnak
2016/12/16 23:05:01
resolved
| |
| 107 return NULL; | |
| 108 } | |
| 64 } | 109 } |
| 65 | 110 |
| 66 | 111 |
| 67 void Platform::Exit(int exit_code) { | 112 void Platform::Exit(int exit_code) { |
| 68 exit(exit_code); | 113 exit(exit_code); |
| 69 } | 114 } |
| 70 | 115 |
| 71 } // namespace bin | 116 } // namespace bin |
| 72 } // namespace dart | 117 } // namespace dart |
| 73 | 118 |
| 74 #endif // defined(TARGET_OS_FUCHSIA) | 119 #endif // defined(TARGET_OS_FUCHSIA) |
| OLD | NEW |