diff options
Diffstat (limited to 'src/lib/os_installation.c')
-rw-r--r-- | src/lib/os_installation.c | 209 |
1 files changed, 101 insertions, 108 deletions
diff --git a/src/lib/os_installation.c b/src/lib/os_installation.c index 9a884f27..b6927874 100644 --- a/src/lib/os_installation.c +++ b/src/lib/os_installation.c | |||
@@ -50,25 +50,21 @@ get_path_from_proc_maps () | |||
50 | FILE *f; | 50 | FILE *f; |
51 | char *lgu; | 51 | char *lgu; |
52 | 52 | ||
53 | GNUNET_snprintf (fn, | 53 | GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ()); |
54 | sizeof(fn), | ||
55 | "/proc/%u/maps", | ||
56 | getpid ()); | ||
57 | f = fopen (fn, "r"); | 54 | f = fopen (fn, "r"); |
58 | if (f == NULL) | 55 | if (f == NULL) |
59 | return NULL; | 56 | return NULL; |
60 | while (NULL != fgets (line, sizeof(line), f)) | 57 | while (NULL != fgets (line, sizeof (line), f)) |
58 | { | ||
59 | if ((1 == | ||
60 | sscanf (line, "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", dir)) && | ||
61 | (NULL != (lgu = strstr (dir, "gnunet-gtk")))) | ||
61 | { | 62 | { |
62 | if ((1 == sscanf (line, | 63 | lgu[0] = '\0'; |
63 | "%*x-%*x %*c%*c%*c%*c %*x %*2u:%*2u %*u%*[ ]%s", | 64 | fclose (f); |
64 | dir)) && | 65 | return GNUNET_strdup (dir); |
65 | (NULL != (lgu = strstr (dir, "gnunet-gtk")))) | ||
66 | { | ||
67 | lgu[0] = '\0'; | ||
68 | fclose (f); | ||
69 | return GNUNET_strdup (dir); | ||
70 | } | ||
71 | } | 66 | } |
67 | } | ||
72 | fclose (f); | 68 | fclose (f); |
73 | return NULL; | 69 | return NULL; |
74 | } | 70 | } |
@@ -83,23 +79,22 @@ get_path_from_proc_exe () | |||
83 | char lnk[1024]; | 79 | char lnk[1024]; |
84 | ssize_t size; | 80 | ssize_t size; |
85 | 81 | ||
86 | GNUNET_snprintf (fn, | 82 | GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/exe", getpid ()); |
87 | sizeof(fn), "/proc/%u/exe", getpid ()); | 83 | size = readlink (fn, lnk, sizeof (lnk) - 1); |
88 | size = readlink (fn, lnk, sizeof (lnk)-1); | ||
89 | if (size <= 0) | 84 | if (size <= 0) |
90 | { | 85 | { |
91 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); | 86 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "readlink", fn); |
92 | return NULL; | 87 | return NULL; |
93 | } | 88 | } |
94 | GNUNET_assert (size < sizeof (lnk)); | 89 | GNUNET_assert (size < sizeof (lnk)); |
95 | lnk[size] = '\0'; | 90 | lnk[size] = '\0'; |
96 | while ((lnk[size] != '/') && (size > 0)) | 91 | while ((lnk[size] != '/') && (size > 0)) |
97 | size--; | 92 | size--; |
98 | if ((size < 4) || (lnk[size - 4] != '/')) | 93 | if ((size < 4) || (lnk[size - 4] != '/')) |
99 | { | 94 | { |
100 | /* not installed in "/bin/" -- binary path probably useless */ | 95 | /* not installed in "/bin/" -- binary path probably useless */ |
101 | return NULL; | 96 | return NULL; |
102 | } | 97 | } |
103 | lnk[size] = '\0'; | 98 | lnk[size] = '\0'; |
104 | return GNUNET_strdup (lnk); | 99 | return GNUNET_strdup (lnk); |
105 | } | 100 | } |
@@ -115,7 +110,7 @@ get_path_from_module_filename () | |||
115 | char path[4097]; | 110 | char path[4097]; |
116 | char *idx; | 111 | char *idx; |
117 | 112 | ||
118 | GetModuleFileName (NULL, path, sizeof(path)-1); | 113 | GetModuleFileName (NULL, path, sizeof (path) - 1); |
119 | idx = path + strlen (path); | 114 | idx = path + strlen (path); |
120 | while ((idx > path) && (*idx != '\\') && (*idx != '/')) | 115 | while ((idx > path) && (*idx != '\\') && (*idx != '/')) |
121 | idx--; | 116 | idx--; |
@@ -138,7 +133,7 @@ get_path_from_NSGetExecutablePath () | |||
138 | 133 | ||
139 | path = NULL; | 134 | path = NULL; |
140 | func = | 135 | func = |
141 | (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); | 136 | (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); |
142 | if (!func) | 137 | if (!func) |
143 | return NULL; | 138 | return NULL; |
144 | path = &zero; | 139 | path = &zero; |
@@ -150,10 +145,10 @@ get_path_from_NSGetExecutablePath () | |||
150 | path = GNUNET_malloc (len); | 145 | path = GNUNET_malloc (len); |
151 | ret = func (path, &len); | 146 | ret = func (path, &len); |
152 | if (ret != 0) | 147 | if (ret != 0) |
153 | { | 148 | { |
154 | GNUNET_free (path); | 149 | GNUNET_free (path); |
155 | return NULL; | 150 | return NULL; |
156 | } | 151 | } |
157 | len = strlen (path); | 152 | len = strlen (path); |
158 | while ((path[len] != '/') && (len > 0)) | 153 | while ((path[len] != '/') && (len > 0)) |
159 | len--; | 154 | len--; |
@@ -172,22 +167,22 @@ get_path_from_dyld_image () | |||
172 | p = NULL; | 167 | p = NULL; |
173 | c = _dyld_image_count (); | 168 | c = _dyld_image_count (); |
174 | for (i = 0; i < c; i++) | 169 | for (i = 0; i < c; i++) |
170 | { | ||
171 | if (_dyld_get_image_header (i) == &_mh_dylib_header) | ||
175 | { | 172 | { |
176 | if (_dyld_get_image_header (i) == &_mh_dylib_header) | 173 | path = _dyld_get_image_name (i); |
177 | { | 174 | if (path != NULL && strlen (path) > 0) |
178 | path = _dyld_get_image_name (i); | 175 | { |
179 | if (path != NULL && strlen (path) > 0) | 176 | p = strdup (path); |
180 | { | 177 | s = p + strlen (p); |
181 | p = strdup (path); | 178 | while ((s > p) && (*s != '/')) |
182 | s = p + strlen (p); | 179 | s--; |
183 | while ((s > p) && (*s != '/')) | 180 | s++; |
184 | s--; | 181 | *s = '\0'; |
185 | s++; | 182 | } |
186 | *s = '\0'; | 183 | break; |
187 | } | ||
188 | break; | ||
189 | } | ||
190 | } | 184 | } |
185 | } | ||
191 | return p; | 186 | return p; |
192 | } | 187 | } |
193 | #endif | 188 | #endif |
@@ -209,26 +204,26 @@ get_path_from_PATH () | |||
209 | pos = path; | 204 | pos = path; |
210 | 205 | ||
211 | while (NULL != (end = strchr (pos, ':'))) | 206 | while (NULL != (end = strchr (pos, ':'))) |
212 | { | 207 | { |
213 | *end = '\0'; | 208 | *end = '\0'; |
214 | sprintf (buf, "%s/%s", pos, "gnunet-gtk"); | 209 | sprintf (buf, "%s/%s", pos, "gnunet-gtk"); |
215 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) | 210 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) |
216 | { | ||
217 | pos = GNUNET_strdup (pos); | ||
218 | GNUNET_free (buf); | ||
219 | GNUNET_free (path); | ||
220 | return pos; | ||
221 | } | ||
222 | pos = end + 1; | ||
223 | } | ||
224 | sprintf (buf, "%s/%s", pos, "gnunet-gtk"); | ||
225 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) | ||
226 | { | 211 | { |
227 | pos = GNUNET_strdup (pos); | 212 | pos = GNUNET_strdup (pos); |
228 | GNUNET_free (buf); | 213 | GNUNET_free (buf); |
229 | GNUNET_free (path); | 214 | GNUNET_free (path); |
230 | return pos; | 215 | return pos; |
231 | } | 216 | } |
217 | pos = end + 1; | ||
218 | } | ||
219 | sprintf (buf, "%s/%s", pos, "gnunet-gtk"); | ||
220 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) | ||
221 | { | ||
222 | pos = GNUNET_strdup (pos); | ||
223 | GNUNET_free (buf); | ||
224 | GNUNET_free (path); | ||
225 | return pos; | ||
226 | } | ||
232 | GNUNET_free (buf); | 227 | GNUNET_free (buf); |
233 | GNUNET_free (path); | 228 | GNUNET_free (path); |
234 | return NULL; | 229 | return NULL; |
@@ -290,8 +285,7 @@ os_get_gnunet_path () | |||
290 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 285 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
291 | _ | 286 | _ |
292 | ("Could not determine installation path for %s. Set `%s' environment variable.\n"), | 287 | ("Could not determine installation path for %s. Set `%s' environment variable.\n"), |
293 | "gnunet-gtk", | 288 | "gnunet-gtk", "GNUNET_GTK_PREFIX"); |
294 | "GNUNET_GTK_PREFIX"); | ||
295 | return NULL; | 289 | return NULL; |
296 | } | 290 | } |
297 | 291 | ||
@@ -357,11 +351,11 @@ GNUNET_GTK_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) | |||
357 | 351 | ||
358 | n = strlen (execpath); | 352 | n = strlen (execpath); |
359 | if (n == 0) | 353 | if (n == 0) |
360 | { | 354 | { |
361 | /* should never happen, but better safe than sorry */ | 355 | /* should never happen, but better safe than sorry */ |
362 | GNUNET_free (execpath); | 356 | GNUNET_free (execpath); |
363 | return NULL; | 357 | return NULL; |
364 | } | 358 | } |
365 | /* remove filename itself */ | 359 | /* remove filename itself */ |
366 | while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) | 360 | while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) |
367 | execpath[--n] = '\0'; | 361 | execpath[--n] = '\0'; |
@@ -370,62 +364,61 @@ GNUNET_GTK_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) | |||
370 | if ((n > 5) && | 364 | if ((n > 5) && |
371 | ((0 == strcasecmp (&execpath[n - 5], "lib32")) || | 365 | ((0 == strcasecmp (&execpath[n - 5], "lib32")) || |
372 | (0 == strcasecmp (&execpath[n - 5], "lib64")))) | 366 | (0 == strcasecmp (&execpath[n - 5], "lib64")))) |
367 | { | ||
368 | if (dirkind != GNUNET_OS_IPK_LIBDIR) | ||
373 | { | 369 | { |
374 | if (dirkind != GNUNET_OS_IPK_LIBDIR) | 370 | /* strip '/lib32' or '/lib64' */ |
375 | { | 371 | execpath[n - 5] = '\0'; |
376 | /* strip '/lib32' or '/lib64' */ | 372 | n -= 5; |
377 | execpath[n - 5] = '\0'; | ||
378 | n -= 5; | ||
379 | } | ||
380 | else | ||
381 | isbasedir = 0; | ||
382 | } | 373 | } |
374 | else | ||
375 | isbasedir = 0; | ||
376 | } | ||
383 | else if ((n > 3) && | 377 | else if ((n > 3) && |
384 | ((0 == strcasecmp (&execpath[n - 3], "bin")) || | 378 | ((0 == strcasecmp (&execpath[n - 3], "bin")) || |
385 | (0 == strcasecmp (&execpath[n - 3], "lib")))) | 379 | (0 == strcasecmp (&execpath[n - 3], "lib")))) |
386 | { | 380 | { |
387 | /* strip '/bin' or '/lib' */ | 381 | /* strip '/bin' or '/lib' */ |
388 | execpath[n - 3] = '\0'; | 382 | execpath[n - 3] = '\0'; |
389 | n -= 3; | 383 | n -= 3; |
390 | } | 384 | } |
391 | /* in case this was a directory named foo-bin, remove "foo-" */ | 385 | /* in case this was a directory named foo-bin, remove "foo-" */ |
392 | while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) | 386 | while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) |
393 | execpath[--n] = '\0'; | 387 | execpath[--n] = '\0'; |
394 | switch (dirkind) | 388 | switch (dirkind) |
395 | { | 389 | { |
396 | case GNUNET_OS_IPK_PREFIX: | 390 | case GNUNET_OS_IPK_PREFIX: |
397 | case GNUNET_OS_IPK_SELF_PREFIX: | 391 | case GNUNET_OS_IPK_SELF_PREFIX: |
398 | dirname = DIR_SEPARATOR_STR; | 392 | dirname = DIR_SEPARATOR_STR; |
399 | break; | 393 | break; |
400 | case GNUNET_OS_IPK_BINDIR: | 394 | case GNUNET_OS_IPK_BINDIR: |
401 | dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; | 395 | dirname = DIR_SEPARATOR_STR "bin" DIR_SEPARATOR_STR; |
402 | break; | 396 | break; |
403 | case GNUNET_OS_IPK_LIBDIR: | 397 | case GNUNET_OS_IPK_LIBDIR: |
404 | if (isbasedir) | 398 | if (isbasedir) |
405 | dirname = | 399 | dirname = |
406 | DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet-gtk" | 400 | DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR "gnunet-gtk" |
407 | DIR_SEPARATOR_STR; | 401 | DIR_SEPARATOR_STR; |
408 | else | 402 | else |
409 | dirname = DIR_SEPARATOR_STR "gnunet-gtk" DIR_SEPARATOR_STR; | 403 | dirname = DIR_SEPARATOR_STR "gnunet-gtk" DIR_SEPARATOR_STR; |
410 | break; | 404 | break; |
411 | case GNUNET_OS_IPK_DATADIR: | 405 | case GNUNET_OS_IPK_DATADIR: |
412 | dirname = | 406 | dirname = |
413 | DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet-gtk" | 407 | DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "gnunet-gtk" |
414 | DIR_SEPARATOR_STR; | 408 | DIR_SEPARATOR_STR; |
415 | break; | 409 | break; |
416 | case GNUNET_OS_IPK_ICONDIR: | 410 | case GNUNET_OS_IPK_ICONDIR: |
417 | dirname = | 411 | dirname = |
418 | DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; | 412 | DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR; |
419 | break; | 413 | break; |
420 | case GNUNET_OS_IPK_LOCALEDIR: | 414 | case GNUNET_OS_IPK_LOCALEDIR: |
421 | dirname = | 415 | dirname = |
422 | DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" | 416 | DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR; |
423 | DIR_SEPARATOR_STR; | 417 | break; |
424 | break; | 418 | default: |
425 | default: | 419 | GNUNET_free (execpath); |
426 | GNUNET_free (execpath); | 420 | return NULL; |
427 | return NULL; | 421 | } |
428 | } | ||
429 | tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1); | 422 | tmp = GNUNET_malloc (strlen (execpath) + strlen (dirname) + 1); |
430 | sprintf (tmp, "%s%s", execpath, dirname); | 423 | sprintf (tmp, "%s%s", execpath, dirname); |
431 | GNUNET_free (execpath); | 424 | GNUNET_free (execpath); |