--- parser3/src/targets/cgi/parser3.C 2024/08/25 13:36:55 1.351 +++ parser3/src/targets/cgi/parser3.C 2024/08/25 19:50:48 1.353 @@ -5,7 +5,7 @@ Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.351 2024/08/25 13:36:55 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.353 2024/08/25 19:50:48 moko Exp $"; #include "pa_config_includes.h" @@ -57,6 +57,15 @@ static char** argv_extra = NULL; static THREAD_LOCAL Request_info *request_info_4log = NULL; // global for correct log() reporting static const char* filespec_4log = NULL; // null only if system-wide auto.p used +template static T *dir_pos(T *fname){ + T *result=NULL; + while (fname=strpbrk(fname, "/\\")){ + result=fname; + fname++; + } + return result; +} + // SAPI static void pa_log(const char* fmt, va_list args) { @@ -76,16 +85,17 @@ static void pa_log(const char* fmt, va_l #endif if(!opened && filespec_4log) { - char beside_config_path[MAX_STRING]; - pa_strncpy(beside_config_path, filespec_4log, MAX_STRING); - if(!(rsplit(beside_config_path, '/') || rsplit(beside_config_path, '\\'))) { // strip filename + char log_spec[MAX_STRING + 12 /* '/parser3.log' */]; + pa_strncpy(log_spec, filespec_4log, MAX_STRING); + + if(char *log_dir_pos=dir_pos(log_spec)){ + strcpy(log_dir_pos, "/parser3.log"); + } else { // no path, just filename - strcpy(beside_config_path, "."); + strcpy(log_spec, "./parser3.log"); } - char file_spec[MAX_STRING]; - snprintf(file_spec, MAX_STRING, "%s/parser3.log", beside_config_path); - f=fopen(file_spec, "at"); + f=fopen(log_spec, "at"); opened=f!=0; } // fallback to stderr @@ -180,7 +190,7 @@ size_t SAPI::send_body(SAPI_Info& info, return info.send_body(buf, size); } -static const char *full_disk_path(const char* file_name = 0) { +static const char *full_disk_path(const char* file_name = "") { char *result; if(file_name[0]=='/' #ifdef WIN32 @@ -225,13 +235,21 @@ static const char *locate_config(const c if(!filespec_4log) filespec_4log=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME); if(!filespec_4log){ - // next to the executable - char *beside_executable_path = pa_strdup(executable_path); - bool stripped_filename = rsplit(beside_executable_path, '/') || rsplit(beside_executable_path, '\\'); - filespec_4log = pa_strcat(stripped_filename ? beside_executable_path : "." /* no path, just filename */ , "/" AUTO_FILE_NAME); - if(entry_exists(filespec_4log)) - return filespec_4log; + const char *exec_dir_pos = dir_pos(executable_path); +#ifdef SYSTEM_CONFIG_FILE + if(exec_dir_pos){ +#endif + // next to the executable + if(!exec_dir_pos || (exec_dir_pos==executable_path+1 && *executable_path=='.')){ + // just parser3.exe or ./parser3.cgi, full path to avoid "parser already configured" + filespec_4log=full_disk_path(AUTO_FILE_NAME); + } else { + filespec_4log=pa_strcat(pa_strdup(executable_path, exec_dir_pos - executable_path), "/" AUTO_FILE_NAME); + } + if(entry_exists(filespec_4log)) + return filespec_4log; #ifdef SYSTEM_CONFIG_FILE + } if(entry_exists(SYSTEM_CONFIG_FILE)){ filespec_4log=NULL; return SYSTEM_CONFIG_FILE; @@ -594,7 +612,7 @@ int main(int argc, char *argv[]) { log("main: entry"); #endif - parser3_filespec = argc ? argv[0] : "parser3"; + parser3_filespec = argc && argv[0] ? argv[0] : "parser3"; umask(2); // were we started as CGI?