diff options
author | Xavier Leroy <xavier.leroy@inria.fr> | 2002-02-11 13:51:40 +0000 |
---|---|---|
committer | Xavier Leroy <xavier.leroy@inria.fr> | 2002-02-11 13:51:40 +0000 |
commit | 429efbf7c2416a997d2ee5ca2d790cfdc944358b (patch) | |
tree | 13ea7ba24ad95fef48af5f6bffdee0c2f861c99d | |
parent | 7f765d521eb9014f42fdf0a83d3c31be897d8dd6 (diff) |
Ajout Sys.executable_name, laisser Sys.argv.(0) inchange (PR#817)
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@4375 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r-- | asmrun/startup.c | 16 | ||||
-rw-r--r-- | byterun/backtrace.c | 2 | ||||
-rw-r--r-- | byterun/main.c | 13 | ||||
-rw-r--r-- | byterun/startup.c | 23 | ||||
-rw-r--r-- | byterun/sys.c | 15 | ||||
-rw-r--r-- | byterun/sys.h | 4 | ||||
-rw-r--r-- | stdlib/sys.ml | 4 | ||||
-rw-r--r-- | stdlib/sys.mli | 3 |
8 files changed, 54 insertions, 26 deletions
diff --git a/asmrun/startup.c b/asmrun/startup.c index 317339fac..8cd9ea412 100644 --- a/asmrun/startup.c +++ b/asmrun/startup.c @@ -113,6 +113,11 @@ extern void init_signals (void); void caml_main(char **argv) { + char * exe_name; +#ifdef __linux__ + static char proc_self_exe[256]; + int retcode; +#endif value res; init_ieee_floats(); @@ -125,7 +130,16 @@ void caml_main(char **argv) percent_free_init, max_percent_free_init); init_atoms(); init_signals(); - sys_init(argv); + exe_name = argv[0]; +#ifdef __linux__ + /* Recover executable name from /proc/self/exe, much more reliable */ + retcode = readlink("/proc/self/exe", proc_self_exe, sizeof(proc_self_exe)); + if (retcode != -1 && retcode < sizeof(proc_self_exe)) { + proc_self_exe[retcode] = 0; + exe_name = proc_self_exe; + } +#endif + sys_init(exe_name, argv); res = caml_start_program(); if (Is_exception_result(res)) fatal_uncaught_exception(Extract_exception(res)); diff --git a/byterun/backtrace.c b/byterun/backtrace.c index 4425ecd3a..2df3be510 100644 --- a/byterun/backtrace.c +++ b/byterun/backtrace.c @@ -101,7 +101,7 @@ static value read_debug_info(void) uint32 num_events, orig, i; value evl, l; - exec_name = caml_main_argv[0]; + exec_name = caml_exe_name; fd = attempt_open(&exec_name, &trail, 1); if (fd < 0) CAMLreturn(Val_false); read_section_descriptors(fd, &trail); diff --git a/byterun/main.c b/byterun/main.c index acc5acf34..9afd3f2be 100644 --- a/byterun/main.c +++ b/byterun/main.c @@ -22,10 +22,6 @@ extern void caml_main (char **); -#ifdef __linux__ -#include <unistd.h> -#endif - #ifdef _WIN32 extern void expand_command_line (int *, char ***); #endif @@ -41,15 +37,6 @@ int main(int argc, char **argv) /* Expand wildcards and diversions in command line */ expand_command_line(&argc, &argv); #endif -#ifdef __linux__ - /* Recover argv[0] from /proc/self/exe, much more reliable */ - char exename[1024]; - int retcode = readlink("/proc/self/exe", exename, sizeof(exename)); - if (retcode != -1 && retcode < sizeof(exename)) { - exename[retcode] = 0; - argv[0] = exename; - } -#endif #if macintosh rotatecursor_options (&something_to_do, 0, NULL); #endif /* macintosh */ diff --git a/byterun/startup.c b/byterun/startup.c index b0e32e4c7..9daa3f1dc 100644 --- a/byterun/startup.c +++ b/byterun/startup.c @@ -317,6 +317,11 @@ CAMLexport void caml_main(char **argv) struct channel * chan; value res; char * shared_lib_path, * shared_libs, * req_prims; + char * exe_name; +#ifdef __linux__ + static char proc_self_exe[256]; + int retcode; +#endif /* Machine-dependent initialization of the floating-point hardware so that it behaves as much as possible as specified in IEEE */ @@ -330,12 +335,22 @@ CAMLexport void caml_main(char **argv) #endif parse_camlrunparam(); pos = 0; - fd = attempt_open(&argv[0], &trail, 0); + exe_name = argv[0]; +#ifdef __linux__ + /* Recover executable name from /proc/self/exe, much more reliable */ + retcode = readlink("/proc/self/exe", proc_self_exe, sizeof(proc_self_exe)); + if (retcode != -1 && retcode < sizeof(proc_self_exe)) { + proc_self_exe[retcode] = 0; + exe_name = proc_self_exe; + } +#endif + fd = attempt_open(&exe_name, &trail, 0); if (fd < 0) { pos = parse_command_line(argv); if (argv[pos] == 0) fatal_error("No bytecode file specified.\n"); - fd = attempt_open(&argv[pos], &trail, 1); + exe_name = argv[pos]; + fd = attempt_open(&exe_name, &trail, 1); switch(fd) { case FILE_NOT_FOUND: fatal_error_arg("Fatal error: cannot find file %s\n", argv[pos]); @@ -381,7 +396,7 @@ CAMLexport void caml_main(char **argv) oldify_mopup (); /* Initialize system libraries */ init_exceptions(); - sys_init(argv + pos); + sys_init(exe_name, argv + pos); #ifdef _WIN32 /* Start a thread to handle signals */ if (getenv("CAMLSIGPIPE")) @@ -435,7 +450,7 @@ CAMLexport void caml_startup_code(code_t code, asize_t code_size, oldify_mopup (); /* Run the code */ init_exceptions(); - sys_init(argv); + sys_init("", argv); res = interprete(start_code, code_size); if (Is_exception_result(res)) fatal_uncaught_exception(Extract_exception(res)); diff --git a/byterun/sys.c b/byterun/sys.c index 4a2195a2e..cb70ba345 100644 --- a/byterun/sys.c +++ b/byterun/sys.c @@ -220,15 +220,24 @@ CAMLprim value sys_getenv(value var) return copy_string(res); } -char ** caml_main_argv; +char * caml_exe_name; +static char ** caml_main_argv; CAMLprim value sys_get_argv(value unit) { - return copy_string_array((char const **) caml_main_argv); + CAMLparam0 (); /* unit is unused */ + CAMLlocal3 (exe_name, argv, res); + exe_name = copy_string(caml_exe_name); + argv = copy_string_array((char const **) caml_main_argv); + res = alloc_small(2, 0); + Field(res, 0) = exe_name; + Field(res, 1) = argv; + CAMLreturn(res); } -void sys_init(char **argv) +void sys_init(char * exe_name, char **argv) { + caml_exe_name = exe_name; caml_main_argv = argv; } diff --git a/byterun/sys.h b/byterun/sys.h index 38768a45f..6b1ac1734 100644 --- a/byterun/sys.h +++ b/byterun/sys.h @@ -21,9 +21,9 @@ #define NO_ARG Val_int(0) CAMLextern void sys_error (value); -extern void sys_init (char **); +extern void sys_init (char * exe_name, char ** argv); CAMLextern value sys_exit (value); -extern char ** caml_main_argv; +extern char * caml_exe_name; #endif /* _sys_ */ diff --git a/stdlib/sys.ml b/stdlib/sys.ml index b666a5a8a..9da1b6c48 100644 --- a/stdlib/sys.ml +++ b/stdlib/sys.ml @@ -16,9 +16,9 @@ (* System interface *) external get_config: unit -> string * int = "sys_get_config" -external get_argv: unit -> string array = "sys_get_argv" +external get_argv: unit -> string * string array = "sys_get_argv" -let argv = get_argv() +let (executable_name, argv) = get_argv() let (os_type, word_size) = get_config() let max_array_length = (1 lsl (word_size - 10)) - 1;; let max_string_length = word_size / 8 * max_array_length - 1;; diff --git a/stdlib/sys.mli b/stdlib/sys.mli index 5333628e2..f733c3ef1 100644 --- a/stdlib/sys.mli +++ b/stdlib/sys.mli @@ -21,6 +21,9 @@ val argv : string array The following elements are the command-line arguments given to the program. *) +val executable_name : string +(** The name of the file containing the executable currently running. *) + external file_exists : string -> bool = "sys_file_exists" (** Test if a file with the given name exists. *) |