Mercurial > sdl-ios-xcode
comparison src/loadso/macosx/SDL_loadso.c @ 1336:3692456e7b0f
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 06:59:48 +0000 |
parents | c9b51268668f |
children | 604d73db6802 |
comparison
equal
deleted
inserted
replaced
1335:c39265384763 | 1336:3692456e7b0f |
---|---|
280 struct dlthread *tss; | 280 struct dlthread *tss; |
281 char * err_str; | 281 char * err_str; |
282 va_start(arg, str); | 282 va_start(arg, str); |
283 tss = pthread_getspecific(dlerror_key); | 283 tss = pthread_getspecific(dlerror_key); |
284 err_str = tss->errstr; | 284 err_str = tss->errstr; |
285 strncpy(err_str, "dlcompat: ", ERR_STR_LEN); | 285 SDL_strncpy(err_str, "dlcompat: ", ERR_STR_LEN); |
286 vsnprintf(err_str + 10, ERR_STR_LEN - 10, str, arg); | 286 vsnprintf(err_str + 10, ERR_STR_LEN - 10, str, arg); |
287 va_end(arg); | 287 va_end(arg); |
288 debug("ERROR: %s\n", err_str); | 288 debug("ERROR: %s\n", err_str); |
289 tss->errset = 1; | 289 tss->errset = 1; |
290 } | 290 } |
296 #endif | 296 #endif |
297 } | 297 } |
298 | 298 |
299 static const char *safegetenv(const char *s) | 299 static const char *safegetenv(const char *s) |
300 { | 300 { |
301 const char *ss = getenv(s); | 301 const char *ss = SDL_getenv(s); |
302 return ss ? ss : ""; | 302 return ss ? ss : ""; |
303 } | 303 } |
304 | 304 |
305 /* because this is only used for debugging and error reporting functions, we | 305 /* because this is only used for debugging and error reporting functions, we |
306 * don't really care about how elegant it is... it could use the load | 306 * don't really care about how elegant it is... it could use the load |
336 unsigned long count = _dyld_image_count(); | 336 unsigned long count = _dyld_image_count(); |
337 unsigned long i; | 337 unsigned long i; |
338 debug("Module name: %s", mod_name); | 338 debug("Module name: %s", mod_name); |
339 for (i = 0; i < count; i++) | 339 for (i = 0; i < count; i++) |
340 { | 340 { |
341 if (!strcmp(mod_name, _dyld_get_image_name(i))) | 341 if (!SDL_strcmp(mod_name, _dyld_get_image_name(i))) |
342 { | 342 { |
343 mh = _dyld_get_image_header(i); | 343 mh = _dyld_get_image_header(i); |
344 break; | 344 break; |
345 } | 345 } |
346 } | 346 } |
360 { | 360 { |
361 size_t buf_size; | 361 size_t buf_size; |
362 static char *buf=NULL; | 362 static char *buf=NULL; |
363 const char *ldlp = safegetenv("LD_LIBRARY_PATH"); | 363 const char *ldlp = safegetenv("LD_LIBRARY_PATH"); |
364 const char *dyldlp = safegetenv("DYLD_LIBRARY_PATH"); | 364 const char *dyldlp = safegetenv("DYLD_LIBRARY_PATH"); |
365 const char *stdpath = getenv("DYLD_FALLBACK_LIBRARY_PATH"); | 365 const char *stdpath = SDL_getenv("DYLD_FALLBACK_LIBRARY_PATH"); |
366 if (!stdpath) | 366 if (!stdpath) |
367 stdpath = "/usr/local/lib:/lib:/usr/lib"; | 367 stdpath = "/usr/local/lib:/lib:/usr/lib"; |
368 if (!buf) | 368 if (!buf) |
369 { | 369 { |
370 buf_size = strlen(ldlp) + strlen(dyldlp) + strlen(stdpath) + 4; | 370 buf_size = SDL_strlen(ldlp) + SDL_strlen(dyldlp) + SDL_strlen(stdpath) + 4; |
371 buf = malloc(buf_size); | 371 buf = SDL_malloc(buf_size); |
372 snprintf(buf, buf_size, "%s%s%s%s%s%c", dyldlp, (dyldlp[0] ? ":" : ""), ldlp, (ldlp[0] ? ":" : ""), | 372 SDL_snprintf(buf, buf_size, "%s%s%s%s%s%c", dyldlp, (dyldlp[0] ? ":" : ""), ldlp, (ldlp[0] ? ":" : ""), |
373 stdpath, '\0'); | 373 stdpath, '\0'); |
374 } | 374 } |
375 return buf; | 375 return buf; |
376 } | 376 } |
377 | 377 |
381 static const char *list = 0; | 381 static const char *list = 0; |
382 static char **path = (char **)0; | 382 static char **path = (char **)0; |
383 static int end = 0; | 383 static int end = 0; |
384 static int numsize = MAX_SEARCH_PATHS; | 384 static int numsize = MAX_SEARCH_PATHS; |
385 static char **tmp; | 385 static char **tmp; |
386 /* So we can call free() in the "destructor" we use i=-1 to return the alloc'd array */ | 386 /* So we can call SDL_free() in the "destructor" we use i=-1 to return the alloc'd array */ |
387 if (i == -1) | 387 if (i == -1) |
388 { | 388 { |
389 return (const char*)path; | 389 return (const char*)path; |
390 } | 390 } |
391 if (!path) | 391 if (!path) |
398 { | 398 { |
399 debug("Increasing size for long PATH"); | 399 debug("Increasing size for long PATH"); |
400 tmp = (char **)calloc((MAX_SEARCH_PATHS + numsize), sizeof(char **)); | 400 tmp = (char **)calloc((MAX_SEARCH_PATHS + numsize), sizeof(char **)); |
401 if (tmp) | 401 if (tmp) |
402 { | 402 { |
403 memcpy(tmp, path, sizeof(char **) * numsize); | 403 SDL_memcpy(tmp, path, sizeof(char **) * numsize); |
404 free(path); | 404 SDL_free(path); |
405 path = tmp; | 405 path = tmp; |
406 numsize += MAX_SEARCH_PATHS; | 406 numsize += MAX_SEARCH_PATHS; |
407 } | 407 } |
408 else | 408 else |
409 { | 409 { |
426 { | 426 { |
427 static char buf[PATH_MAX]; | 427 static char buf[PATH_MAX]; |
428 const char *path = getSearchPath(i); | 428 const char *path = getSearchPath(i); |
429 if (path) | 429 if (path) |
430 { | 430 { |
431 snprintf(buf, PATH_MAX, "%s/%s", path, file); | 431 SDL_snprintf(buf, PATH_MAX, "%s/%s", path, file); |
432 } | 432 } |
433 return path ? buf : 0; | 433 return path ? buf : 0; |
434 } | 434 } |
435 | 435 |
436 /* Given a file name, try to determine the full path for that file. Starts | 436 /* Given a file name, try to determine the full path for that file. Starts |
444 char *fileName; | 444 char *fileName; |
445 debug("finding file %s", file); | 445 debug("finding file %s", file); |
446 *fullPath = file; | 446 *fullPath = file; |
447 if (0 == stat(file, &sbuf)) | 447 if (0 == stat(file, &sbuf)) |
448 return &sbuf; | 448 return &sbuf; |
449 if (strchr(file, '/')) | 449 if (SDL_strchr(file, '/')) |
450 return 0; /* If the path had a / we don't look in env var places */ | 450 return 0; /* If the path had a / we don't look in env var places */ |
451 fileName = NULL; | 451 fileName = NULL; |
452 if (!fileName) | 452 if (!fileName) |
453 fileName = (char *)file; | 453 fileName = (char *)file; |
454 while ((*fullPath = getFullPath(i++, fileName))) | 454 while ((*fullPath = getFullPath(i++, fileName))) |
565 if (!mh) | 565 if (!mh) |
566 { | 566 { |
567 for (j = 0; j < i; j++) | 567 for (j = 0; j < i; j++) |
568 { | 568 { |
569 id = _dyld_get_image_name(j); | 569 id = _dyld_get_image_name(j); |
570 if (!strcmp(id, name)) | 570 if (!SDL_strcmp(id, name)) |
571 { | 571 { |
572 mh = _dyld_get_image_header(j); | 572 mh = _dyld_get_image_header(j); |
573 break; | 573 break; |
574 } | 574 } |
575 } | 575 } |
618 } | 618 } |
619 } | 619 } |
620 return nssym; | 620 return nssym; |
621 } | 621 } |
622 | 622 |
623 /* Up to the caller to free() returned string */ | 623 /* Up to the caller to SDL_free() returned string */ |
624 static inline const char *dyld_error_str() | 624 static inline const char *dyld_error_str() |
625 { | 625 { |
626 NSLinkEditErrors dylder; | 626 NSLinkEditErrors dylder; |
627 int dylderno; | 627 int dylderno; |
628 const char *dylderrstr; | 628 const char *dylderrstr; |
629 const char *dyldfile; | 629 const char *dyldfile; |
630 const char* retStr = NULL; | 630 const char* retStr = NULL; |
631 NSLinkEditError(&dylder, &dylderno, &dyldfile, &dylderrstr); | 631 NSLinkEditError(&dylder, &dylderno, &dyldfile, &dylderrstr); |
632 if (dylderrstr && strlen(dylderrstr)) | 632 if (dylderrstr && SDL_strlen(dylderrstr)) |
633 { | 633 { |
634 retStr = malloc(strlen(dylderrstr) +1); | 634 retStr = SDL_malloc(SDL_strlen(dylderrstr) +1); |
635 strcpy((char*)retStr,dylderrstr); | 635 SDL_strcpy((char*)retStr,dylderrstr); |
636 } | 636 } |
637 return retStr; | 637 return retStr; |
638 } | 638 } |
639 | 639 |
640 static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetError) | 640 static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetError) |
733 nssym = NSLookupAndBindSymbol(symbol); | 733 nssym = NSLookupAndBindSymbol(symbol); |
734 } | 734 } |
735 else | 735 else |
736 { | 736 { |
737 if (savedErrorStr) | 737 if (savedErrorStr) |
738 free((char*)savedErrorStr); | 738 SDL_free((char*)savedErrorStr); |
739 savedErrorStr = malloc(256); | 739 savedErrorStr = SDL_malloc(256); |
740 snprintf((char*)savedErrorStr, 256, "Symbol \"%s\" not in global context",symbol); | 740 SDL_snprintf((char*)savedErrorStr, 256, "Symbol \"%s\" not in global context",symbol); |
741 } | 741 } |
742 } | 742 } |
743 } | 743 } |
744 /* Error reporting */ | 744 /* Error reporting */ |
745 if (!nssym) | 745 if (!nssym) |
746 { | 746 { |
747 if (!savedErrorStr || !strlen(savedErrorStr)) | 747 if (!savedErrorStr || !SDL_strlen(savedErrorStr)) |
748 { | 748 { |
749 if (savedErrorStr) | 749 if (savedErrorStr) |
750 free((char*)savedErrorStr); | 750 SDL_free((char*)savedErrorStr); |
751 savedErrorStr = malloc(256); | 751 savedErrorStr = SDL_malloc(256); |
752 snprintf((char*)savedErrorStr, 256,"Symbol \"%s\" not found",symbol); | 752 SDL_snprintf((char*)savedErrorStr, 256,"Symbol \"%s\" not found",symbol); |
753 } | 753 } |
754 if (canSetError) | 754 if (canSetError) |
755 { | 755 { |
756 error(savedErrorStr); | 756 error(savedErrorStr); |
757 } | 757 } |
758 else | 758 else |
759 { | 759 { |
760 debug(savedErrorStr); | 760 debug(savedErrorStr); |
761 } | 761 } |
762 if (savedErrorStr) | 762 if (savedErrorStr) |
763 free((char*)savedErrorStr); | 763 SDL_free((char*)savedErrorStr); |
764 return NULL; | 764 return NULL; |
765 } | 765 } |
766 return NSAddressOfSymbol(nssym); | 766 return NSAddressOfSymbol(nssym); |
767 } | 767 } |
768 | 768 |
837 functions with RTLD_NOW, I don't deem it necessary at the moment */ | 837 functions with RTLD_NOW, I don't deem it necessary at the moment */ |
838 } | 838 } |
839 if (!(dls->module)) | 839 if (!(dls->module)) |
840 { | 840 { |
841 NSLinkEditError(&ler, &lerno, &file, &errstr); | 841 NSLinkEditError(&ler, &lerno, &file, &errstr); |
842 if (!errstr || (!strlen(errstr))) | 842 if (!errstr || (!SDL_strlen(errstr))) |
843 error("Can't open this file type"); | 843 error("Can't open this file type"); |
844 else | 844 else |
845 error(errstr); | 845 error(errstr); |
846 if ((dls->flags & DL_IN_LIST) == 0) | 846 if ((dls->flags & DL_IN_LIST) == 0) |
847 { | 847 { |
848 free(dls); | 848 SDL_free(dls); |
849 } | 849 } |
850 return NULL; | 850 return NULL; |
851 } | 851 } |
852 } | 852 } |
853 else | 853 else |
865 if (!dls->module) | 865 if (!dls->module) |
866 { | 866 { |
867 NSLinkEditError(&ler, &lerno, &file, &errstr); | 867 NSLinkEditError(&ler, &lerno, &file, &errstr); |
868 if ((dls->flags & DL_IN_LIST) == 0) | 868 if ((dls->flags & DL_IN_LIST) == 0) |
869 { | 869 { |
870 free(dls); | 870 SDL_free(dls); |
871 } | 871 } |
872 error(errstr); | 872 error(errstr); |
873 return NULL; | 873 return NULL; |
874 } | 874 } |
875 | 875 |
915 tss->errset = 0; | 915 tss->errset = 0; |
916 } | 916 } |
917 | 917 |
918 static void dlerrorfree(void *data) | 918 static void dlerrorfree(void *data) |
919 { | 919 { |
920 free(data); | 920 SDL_free(data); |
921 } | 921 } |
922 | 922 |
923 /* We kind of want a recursive lock here, but meet a little trouble | 923 /* We kind of want a recursive lock here, but meet a little trouble |
924 * because they are not available pre OS X 10.2, so we fake it | 924 * because they are not available pre OS X 10.2, so we fake it |
925 * using thread specific storage to keep a lock count | 925 * using thread specific storage to keep a lock count |
930 struct dlthread *tss; | 930 struct dlthread *tss; |
931 dlcompat_init_check(); | 931 dlcompat_init_check(); |
932 tss = pthread_getspecific(dlerror_key); | 932 tss = pthread_getspecific(dlerror_key); |
933 if (!tss) | 933 if (!tss) |
934 { | 934 { |
935 tss = malloc(sizeof(struct dlthread)); | 935 tss = SDL_malloc(sizeof(struct dlthread)); |
936 tss->lockcnt = 0; | 936 tss->lockcnt = 0; |
937 tss->errset = 0; | 937 tss->errset = 0; |
938 if (pthread_setspecific(dlerror_key, tss)) | 938 if (pthread_setspecific(dlerror_key, tss)) |
939 { | 939 { |
940 fprintf(stderr,"dlcompat: pthread_setspecific failed\n"); | 940 fprintf(stderr,"dlcompat: pthread_setspecific failed\n"); |
1008 } | 1008 } |
1009 | 1009 |
1010 #if !FINK_BUILD | 1010 #if !FINK_BUILD |
1011 static void *SDL_OSX_dlsym(void * dl_restrict handle, const char * dl_restrict symbol) | 1011 static void *SDL_OSX_dlsym(void * dl_restrict handle, const char * dl_restrict symbol) |
1012 { | 1012 { |
1013 int sym_len = strlen(symbol); | 1013 int sym_len = SDL_strlen(symbol); |
1014 void *value = NULL; | 1014 void *value = NULL; |
1015 char *malloc_sym = NULL; | 1015 char *malloc_sym = NULL; |
1016 dolock(); | 1016 dolock(); |
1017 malloc_sym = malloc(sym_len + 2); | 1017 malloc_sym = SDL_malloc(sym_len + 2); |
1018 if (malloc_sym) | 1018 if (malloc_sym) |
1019 { | 1019 { |
1020 sprintf(malloc_sym, "_%s", symbol); | 1020 sprintf(malloc_sym, "_%s", symbol); |
1021 value = dlsymIntern(handle, malloc_sym, 1); | 1021 value = dlsymIntern(handle, malloc_sym, 1); |
1022 free(malloc_sym); | 1022 SDL_free(malloc_sym); |
1023 } | 1023 } |
1024 else | 1024 else |
1025 { | 1025 { |
1026 error("Unable to allocate memory"); | 1026 error("Unable to allocate memory"); |
1027 goto dlsymerror; | 1027 goto dlsymerror; |
1054 * | 1054 * |
1055 * Note that I haven't figured out yet which should be "standard", prepend | 1055 * Note that I haven't figured out yet which should be "standard", prepend |
1056 * the underscore always, or not at all. These global functions need to go away | 1056 * the underscore always, or not at all. These global functions need to go away |
1057 * for opendarwin. | 1057 * for opendarwin. |
1058 */ | 1058 */ |
1059 int sym_len = strlen(symbol); | 1059 int sym_len = SDL_strlen(symbol); |
1060 void *value = NULL; | 1060 void *value = NULL; |
1061 char *malloc_sym = NULL; | 1061 char *malloc_sym = NULL; |
1062 malloc_sym = malloc(sym_len + 2); | 1062 malloc_sym = SDL_malloc(sym_len + 2); |
1063 if (malloc_sym) | 1063 if (malloc_sym) |
1064 { | 1064 { |
1065 sprintf(malloc_sym, "_%s", symbol); | 1065 sprintf(malloc_sym, "_%s", symbol); |
1066 value = dlsymIntern(handle, malloc_sym, 1); | 1066 value = dlsymIntern(handle, malloc_sym, 1); |
1067 free(malloc_sym); | 1067 SDL_free(malloc_sym); |
1068 } | 1068 } |
1069 else | 1069 else |
1070 { | 1070 { |
1071 error("Unable to allocate memory"); | 1071 error("Unable to allocate memory"); |
1072 } | 1072 } |
1271 lc = (struct load_command *)((char *)mh + sizeof(struct mach_header)); | 1271 lc = (struct load_command *)((char *)mh + sizeof(struct mach_header)); |
1272 for (j = 0; j < mh->ncmds; j++, lc = (struct load_command *)((char *)lc + lc->cmdsize)) | 1272 for (j = 0; j < mh->ncmds; j++, lc = (struct load_command *)((char *)lc + lc->cmdsize)) |
1273 { | 1273 { |
1274 if (LC_SEGMENT == lc->cmd) | 1274 if (LC_SEGMENT == lc->cmd) |
1275 { | 1275 { |
1276 if (!strcmp(((struct segment_command *)lc)->segname, "__LINKEDIT")) | 1276 if (!SDL_strcmp(((struct segment_command *)lc)->segname, "__LINKEDIT")) |
1277 break; | 1277 break; |
1278 } | 1278 } |
1279 } | 1279 } |
1280 table_off = | 1280 table_off = |
1281 ((unsigned long)((struct segment_command *)lc)->vmaddr) - | 1281 ((unsigned long)((struct segment_command *)lc)->vmaddr) - |
1339 union | 1339 union |
1340 { | 1340 { |
1341 void *d; | 1341 void *d; |
1342 dlfunc_t f; | 1342 dlfunc_t f; |
1343 } rv; | 1343 } rv; |
1344 int sym_len = strlen(symbol); | 1344 int sym_len = SDL_strlen(symbol); |
1345 char *malloc_sym = NULL; | 1345 char *malloc_sym = NULL; |
1346 dolock(); | 1346 dolock(); |
1347 malloc_sym = malloc(sym_len + 2); | 1347 malloc_sym = SDL_malloc(sym_len + 2); |
1348 if (malloc_sym) | 1348 if (malloc_sym) |
1349 { | 1349 { |
1350 sprintf(malloc_sym, "_%s", symbol); | 1350 sprintf(malloc_sym, "_%s", symbol); |
1351 rv.d = dlsymIntern(handle, malloc_sym, 1); | 1351 rv.d = dlsymIntern(handle, malloc_sym, 1); |
1352 free(malloc_sym); | 1352 SDL_free(malloc_sym); |
1353 } | 1353 } |
1354 else | 1354 else |
1355 { | 1355 { |
1356 error("Unable to allocate memory"); | 1356 error("Unable to allocate memory"); |
1357 goto dlfuncerror; | 1357 goto dlfuncerror; |