From 05442dfd86e01ae3248e71eb0cf7128adc03e30f Mon Sep 17 00:00:00 2001 From: David Declerck Date: Sun, 2 Jun 2024 00:33:24 +0200 Subject: [PATCH] Refactor cob_chk_file_mapping --- libcob/fileio.c | 101 +++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 61 deletions(-) diff --git a/libcob/fileio.c b/libcob/fileio.c index d4873fac9..4874cf7c3 100644 --- a/libcob/fileio.c +++ b/libcob/fileio.c @@ -1633,6 +1633,43 @@ do_acu_hypen_translation (char *src) strncpy (file_open_name, file_open_buff, (size_t)COB_FILE_MAX); } +/* apply COB_FILE_PATH if set (similar to ACUCOBOL's FILE-PREFIX) + MF and Fujistu simply don't have that - not set by default, + so no compatilibity issue here */ +static void +apply_file_paths (void) +{ + int k; + if (file_paths) { + for(k=0; file_paths[k] != NULL; k++) { + snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", + file_paths[k], SLASH_CHAR, file_open_name); + file_open_buff[COB_FILE_MAX] = 0; + if (access (file_open_buff, F_OK) == 0) { + break; + } +#if defined(WITH_CISAM) || defined(WITH_DISAM) || defined(WITH_VBISAM) || defined(WITH_VISAM) + /* ISAM may append '.dat' to file name */ + snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s.dat", + file_paths[k], SLASH_CHAR, file_open_name); + file_open_buff[COB_FILE_MAX] = 0; + if (access (file_open_buff, F_OK) == 0) { + snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", + file_paths[k], SLASH_CHAR, file_open_name); + file_open_buff[COB_FILE_MAX] = 0; + break; + } +#endif + } + if (file_paths[k] == NULL) { + snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", + file_paths[0], SLASH_CHAR, file_open_name); + file_open_buff[COB_FILE_MAX] = 0; + } + strncpy (file_open_name, file_open_buff, (size_t)COB_FILE_MAX); + } +} + void cob_chk_file_mapping (cob_file *f, char *filename) { @@ -1642,7 +1679,7 @@ cob_chk_file_mapping (cob_file *f, char *filename) char *saveptr; char *orig; unsigned int dollar, badchar; - int k, qt; + int qt; if (f != NULL) memset (f->file_status, '0', (size_t)2); @@ -1717,37 +1754,7 @@ cob_chk_file_mapping (cob_file *f, char *filename) return ; } } - /* apply COB_FILE_PATH if set (similar to ACUCOBOL's FILE-PREFIX) - MF and Fujistu simply don't have that - not set by default, - so no compatilibity issue here */ - if (file_paths) { - for(k=0; file_paths[k] != NULL; k++) { - snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", - file_paths[k], SLASH_CHAR, file_open_name); - file_open_buff[COB_FILE_MAX] = 0; - if (access (file_open_buff, F_OK) == 0) { - break; - } -#if defined(WITH_CISAM) || defined(WITH_DISAM) || defined(WITH_VBISAM) || defined(WITH_VISAM) - /* ISAM may append '.dat' to file name */ - snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s.dat", - file_paths[k], SLASH_CHAR, file_open_name); - file_open_buff[COB_FILE_MAX] = 0; - if (access (file_open_buff, F_OK) == 0) { - snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", - file_paths[k], SLASH_CHAR, file_open_name); - file_open_buff[COB_FILE_MAX] = 0; - break; - } -#endif - } - if (file_paths[k] == NULL) { - snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", - file_paths[0], SLASH_CHAR, file_open_name); - file_open_buff[COB_FILE_MAX] = 0; - } - strncpy (file_open_name, file_open_buff, (size_t)COB_FILE_MAX); - } + apply_file_paths (); return; } @@ -1832,36 +1839,8 @@ cob_chk_file_mapping (cob_file *f, char *filename) if (looks_absolute (file_open_name)) { return; } - /* apply COB_FILE_PATH if set (similar to ACUCOBOL's FILE-PREFIX) */ - if (file_paths) { - for(k=0; file_paths[k] != NULL; k++) { - snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", - file_paths[k], SLASH_CHAR, file_open_name); - file_open_buff[COB_FILE_MAX] = 0; - if (access (file_open_buff, F_OK) == 0) { - break; - } -#if defined(WITH_CISAM) || defined(WITH_DISAM) || defined(WITH_VBISAM) || defined(WITH_VISAM) - /* ISAM may append '.dat' to file name */ - snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s.dat", - file_paths[k], SLASH_CHAR, file_open_name); - file_open_buff[COB_FILE_MAX] = 0; - if (access (file_open_buff, F_OK) == 0) { - snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", - file_paths[k], SLASH_CHAR, file_open_name); - file_open_buff[COB_FILE_MAX] = 0; - break; - } -#endif - } - if (file_paths[k] == NULL) { - snprintf (file_open_buff, (size_t)COB_FILE_MAX, "%s%c%s", - file_paths[0], SLASH_CHAR, file_open_name); - file_open_buff[COB_FILE_MAX] = 0; - } - strncpy (file_open_name, file_open_buff, (size_t)COB_FILE_MAX); - } + apply_file_paths (); } void