Move the implemetation of GetExeName() from psqlodbc.c(psqlsetup.c) to mylog.c.
authorHiroshi Inoue <h-inoue@dream.email.ne.jp>
Thu, 24 Aug 2017 02:31:10 +0000 (11:31 +0900)
committerHiroshi Inoue <h-inoue@dream.email.ne.jp>
Thu, 24 Aug 2017 02:31:10 +0000 (11:31 +0900)
mylog.c
psqlodbc.c
psqlsetup.c

diff --git a/mylog.c b/mylog.c
index bd5d9203ee46bf8d5b187e30f9bf2d9ce5e604d9..8a9417220b19b05880ca3b34230ab3e07ae508c7 100644 (file)
--- a/mylog.c
+++ b/mylog.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include <ctype.h>
 #include <time.h>
 
 #ifndef WIN32
@@ -105,6 +106,59 @@ generate_homefile(const char *prefix, char *filename, size_t filenamelen)
    return;
 }
 
+#ifdef  WIN32
+static  char    exename[_MAX_FNAME];
+#elif   defined MAXNAMELEN
+static  char    exename[MAXNAMELEN];
+#else
+static  char    exename[256];
+#endif
+
+const char *GetExeProgramName()
+{
+   static  int init = 1;
+
+   if (init)
+   {
+       UCHAR    *p;
+#ifdef  WIN32
+       char    pathname[_MAX_PATH];
+
+       if (GetModuleFileName(NULL, pathname, sizeof(pathname)) > 0)
+       _splitpath(pathname, NULL, NULL, exename, NULL);
+#else
+       CSTR flist[] = {"/proc/self/exe", "/proc/curproc/file", "/proc/curproc/exe" };
+       int     i;
+       char    path_name[256];
+
+       for (i = 0; i < sizeof(flist) / sizeof(flist[0]); i++)
+       {
+           if (readlink(flist[i], path_name, sizeof(path_name)) > 0)
+           {
+               /* fprintf(stderr, "i=%d pathname=%s\n", i, path_name); */
+               STRCPY_FIXED(exename, po_basename(path_name));
+               break;
+           }
+       }
+#endif /* WIN32 */
+       for (p = (UCHAR *) exename; '\0' != *p; p++)
+       {
+           if (isalnum(*p))
+               continue;
+           switch (*p)
+           {
+               case '_':
+               case '-':
+                   continue;
+           }
+           *p = '\0';      /* avoid multi bytes for safety */
+           break;
+       }
+       init = 0;
+   }
+   return exename;
+}
+
 #if defined(WIN_MULTITHREAD_SUPPORT)
 static CRITICAL_SECTION    qlog_cs, mylog_cs;
 #elif defined(POSIX_MULTITHREAD_SUPPORT)
index 38b467f87d08f8171588e62f521af0dd82bb07e5..b12916d4b5c2534cb8ccce0a25e18fe87a9be922 100644 (file)
 #include <libgen.h>
 #endif
 
-#ifdef WIN32
-static char    exename[_MAX_FNAME];
-#elif  defined MAXNAMELEN
-static char    exename[MAXNAMELEN];
-#else
-static char    exename[256];
-#endif
-
 static int exepgm = 0;
 BOOL isMsAccess(void) {return 1 == exepgm;}
 BOOL isMsQuery(void) {return 2 == exepgm;}
 BOOL isSqlServr(void) {return 3 == exepgm;}
 
-const char *GetExeProgramName()
-{
-   int init = 1;
-
-   if (init)
-   {
-       char    *p;
-
-       for (p = exename; '\0' != *p; p++)
-       {
-           if ((UCHAR) *p >= 0x80)
-           {
-               *p = '\0';  /* avoid multi bytes for safety */
-               break;
-           }
-       }
-       init = 0;
-   }
-   return exename;
-}
 
 RETCODE SQL_API SQLDummyOrdinal(void);
 
@@ -140,25 +112,20 @@ HINSTANCE s_hModule;      /* Saved module handle. */
 BOOL       WINAPI
 DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
 {
+   const char *exename = GetExeProgramName();
+
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            s_hModule = hInst;  /* Save for dialog boxes */
 
-           {
-               char    pathname[_MAX_PATH];
-
-               if (GetModuleFileName(NULL, pathname, sizeof(pathname)) > 0)
-               {
-                   _splitpath(pathname, NULL, NULL, exename, NULL);
-                   if (stricmp(exename, "msaccess") == 0)
-                       exepgm = 1;
-                   else if (strnicmp(exename, "msqry", 5) == 0)
-                       exepgm = 2;
-                   else if (strnicmp(exename, "sqlservr", 8) == 0)
-                       exepgm = 3;
-               }
-           }
+
+           if (stricmp(exename, "msaccess") == 0)
+               exepgm = 1;
+           else if (strnicmp(exename, "msqry", 5) == 0)
+               exepgm = 2;
+           else if (strnicmp(exename, "sqlservr", 8) == 0)
+               exepgm = 3;
            initialize_global_cs();
            MYLOG(0, "exe name=%s\n", exename);
            break;
@@ -187,23 +154,6 @@ DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
 
 #else                          /* not WIN32 */
 
-CSTR flist[] = {"/proc/self/exe", "/proc/curproc/file", "/proc/curproc/exe" };
-static void getExeName()
-{
-   int i;
-   char    path_name[256];
-
-   for (i = 0; i < sizeof(flist) / sizeof(flist[0]); i++)
-   {
-       if (readlink(flist[i], path_name, sizeof(path_name)) > 0)
-       {
-           /* fprintf(stderr, "i=%d pathname=%s\n", i, path_name); */
-           STRCPY_FIXED(exename, basename(path_name));
-           break;
-       }
-   }
-}
-
 #ifdef __GNUC__
 
 /* Shared library initializer and destructor, using gcc's attributes */
@@ -212,7 +162,6 @@ static void
 __attribute__((constructor))
 psqlodbc_init(void)
 {
-   getExeName();
    initialize_global_cs();
 }
 
index 8b69ead705226fa8d13da8ca837fc04eddd200b2..a46b3b3e73ffdd6af74508e5e11b7f05d00c0879 100644 (file)
@@ -60,11 +60,6 @@ CSTR psqlodbc = "psqlodbc35w";
 CSTR   psqlodbc = "psqlodbc30a";
 #endif
 
-static char exename[_MAX_PATH];
-const char *GetExeProgramName()
-{
-   return exename;
-}
 
 BOOL       WINAPI
 DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
@@ -75,12 +70,6 @@ DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
    {
        case DLL_PROCESS_ATTACH:
            s_hModule = hInst;  /* Save for dialog boxes */
-           {
-               char    pathname[_MAX_PATH];
-
-               if (GetModuleFileName(NULL, pathname, sizeof(pathname)) > 0)
-                   _splitpath(pathname, NULL, NULL, exename, NULL);
-           }
            initialize_global_cs();
 #ifdef PG_BIN
            if (s_hLModule = LoadLibraryEx(PG_BIN "\\libpq.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH), s_hLModule == NULL)