diff --git a/libcob/ChangeLog b/libcob/ChangeLog index 5d6d2bd54..acad00810 100644 --- a/libcob/ChangeLog +++ b/libcob/ChangeLog @@ -677,6 +677,11 @@ having only its POINTER be "external" and use cob_file_malloc to allocate and set the attributes +2021-09-28 Simon Sobisch + + * common.c (cob_gettmpdir): do the lookup and validation only once, + also ensure for TMPDIR that it has no trailing slash + 2021-09-27 Simon Sobisch * common.c (call_exit_handlers_and_terminate): extracted from diff --git a/libcob/common.c b/libcob/common.c index 48215ab4f..6d10a0004 100644 --- a/libcob/common.c +++ b/libcob/common.c @@ -5642,45 +5642,57 @@ check_valid_env_tmpdir (const char *envname) static const char * cob_gettmpdir (void) { - const char *tmpdir; + static const char *tmpdir = NULL; char *tmp; - if ((tmpdir = check_valid_env_tmpdir ("TMPDIR")) == NULL) { - tmp = NULL; + if (tmpdir != NULL) { + return tmpdir; + } + + tmp = NULL; + /* target directory, also used by most called external tools*/ + if ((tmpdir = check_valid_env_tmpdir ("TMPDIR")) == NULL + /* OS specific temporary paths */ #ifdef _WIN32 - if ((tmpdir = check_valid_env_tmpdir ("TEMP")) == NULL - && (tmpdir = check_valid_env_tmpdir ("TMP")) == NULL - && (tmpdir = check_valid_env_tmpdir ("USERPROFILE")) == NULL) { + && (tmpdir = check_valid_env_tmpdir ("TEMP")) == NULL + && (tmpdir = check_valid_env_tmpdir ("TMP")) == NULL + && (tmpdir = check_valid_env_tmpdir ("USERPROFILE")) == NULL) { #else - if ((tmpdir = check_valid_env_tmpdir ("TMP")) == NULL - && (tmpdir = check_valid_env_tmpdir ("TEMP")) == NULL) { - if (!check_valid_dir ("/tmp")) { - tmp = cob_fast_malloc (5U); - strcpy (tmp, "/tmp"); - tmpdir = tmp; - } + && (tmpdir = check_valid_env_tmpdir ("TMP")) == NULL + && (tmpdir = check_valid_env_tmpdir ("TEMP")) == NULL) { + if (!check_valid_dir ("/tmp")) { + tmp = cob_fast_malloc (5U); + strcpy (tmp, "/tmp"); + tmpdir = tmp; } - if (!tmpdir) { #endif - tmp = cob_fast_malloc (2U); - tmp[0] = '.'; - tmp[1] = 0; + } + /* fallback if still not valid: current directory */ + if (!tmpdir) { + tmp = cob_fast_malloc (2U); + tmp[0] = '.'; + tmp[1] = 0; + tmpdir = tmp; + } else { + /* if we have a valid path: ensure there's no trailing slash */ + size_t size = strlen (tmpdir) - 1; + if (tmpdir[size] == SLASH_CHAR) { + tmp = (char*)cob_fast_malloc (size + 1); + memcpy (tmp, tmpdir, size); + tmp[size] = 0; tmpdir = tmp; - } else { - size_t size = strlen (tmpdir) - 1; - if (tmpdir[size] == SLASH_CHAR) { - tmp = (char*)cob_fast_malloc (size + 1); - memcpy (tmp, tmpdir, size); - tmp[size] = 0; - tmpdir = tmp; - } - } - (void)cob_setenv ("TMPDIR", tmpdir, 1); - if (tmp) { - cob_free ((void *)tmp); - tmpdir = getenv ("TMPDIR"); } } + /* ensure TMPDIR is set for called tools (which partially break hard otherwise) */ + (void)cob_setenv ("TMPDIR", tmpdir, 1); + + if (tmp) { + cob_free ((void *)tmp); + } + + /* get the pointer to the environment copy - as this may point to a different place - + store it for subsequent calls and finally return it to the caller */ + tmpdir = getenv ("TMPDIR"); return tmpdir; } diff --git a/tests/ChangeLog b/tests/ChangeLog index cad5845a3..6169228ca 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -64,6 +64,10 @@ * testsuite.src/listings.at: ignore all stderr in listing tests as those are checked via the listing output there +2021-09-28 Simon Sobisch + + * atlocal.in: workaround for MSYS issues + 2021-09-25 Ron Norman * atlocal.in,atlocal_win,atlocal_valgrind: Add -std=default to COBC diff --git a/tests/atlocal.in b/tests/atlocal.in index ff4e4332e..f054d9454 100644 --- a/tests/atlocal.in +++ b/tests/atlocal.in @@ -140,6 +140,12 @@ _return_path () { # Fix for testcases where cobc translates path to win32 equivalents if test "x$MSYSTEM" != "x"; then PATHSEP=";" + # finding the cobc executable is done from MSYS, but if this calls cobcrun + # it just uses the PATH variable which not necessarily has the correct + # order - especially if the current system has cobcrun installed - so + # let that cobc call COBCRUN with a full path (converted by MSYS into + # the Win32 variant when starting the cobc process) + COBCRUN=${ABS_COBCRUN} else PATHSEP='@PATH_SEPARATOR@'; fi diff --git a/tests/testsuite.src/run_file.at b/tests/testsuite.src/run_file.at index a7e9318fa..add3dc61a 100644 --- a/tests/testsuite.src/run_file.at +++ b/tests/testsuite.src/run_file.at @@ -6080,9 +6080,13 @@ AT_CHECK([$COBCRUN_DIRECT ./prog ./prog.cob], [0], AT_CHECK([DD_dot="." dd_dot="A" dot="B" $COBCRUN_DIRECT ./prog 'dot/prog.cob'], [0], [RUN WITH dot/prog.cob ], []) -AT_CHECK([DD_dot="" dd_dot="." dot="A" $COBCRUN_DIRECT ./prog 'dot/prog.cob'], [0], -[RUN WITH dot/prog.cob +# MinGW and other Win32 builds would fail this - because "getenv is not case sensitive" +# note: does not apply to cygwin; "AAA" wins over "aaa" +if test "$PATHSEP" != ";"; then + AT_CHECK([DD_dot="" dd_dot="." dot="A" $COBCRUN_DIRECT ./prog 'dot/prog.cob'], [0], + [RUN WITH dot/prog.cob ], []) +fi # MF would say (ignore for now as this conflicts with ACUCOBOL) don't resolve by $ # as it does not start with $: #AT_CHECK([DD_dot="" dd_dot="" dot="." $COBCRUN_DIRECT ./prog 'dot/prog.cob'], [0], @@ -6100,9 +6104,13 @@ error opening file ... AT_CHECK([DD_dot="." dd_dot="" dot="" $COBCRUN_DIRECT ./prog '$dot/prog.cob'], [0], [RUN WITH $dot/prog.cob ], []) -AT_CHECK([DD_dot="" dd_dot="." dot="" $COBCRUN_DIRECT ./prog '$dot/prog.cob'], [0], -[RUN WITH $dot/prog.cob +# MinGW and other Win32 builds would fail this - because "getenv is not case sensitive" +# note: does not apply to cygwin; "AAA" wins over "aaa" +if test "$PATHSEP" != ";"; then + AT_CHECK([DD_dot="" dd_dot="." dot="" $COBCRUN_DIRECT ./prog '$dot/prog.cob'], [0], + [RUN WITH $dot/prog.cob ], []) +fi AT_CHECK([DD_dot="" dd_dot="" dot="." $COBCRUN_DIRECT ./prog '$dot/prog.cob'], [0], [RUN WITH $dot/prog.cob ], []) @@ -6111,7 +6119,7 @@ AT_CHECK([DD_dot="" dd_dot="" dot="." $COBCRUN_DIRECT ./prog '$dot/prog.cob'], AT_CHECK([DD_dot="" dd_dot="" dot="" $COBCRUN_DIRECT ./prog '$dot/prog.cob'], [0], [RUN WITH $dot/prog.cob ], []) -# would _possibly_ fail with MF (docs are wrong here in any case...) +# would _possibly_ fail with MF (their docs do not match their behaviour in any case...) # but this is by GnuCOBOL design here: AT_CHECK([DD_dot="" dd_dot="" dot="" $COBCRUN_DIRECT ./prog '$dot/$dotter/prog.cob'], [0], [RUN WITH $dot/$dotter/prog.cob @@ -11994,7 +12002,7 @@ AT_DATA([prog.cob], [ MOVE READ-RANDOM-LOCK TO COBOL-OP. SET FCD-RECORD-ADDRESS TO ADDRESS OF RECORD-AREA. PERFORM A-200-CALL-EXTFH. - MOVE FCD-RELATIVE-KEY TO I. + MOVE FCD-RELATIVE-KEY TO I. A-400-REWRITE-RECORD. MOVE COBOL-TYPE TO ACTION-TYPE. MOVE REWRITE-RECORD TO COBOL-OP.