psql \i handling ~ in specified file name - Mailing list pgsql-patches

From Zach Irmen
Subject psql \i handling ~ in specified file name
Date
Msg-id 005501c3c77b$85a39160$0b01010a@ed.shawcable.net
Whole thread Raw
Responses Re: psql \i handling ~ in specified file name
Re: psql \i handling ~ in specified file name
List pgsql-patches
refers to following item on TODO
Have psql '\i ~/<tab><tab>' actually load files it displays from home dir

A reviewable patch that could handle tilde expansion for
psql \i ~/filename
or
psql \i ~username/filename

--
Zach Irmen


cvs server: Diffing .
Index: command.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/command.c,v
retrieving revision 1.108
diff -c -r1.108 command.c
*** command.c   1 Dec 2003 22:21:54 -0000       1.108
--- command.c   21 Dec 2003 04:17:24 -0000
***************
*** 65,70 ****
--- 65,72 ----
  static bool do_connect(const char *new_dbname, const char *new_user);
  static bool do_shell(const char *command);

+ static char *expand_tilde(char *filename);
+
  /*----------
   * HandleSlashCmds:
   *
***************
*** 522,527 ****
--- 524,530 ----
        /* \i is include file */
        else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0)
        {
+               char       *expanded_fname;
                char       *fname = scan_option(&string, OT_NORMAL, NULL,
true);

                if (!fname)
***************
*** 531,537 ****
                }
                else
                {
!                       success = (process_file(fname) == EXIT_SUCCESS);
                        free(fname);
                }
        }
--- 534,542 ----
                }
                else
                {
!                       expanded_fname = expand_tilde(fname);
!                       success = (process_file(expanded_fname) ==
EXIT_SUCCESS);
!                       free(expanded_fname);
                        free(fname);
                }
        }
***************
*** 1678,1683 ****
--- 1683,1731 ----
  }


+ /* expand_tilde
+  *
+  * substitute '~' with HOME or '~username' with username's home dir
+  *
+  */
+ static char *
+ expand_tilde(char *filename)
+ {
+       char       *home;
+       char       oldp, *p;
+       struct passwd *pw;
+
+       filename = xstrdup(filename);
+
+       /* try tilde expansion */
+       if (*filename == '~')
+       {
+               home = NULL;
+
+               p = filename+1;
+               while (*p != '/' && *p != '\0')
+                       p++;
+
+               oldp = *p;
+               *p = '\0';
+
+               if (*(filename+1) == '\0')
+                       home = xstrdup(getenv("HOME"));
+               else if ((pw = getpwnam(filename+1)) != NULL)
+                       home = xstrdup(pw->pw_dir);
+
+               *p = oldp;
+               if (home)
+               {
+                       home = realloc(home, strlen(home) + strlen(p) + 1);
+                       strcat(home,p);
+                       free(filename);
+                       filename = home;
+               }
+       }
+
+       return filename;
+ }

  /*
   * process_file



pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [GENERAL] Temporary tables and miscellaneous schemas
Next
From: Bruce Momjian
Date:
Subject: Re: [COMMITTERS] pgsql-server/ oc/src/sgml/datatype.sgml rc/bac ...