diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-06-13 08:58:42 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-06-13 08:58:42 +0000 |
commit | f7de70d68cfad378a9b4f3b1f572196e8fa23e2a (patch) | |
tree | 458f632a19f37ee071be657e099b3f0e9f99980c /src/util/os_installation.c | |
parent | 10cb56ae4e463e404d4d3ba2147340809d608ed8 (diff) | |
download | gnunet-f7de70d68cfad378a9b4f3b1f572196e8fa23e2a.tar.gz gnunet-f7de70d68cfad378a9b4f3b1f572196e8fa23e2a.zip |
-fixes and cleanup
Diffstat (limited to 'src/util/os_installation.c')
-rw-r--r-- | src/util/os_installation.c | 110 |
1 files changed, 55 insertions, 55 deletions
diff --git a/src/util/os_installation.c b/src/util/os_installation.c index 2e1acac64..962d016e7 100644 --- a/src/util/os_installation.c +++ b/src/util/os_installation.c | |||
@@ -56,8 +56,7 @@ get_path_from_proc_maps () | |||
56 | char *lgu; | 56 | char *lgu; |
57 | 57 | ||
58 | GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ()); | 58 | GNUNET_snprintf (fn, sizeof (fn), "/proc/%u/maps", getpid ()); |
59 | f = FOPEN (fn, "r"); | 59 | if (NULL == (f = FOPEN (fn, "r"))) |
60 | if (f == NULL) | ||
61 | return NULL; | 60 | return NULL; |
62 | while (NULL != fgets (line, sizeof (line), f)) | 61 | while (NULL != fgets (line, sizeof (line), f)) |
63 | { | 62 | { |
@@ -74,6 +73,7 @@ get_path_from_proc_maps () | |||
74 | return NULL; | 73 | return NULL; |
75 | } | 74 | } |
76 | 75 | ||
76 | |||
77 | /** | 77 | /** |
78 | * Try to determine path by reading /proc/PID/exe | 78 | * Try to determine path by reading /proc/PID/exe |
79 | */ | 79 | */ |
@@ -131,6 +131,7 @@ get_path_from_module_filename () | |||
131 | #if DARWIN | 131 | #if DARWIN |
132 | typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize); | 132 | typedef int (*MyNSGetExecutablePathProto) (char *buf, size_t * bufsize); |
133 | 133 | ||
134 | |||
134 | static char * | 135 | static char * |
135 | get_path_from_NSGetExecutablePath () | 136 | get_path_from_NSGetExecutablePath () |
136 | { | 137 | { |
@@ -138,22 +139,19 @@ get_path_from_NSGetExecutablePath () | |||
138 | char *path; | 139 | char *path; |
139 | size_t len; | 140 | size_t len; |
140 | MyNSGetExecutablePathProto func; | 141 | MyNSGetExecutablePathProto func; |
141 | int ret; | ||
142 | 142 | ||
143 | path = NULL; | 143 | path = NULL; |
144 | func = | 144 | if (NULL == (func = |
145 | (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"); | 145 | (MyNSGetExecutablePathProto) dlsym (RTLD_DEFAULT, "_NSGetExecutablePath"))) |
146 | if (!func) | ||
147 | return NULL; | 146 | return NULL; |
148 | path = &zero; | 147 | path = &zero; |
149 | len = 0; | 148 | len = 0; |
150 | /* get the path len, including the trailing \0 */ | 149 | /* get the path len, including the trailing \0 */ |
151 | func (path, &len); | 150 | func (path, &len); |
152 | if (len == 0) | 151 | if (0 == len) |
153 | return NULL; | 152 | return NULL; |
154 | path = GNUNET_malloc (len); | 153 | path = GNUNET_malloc (len); |
155 | ret = func (path, &len); | 154 | if (0 != func (path, &len)) |
156 | if (ret != 0) | ||
157 | { | 155 | { |
158 | GNUNET_free (path); | 156 | GNUNET_free (path); |
159 | return NULL; | 157 | return NULL; |
@@ -165,11 +163,13 @@ get_path_from_NSGetExecutablePath () | |||
165 | return path; | 163 | return path; |
166 | } | 164 | } |
167 | 165 | ||
166 | |||
168 | static char * | 167 | static char * |
169 | get_path_from_dyld_image () | 168 | get_path_from_dyld_image () |
170 | { | 169 | { |
171 | const char *path; | 170 | const char *path; |
172 | char *p, *s; | 171 | char *p; |
172 | char *s; | ||
173 | int i; | 173 | int i; |
174 | int c; | 174 | int c; |
175 | 175 | ||
@@ -180,11 +180,11 @@ get_path_from_dyld_image () | |||
180 | if (_dyld_get_image_header (i) == &_mh_dylib_header) | 180 | if (_dyld_get_image_header (i) == &_mh_dylib_header) |
181 | { | 181 | { |
182 | path = _dyld_get_image_name (i); | 182 | path = _dyld_get_image_name (i); |
183 | if (path != NULL && strlen (path) > 0) | 183 | if ( (NULL != path) && (strlen (path) > 0) ) |
184 | { | 184 | { |
185 | p = GNUNET_strdup (path); | 185 | p = GNUNET_strdup (path); |
186 | s = p + strlen (p); | 186 | s = p + strlen (p); |
187 | while ((s > p) && (*s != '/')) | 187 | while ((s > p) && ('/' != *s)) |
188 | s--; | 188 | s--; |
189 | s++; | 189 | s++; |
190 | *s = '\0'; | 190 | *s = '\0'; |
@@ -196,6 +196,7 @@ get_path_from_dyld_image () | |||
196 | } | 196 | } |
197 | #endif | 197 | #endif |
198 | 198 | ||
199 | |||
199 | /** | 200 | /** |
200 | * Return the actual path to a file found in the current | 201 | * Return the actual path to a file found in the current |
201 | * PATH environment variable. | 202 | * PATH environment variable. |
@@ -213,7 +214,7 @@ get_path_from_PATH (const char *binary) | |||
213 | const char *p; | 214 | const char *p; |
214 | 215 | ||
215 | p = getenv ("PATH"); | 216 | p = getenv ("PATH"); |
216 | if (p == NULL) | 217 | if (NULL == p) |
217 | return NULL; | 218 | return NULL; |
218 | path = GNUNET_strdup (p); /* because we write on it */ | 219 | path = GNUNET_strdup (p); /* because we write on it */ |
219 | buf = GNUNET_malloc (strlen (path) + 20); | 220 | buf = GNUNET_malloc (strlen (path) + 20); |
@@ -232,7 +233,7 @@ get_path_from_PATH (const char *binary) | |||
232 | pos = end + 1; | 233 | pos = end + 1; |
233 | } | 234 | } |
234 | sprintf (buf, "%s/%s", pos, binary); | 235 | sprintf (buf, "%s/%s", pos, binary); |
235 | if (GNUNET_DISK_file_test (buf) == GNUNET_YES) | 236 | if (GNUNET_YES == GNUNET_DISK_file_test (buf)) |
236 | { | 237 | { |
237 | pos = GNUNET_strdup (pos); | 238 | pos = GNUNET_strdup (pos); |
238 | GNUNET_free (buf); | 239 | GNUNET_free (buf); |
@@ -244,17 +245,18 @@ get_path_from_PATH (const char *binary) | |||
244 | return NULL; | 245 | return NULL; |
245 | } | 246 | } |
246 | 247 | ||
248 | |||
247 | static char * | 249 | static char * |
248 | get_path_from_GNUNET_PREFIX () | 250 | get_path_from_GNUNET_PREFIX () |
249 | { | 251 | { |
250 | const char *p; | 252 | const char *p; |
251 | 253 | ||
252 | p = getenv ("GNUNET_PREFIX"); | 254 | if (NULL != (p = getenv ("GNUNET_PREFIX"))) |
253 | if (p != NULL) | ||
254 | return GNUNET_strdup (p); | 255 | return GNUNET_strdup (p); |
255 | return NULL; | 256 | return NULL; |
256 | } | 257 | } |
257 | 258 | ||
259 | |||
258 | /** | 260 | /** |
259 | * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path | 261 | * @brief get the path to GNUnet bin/ or lib/, prefering the lib/ path |
260 | * @author Milan | 262 | * @author Milan |
@@ -266,32 +268,25 @@ os_get_gnunet_path () | |||
266 | { | 268 | { |
267 | char *ret; | 269 | char *ret; |
268 | 270 | ||
269 | ret = get_path_from_GNUNET_PREFIX (); | 271 | if (NULL != (ret = get_path_from_GNUNET_PREFIX ())) |
270 | if (ret != NULL) | ||
271 | return ret; | 272 | return ret; |
272 | #if LINUX | 273 | #if LINUX |
273 | ret = get_path_from_proc_maps (); | 274 | if (NULL != (ret = get_path_from_proc_maps ())) |
274 | if (ret != NULL) | ||
275 | return ret; | 275 | return ret; |
276 | ret = get_path_from_proc_exe (); | 276 | if (NULL != (ret = get_path_from_proc_exe ())) |
277 | if (ret != NULL) | ||
278 | return ret; | 277 | return ret; |
279 | #endif | 278 | #endif |
280 | #if WINDOWS | 279 | #if WINDOWS |
281 | ret = get_path_from_module_filename (); | 280 | if (NULL != (ret = get_path_from_module_filename ())) |
282 | if (ret != NULL) | ||
283 | return ret; | 281 | return ret; |
284 | #endif | 282 | #endif |
285 | #if DARWIN | 283 | #if DARWIN |
286 | ret = get_path_from_dyld_image (); | 284 | if (NULL != (ret = get_path_from_dyld_image ())) |
287 | if (ret != NULL) | ||
288 | return ret; | 285 | return ret; |
289 | ret = get_path_from_NSGetExecutablePath (); | 286 | if (NULL != (ret = get_path_from_NSGetExecutablePath ())) |
290 | if (ret != NULL) | ||
291 | return ret; | 287 | return ret; |
292 | #endif | 288 | #endif |
293 | ret = get_path_from_PATH ("gnunet-arm"); | 289 | if (NULL != (ret = get_path_from_PATH ("gnunet-arm"))) |
294 | if (ret != NULL) | ||
295 | return ret; | 290 | return ret; |
296 | /* other attempts here */ | 291 | /* other attempts here */ |
297 | LOG (GNUNET_ERROR_TYPE_ERROR, | 292 | LOG (GNUNET_ERROR_TYPE_ERROR, |
@@ -312,24 +307,20 @@ os_get_exec_path () | |||
312 | { | 307 | { |
313 | char *ret; | 308 | char *ret; |
314 | 309 | ||
315 | ret = NULL; | ||
316 | #if LINUX | 310 | #if LINUX |
317 | ret = get_path_from_proc_exe (); | 311 | if (NULL != (ret = get_path_from_proc_exe ())) |
318 | if (ret != NULL) | ||
319 | return ret; | 312 | return ret; |
320 | #endif | 313 | #endif |
321 | #if WINDOWS | 314 | #if WINDOWS |
322 | ret = get_path_from_module_filename (); | 315 | if (NULL != (ret = get_path_from_module_filename ())) |
323 | if (ret != NULL) | ||
324 | return ret; | 316 | return ret; |
325 | #endif | 317 | #endif |
326 | #if DARWIN | 318 | #if DARWIN |
327 | ret = get_path_from_NSGetExecutablePath (); | 319 | if (NULL != (ret = get_path_from_NSGetExecutablePath ())) |
328 | if (ret != NULL) | ||
329 | return ret; | 320 | return ret; |
330 | #endif | 321 | #endif |
331 | /* other attempts here */ | 322 | /* other attempts here */ |
332 | return ret; | 323 | return NULL; |
333 | } | 324 | } |
334 | 325 | ||
335 | 326 | ||
@@ -355,21 +346,21 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) | |||
355 | 346 | ||
356 | /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some | 347 | /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some |
357 | * guess for the current app */ | 348 | * guess for the current app */ |
358 | if (execpath == NULL) | 349 | if (NULL == execpath) |
359 | execpath = os_get_gnunet_path (); | 350 | execpath = os_get_gnunet_path (); |
360 | 351 | ||
361 | if (execpath == NULL) | 352 | if (NULL == execpath) |
362 | return NULL; | 353 | return NULL; |
363 | 354 | ||
364 | n = strlen (execpath); | 355 | n = strlen (execpath); |
365 | if (n == 0) | 356 | if (0 == n) |
366 | { | 357 | { |
367 | /* should never happen, but better safe than sorry */ | 358 | /* should never happen, but better safe than sorry */ |
368 | GNUNET_free (execpath); | 359 | GNUNET_free (execpath); |
369 | return NULL; | 360 | return NULL; |
370 | } | 361 | } |
371 | /* remove filename itself */ | 362 | /* remove filename itself */ |
372 | while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR)) | 363 | while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1])) |
373 | execpath[--n] = '\0'; | 364 | execpath[--n] = '\0'; |
374 | 365 | ||
375 | isbasedir = 1; | 366 | isbasedir = 1; |
@@ -377,7 +368,7 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) | |||
377 | ((0 == strcasecmp (&execpath[n - 5], "lib32")) || | 368 | ((0 == strcasecmp (&execpath[n - 5], "lib32")) || |
378 | (0 == strcasecmp (&execpath[n - 5], "lib64")))) | 369 | (0 == strcasecmp (&execpath[n - 5], "lib64")))) |
379 | { | 370 | { |
380 | if (dirkind != GNUNET_OS_IPK_LIBDIR) | 371 | if (GNUNET_OS_IPK_LIBDIR != dirkind) |
381 | { | 372 | { |
382 | /* strip '/lib32' or '/lib64' */ | 373 | /* strip '/lib32' or '/lib64' */ |
383 | execpath[n - 5] = '\0'; | 374 | execpath[n - 5] = '\0'; |
@@ -458,30 +449,39 @@ GNUNET_OS_check_helper_binary (const char *binary) | |||
458 | struct stat statbuf; | 449 | struct stat statbuf; |
459 | char *p; | 450 | char *p; |
460 | char *pf; | 451 | char *pf; |
461 | |||
462 | #ifdef MINGW | 452 | #ifdef MINGW |
463 | SOCKET rawsock; | 453 | SOCKET rawsock; |
464 | char *binaryexe; | 454 | char *binaryexe; |
465 | 455 | ||
466 | GNUNET_asprintf (&binaryexe, "%s.exe", binary); | 456 | GNUNET_asprintf (&binaryexe, "%s.exe", binary); |
467 | p = get_path_from_PATH (binaryexe); | 457 | if (DIR_SEPARATOR == binary[0]) |
468 | if (p != NULL) | 458 | p = GNUNET_strdup (binary); |
459 | else | ||
469 | { | 460 | { |
470 | GNUNET_asprintf (&pf, "%s/%s", p, binaryexe); | 461 | p = get_path_from_PATH (binaryexe); |
471 | GNUNET_free (p); | 462 | if (NULL != p) |
472 | p = pf; | 463 | { |
464 | GNUNET_asprintf (&pf, "%s/%s", p, binaryexe); | ||
465 | GNUNET_free (p); | ||
466 | p = pf; | ||
467 | } | ||
473 | } | 468 | } |
474 | GNUNET_free (binaryexe); | 469 | GNUNET_free (binaryexe); |
475 | #else | 470 | #else |
476 | p = get_path_from_PATH (binary); | 471 | if (DIR_SEPARATOR == binary[0]) |
477 | if (p != NULL) | 472 | p = GNUNET_strdup (binary); |
473 | else | ||
478 | { | 474 | { |
479 | GNUNET_asprintf (&pf, "%s/%s", p, binary); | 475 | p = get_path_from_PATH (binary); |
480 | GNUNET_free (p); | 476 | if (NULL != p) |
481 | p = pf; | 477 | { |
478 | GNUNET_asprintf (&pf, "%s/%s", p, binary); | ||
479 | GNUNET_free (p); | ||
480 | p = pf; | ||
481 | } | ||
482 | } | 482 | } |
483 | #endif | 483 | #endif |
484 | if (p == NULL) | 484 | if (NULL == p) |
485 | { | 485 | { |
486 | LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"), | 486 | LOG (GNUNET_ERROR_TYPE_INFO, _("Could not find binary `%s' in PATH!\n"), |
487 | binary); | 487 | binary); |