| 137 | dladdr(), pstat_getpathname(), etc. */ |
| 138 | |
| 139 | static void |
| 140 | set_program_path (char const *arg) |
| 141 | { |
| 142 | if (strchr (arg, '/')) /* Use absolute or relative paths directly. */ |
| 143 | { |
| 144 | program_path = dir_name (arg); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | char *path = xreadlink ("/proc/self/exe"); |
| 149 | if (path) |
| 150 | program_path = dir_name (path); |
| 151 | else if ((path = getenv ("PATH"))) |
| 152 | { |
| 153 | path = xstrdup (path); |
| 154 | for (char *dir = strtok (path, ":"); dir != NULL; |
| 155 | dir = strtok (NULL, ":")) |
| 156 | { |
| 157 | char *candidate = file_name_concat (dir, arg, NULL); |
| 158 | if (access (candidate, X_OK) == 0) |
| 159 | { |
| 160 | program_path = dir_name (candidate); |
| 161 | free (candidate); |
| 162 | break; |
| 163 | } |
| 164 | free (candidate); |
| 165 | } |
| 166 | } |
| 167 | free (path); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static int |
| 172 | optc_to_fileno (int c) |