Thread: Re: [pgsql-hackers-win32] 8.0 Beta 1 // Environment Vars // .pgpass

Re: [pgsql-hackers-win32] 8.0 Beta 1 // Environment Vars // .pgpass

From
Bruce Momjian
Date:
I have applied the attached patch which adds get_home_path() which uses
USERPROFILE on Win32 and HOME on Unix.

This should fix the reported problem.

---------------------------------------------------------------------------

Andrew Dunstan wrote:
>
>
> Joerg Hessdoerfer wrote:
>
> >On Thursday 12 August 2004 06:25, Tom Lane wrote:
> >
> >
> >>"efesar (the kay)" <efesar@unm.edu> writes:
> >>
> >>
> >>>The 8.0 version does not acknowledge env vars like PGPASSWORD and seems
> >>>to ignore the contents of the .pgpass file.
> >>>
> >>>
> >>Hmmm.   Those are probably the same problem, since finding .pgpass
> >>depends on expanding the $HOME env var.  But I have no idea why
> >>env vars aren't working for you ...
> >>
> >>
> >Yep. AFAIK, $HOME doesn't always exist in WIN32, but $HOMEDRIVE and $HOMEPATH
> >(or %HOMEPATH% and %HOMEDRIVE% ;-) do. Perhaps PG needs to do some munging to
> >build the home path from those two?
> >
> >
> >
> >
>
> On my XP-Pro machine the appropriate setting appears to be %USERPROFILE%
> - %HOMEPATH% is just "\"
>
> See here for a complete list of available environment vars:
>
> http://www.winnetmag.com/Article/ArticleID/23873/23873.html
>
> cheers
>
> andrew
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/bin/psql/common.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/common.c,v
retrieving revision 1.87
diff -c -c -r1.87 common.c
*** src/bin/psql/common.c    23 May 2004 22:20:10 -0000    1.87
--- src/bin/psql/common.c    18 Aug 2004 02:48:08 -0000
***************
*** 1078,1090 ****
      if (**filename == '~')
      {
          char       *fn;
-         char       *home;
          char        oldp,
                     *p;
          struct passwd *pw;

          fn = *filename;
!         home = NULL;

          p = fn + 1;
          while (*p != '/' && *p != '\0')
--- 1078,1090 ----
      if (**filename == '~')
      {
          char       *fn;
          char        oldp,
                     *p;
          struct passwd *pw;
+         char        home[MAXPGPATH];

          fn = *filename;
!         *home = '\0';

          p = fn + 1;
          while (*p != '/' && *p != '\0')
***************
*** 1094,1105 ****
          *p = '\0';

          if (*(fn + 1) == '\0')
!             home = getenv("HOME");
          else if ((pw = getpwnam(fn + 1)) != NULL)
!             home = pw->pw_dir;

          *p = oldp;
!         if (home)
          {
              char       *newfn;

--- 1094,1105 ----
          *p = '\0';

          if (*(fn + 1) == '\0')
!             get_home_path(home);
          else if ((pw = getpwnam(fn + 1)) != NULL)
!             StrNCpy(home, pw->pw_dir, MAXPGPATH);

          *p = oldp;
!         if (strlen(home) != 0)
          {
              char       *newfn;

Index: src/bin/psql/input.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/input.c,v
retrieving revision 1.34
diff -c -c -r1.34 input.c
*** src/bin/psql/input.c    25 Jan 2004 03:07:22 -0000    1.34
--- src/bin/psql/input.c    18 Aug 2004 02:48:08 -0000
***************
*** 171,177 ****
  #ifdef USE_READLINE
      if (flags & 1)
      {
!         const char *home;

          useReadline = true;
          initialize_readline();
--- 171,177 ----
  #ifdef USE_READLINE
      if (flags & 1)
      {
!         char home[MAXPGPATH];

          useReadline = true;
          initialize_readline();
***************
*** 180,187 ****
          if (GetVariable(pset.vars, "HISTSIZE") == NULL)
              SetVariable(pset.vars, "HISTSIZE", "500");
          using_history();
!         home = getenv("HOME");
!         if (home)
          {
              char *psql_history;

--- 180,186 ----
          if (GetVariable(pset.vars, "HISTSIZE") == NULL)
              SetVariable(pset.vars, "HISTSIZE", "500");
          using_history();
!         if (get_home_path(home))
          {
              char *psql_history;

***************
*** 231,240 ****
  #ifdef USE_READLINE
      if (useHistory)
      {
!         char       *home;

!         home = getenv("HOME");
!         if (home)
          {
              char    *psql_history;
              int         hist_size;
--- 230,238 ----
  #ifdef USE_READLINE
      if (useHistory)
      {
!         char       home[MAXPGPATH];

!         if (get_home_path(home))
          {
              char    *psql_history;
              int         hist_size;
Index: src/bin/psql/startup.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/startup.c,v
retrieving revision 1.95
diff -c -c -r1.95 startup.c
*** src/bin/psql/startup.c    3 Jun 2004 00:07:37 -0000    1.95
--- src/bin/psql/startup.c    18 Aug 2004 02:48:09 -0000
***************
*** 570,577 ****
  static void
  process_psqlrc(char *argv0)
  {
-     char       *home;
      char       *psqlrc;
      char       global_file[MAXPGPATH];
      char       my_exec_path[MAXPGPATH];
      char       etc_path[MAXPGPATH];
--- 570,577 ----
  static void
  process_psqlrc(char *argv0)
  {
      char       *psqlrc;
+     char       home[MAXPGPATH];
      char       global_file[MAXPGPATH];
      char       my_exec_path[MAXPGPATH];
      char       etc_path[MAXPGPATH];
***************
*** 582,588 ****
      snprintf(global_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
      process_psqlrc_file(global_file);

!     if ((home = getenv("HOME")) != NULL)
      {
          psqlrc = pg_malloc(strlen(home) + 1 + strlen(PSQLRC) + 1);
          sprintf(psqlrc, "%s/%s", home, PSQLRC);
--- 582,588 ----
      snprintf(global_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
      process_psqlrc_file(global_file);

!     if (get_home_path(home))
      {
          psqlrc = pg_malloc(strlen(home) + 1 + strlen(PSQLRC) + 1);
          sprintf(psqlrc, "%s/%s", home, PSQLRC);
Index: src/include/port.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/include/port.h,v
retrieving revision 1.53
diff -c -c -r1.53 port.h
*** src/include/port.h    17 Aug 2004 14:38:38 -0000    1.53
--- src/include/port.h    18 Aug 2004 02:48:10 -0000
***************
*** 50,55 ****
--- 50,56 ----
  extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
  extern void get_locale_path(const char *my_exec_path, char *ret_path);
  extern void set_pglocale_pgservice(const char *argv0, const char *app);
+ extern bool get_home_path(char *ret_path);

  /*
   *    is_absolute_path
***************
*** 74,82 ****
  #endif


-
-
-
  /* Portable way to find binaries */
  extern int find_my_exec(const char *argv0, char *retpath);
  extern int find_other_exec(const char *argv0, const char *target,
--- 75,80 ----
***************
*** 104,109 ****
--- 102,113 ----
  #define SYSTEMQUOTE ""
  #endif

+ #ifdef WIN32
+ #define HOMEDIR    "USERPROFILE"
+ #else
+ #define HOMEDIR    "HOME"
+ #endif
+
  /* Portable delay handling */
  extern void pg_usleep(long microsec);

Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.280
diff -c -c -r1.280 fe-connect.c
*** src/interfaces/libpq/fe-connect.c    17 Aug 2004 04:24:23 -0000    1.280
--- src/interfaces/libpq/fe-connect.c    18 Aug 2004 02:48:16 -0000
***************
*** 3093,3099 ****
  {
      FILE       *fp;
      char       *pgpassfile;
!     char       *home;
      struct stat stat_buf;

  #define LINELEN NAMEDATALEN*5
--- 3093,3099 ----
  {
      FILE       *fp;
      char       *pgpassfile;
!     char       home[MAXPGPATH];
      struct stat stat_buf;

  #define LINELEN NAMEDATALEN*5
***************
*** 3112,3119 ****
          port = DEF_PGPORT_STR;

      /* Look for it in the home dir */
!     home = getenv("HOME");
!     if (!home)
          return NULL;

      pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1);
--- 3112,3118 ----
          port = DEF_PGPORT_STR;

      /* Look for it in the home dir */
!     if (!get_home_path(home))
          return NULL;

      pgpassfile = malloc(strlen(home) + 1 + strlen(PGPASSFILE) + 1);
Index: src/port/path.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/port/path.c,v
retrieving revision 1.30
diff -c -c -r1.30 path.c
*** src/port/path.c    13 Aug 2004 14:47:23 -0000    1.30
--- src/port/path.c    18 Aug 2004 02:48:20 -0000
***************
*** 371,376 ****
--- 371,397 ----


  /*
+  *    get_include_path
+  */
+ bool
+ get_home_path(char *ret_path)
+ {
+     if (getenv(HOMEDIR) == NULL)
+     {
+         *ret_path = '\0';
+         return false;
+     }
+     else
+     {
+         StrNCpy(ret_path, getenv(HOMEDIR), MAXPGPATH);
+         canonicalize_path(ret_path);
+         return true;
+     }
+ }
+
+
+
+ /*
   *    make_relative - adjust path to be relative to bin/
   */
  static void