John Stiles | 964d8f1 | 2022-10-26 13:52:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "tools/SkGetExecutablePath.h" |
| 9 | #include <cstddef> |
| 10 | #include <linux/limits.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | // Note that /proc/self/exe is Linux-specific; this won't work on other UNIX systems. |
| 15 | |
John Stiles | f04b827 | 2022-11-04 13:58:26 -0400 | [diff] [blame] | 16 | std::string SkGetExecutablePath() { |
| 17 | std::string result(PATH_MAX, '\0'); |
John Stiles | 1e64fb9 | 2022-10-27 14:02:39 -0400 | [diff] [blame] | 18 | ssize_t len = readlink("/proc/self/exe", result.data(), result.size() - 1); |
John Stiles | 964d8f1 | 2022-10-26 13:52:03 -0400 | [diff] [blame] | 19 | if (len < 0 || static_cast<size_t>(len) >= PATH_MAX - 1) { |
John Stiles | f04b827 | 2022-11-04 13:58:26 -0400 | [diff] [blame] | 20 | result.clear(); |
John Stiles | 964d8f1 | 2022-10-26 13:52:03 -0400 | [diff] [blame] | 21 | } else { |
| 22 | result.resize(len); |
| 23 | } |
| 24 | return result; |
| 25 | } |