From 7276a925516c60f3b3796efbc8fd08b5dce996f9 Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Tue, 22 Dec 2009 21:00:27 -0800 Subject: [PATCH] Force working directory to user's home-dir when started as mac application bundle. --- src/microcode/option.c | 5 +-- src/microcode/pruxenv.c | 46 +---------------------- src/microcode/uxtop.c | 81 ++++++++++++++++++++++++++++++++++++++--- src/microcode/uxtop.h | 5 +++ 4 files changed, 83 insertions(+), 54 deletions(-) diff --git a/src/microcode/option.c b/src/microcode/option.c index 850cdd72a..8d43c5d22 100644 --- a/src/microcode/option.c +++ b/src/microcode/option.c @@ -30,6 +30,7 @@ USA. #include "fasl.h" #include "osenv.h" #include "osfs.h" +#include "uxtop.h" #include #define xfree(p) OS_free ((void *) (p)) @@ -73,10 +74,6 @@ USA. #endif #define FILE_READABLE(filename) (OS_file_access ((filename), 4)) - -#ifdef __APPLE__ - extern const char * macosx_main_bundle_dir (void); -#endif static bool option_summary; diff --git a/src/microcode/pruxenv.c b/src/microcode/pruxenv.c index 29cc0f174..1e0980122 100644 --- a/src/microcode/pruxenv.c +++ b/src/microcode/pruxenv.c @@ -28,6 +28,7 @@ USA. #include "scheme.h" #include "prims.h" #include "ux.h" +#include "uxtop.h" #include "uxtrap.h" extern const char * OS_current_user_name (void); @@ -36,10 +37,6 @@ extern const char * OS_current_user_home_directory (void); #ifdef HAVE_SOCKETS # include "uxsock.h" #endif - -#ifdef __APPLE__ -# include -#endif DEFINE_PRIMITIVE ("FILE-TIME->STRING", Prim_file_time_to_string, 1, 1, "Convert a file system time stamp into a date/time string.") @@ -241,47 +238,6 @@ DEFINE_PRIMITIVE ("INSTRUCTION-ADDRESS->COMPILED-CODE-BLOCK", #endif } -#ifdef __APPLE__ -const char * -macosx_main_bundle_dir (void) -{ - CFBundleRef bundle; - CFURLRef url; - UInt8 buffer [4096]; - char * bp; - char * result; - - bundle = (CFBundleGetMainBundle()); - if (bundle == 0) - return (0); - - url = (CFBundleCopyResourceURL (bundle, (CFSTR ("all")), (CFSTR ("com")), 0)); - if (url == 0) - return (0); - - if (!CFURLGetFileSystemRepresentation (url, true, buffer, (sizeof (buffer)))) - { - CFRelease (url); - return (0); - } - CFRelease (url); - bp = ((char *) buffer); - - /* Discard everything after the final slash. */ - { - char * slash = (strrchr (bp, '/')); - if (slash != 0) - (*slash) = '\0'; - } - - result = (UX_malloc ((strlen (bp)) + 1)); - if (result != 0) - strcpy (result, bp); - - return (result); -} -#endif - DEFINE_PRIMITIVE ("MACOSX-MAIN-BUNDLE-DIR", Prim_macosx_main_bundle_dir, 0, 0, 0) { diff --git a/src/microcode/uxtop.c b/src/microcode/uxtop.c index 3c2ce38ed..b7f3404e5 100644 --- a/src/microcode/uxtop.c +++ b/src/microcode/uxtop.c @@ -26,6 +26,7 @@ USA. #include "ux.h" #include "uxtop.h" #include "osctty.h" +#include "osenv.h" #include "uxutil.h" #include "errors.h" #include "option.h" @@ -33,6 +34,12 @@ USA. #include "object.h" #include "extern.h" +#ifdef __APPLE__ +# include + extern const char * OS_current_user_home_directory (void); + static CFURLRef macosx_default_band_url (void); +#endif + extern void UX_initialize_channels (void); extern void UX_initialize_ctty (int interactive); extern void UX_initialize_directory_reader (void); @@ -98,6 +105,16 @@ OS_initialize (void) #if defined(_SUNOS) || defined(_SUNOS3) || defined(_SUNOS4) vadvise (VA_ANOM); /* Anomalous paging, don't try to guess. */ #endif +#ifdef __APPLE__ + /* If in MacOS X application bundle, force working directory to + user's home directory. */ + if (macosx_in_app_p ()) + { + const char * home_dir = OS_current_user_home_directory (); + if (home_dir != 0) + OS_set_working_dir_pathname (home_dir); + } +#endif } void @@ -113,11 +130,8 @@ OS_announcement (void) void OS_reset (void) { - /* - There should really be a reset for each initialize above, - but the rest seem innocuous. - */ - + /* There should really be a reset for each initialize above, but the + rest seem innocuous. */ UX_reset_channels (); UX_reset_terminals (); UX_reset_processes (); @@ -180,6 +194,63 @@ OS_restore_external_state (void) UX_ctty_restore_external_state (); } +#ifdef __APPLE__ + +const char * +macosx_main_bundle_dir (void) +{ + CFURLRef url = (macosx_default_band_url ()); + UInt8 buffer [4096]; + char * bp; + char * result; + + if (url == 0) + return (0); + + if (!CFURLGetFileSystemRepresentation (url, true, buffer, (sizeof (buffer)))) + { + CFRelease (url); + return (0); + } + CFRelease (url); + bp = ((char *) buffer); + + /* Discard everything after the final slash. */ + { + char * slash = (strrchr (bp, '/')); + if (slash != 0) + (*slash) = '\0'; + } + + result = (UX_malloc ((strlen (bp)) + 1)); + if (result != 0) + strcpy (result, bp); + + return (result); +} + +bool +macosx_in_app_p (void) +{ + CFURLRef url = (macosx_default_band_url ()); + if (url == 0) + return (false); + CFRelease (url); + return (true); +} + +static CFURLRef +macosx_default_band_url (void) +{ + CFBundleRef bundle = (CFBundleGetMainBundle()); + return + ((bundle != 0) + ? (CFBundleCopyResourceURL (bundle, (CFSTR ("all")), (CFSTR ("com")), 0)) + : 0); +} + +#endif + enum syserr_names OS_error_code_to_syserr (int code) { diff --git a/src/microcode/uxtop.h b/src/microcode/uxtop.h index 996eeddeb..a846264b7 100644 --- a/src/microcode/uxtop.h +++ b/src/microcode/uxtop.h @@ -30,4 +30,9 @@ USA. extern void UX_dump_core (void); +#ifdef __APPLE__ + extern bool macosx_in_app_p (void); + extern const char * macosx_main_bundle_dir (void); +#endif + #endif /* SCM_UXTOP_H */ -- 2.25.1