From 2795592e52c88e510ae4bcbc17b305d6adc0b2b6 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Fri, 14 Oct 2011 20:26:28 -0400 Subject: [PATCH] Allow a major PG version psql .psqlrc file to be used if a minor matching version file does not exist. This avoids needing to rename .psqlrc files after minor version upgrades. --- doc/src/sgml/ref/psql-ref.sgml | 6 ++++-- src/bin/psql/startup.c | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 662eab7b6d0..4eefe3b4ab2 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -3332,8 +3332,10 @@ PSQL_EDITOR_LINENUMBER_ARG='--line ' Both the system-wide psqlrc file and the user's ~/.psqlrc file can be made version-specific by appending a dash and the PostgreSQL - release number, for example ~/.psqlrc-&version;. - A matching version-specific file will be read in preference to a + major or minor release number, for example + ~/.psqlrc-9.2 or + ~/.psqlrc-9.2.5. The most specific + version-matching file will be read in preference to a non-version-specific file. diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 3c17eece7b8..71829eb6ec3 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -594,20 +594,27 @@ process_psqlrc(char *argv0) static void process_psqlrc_file(char *filename) { - char *psqlrc; + char *psqlrc_minor, *psqlrc_major; #if defined(WIN32) && (!defined(__MINGW32__)) #define R_OK 4 #endif - psqlrc = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1); - sprintf(psqlrc, "%s-%s", filename, PG_VERSION); + psqlrc_minor = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1); + sprintf(psqlrc_minor, "%s-%s", filename, PG_VERSION); + psqlrc_major = pg_malloc(strlen(filename) + 1 + strlen(PG_MAJORVERSION) + 1); + sprintf(psqlrc_major, "%s-%s", filename, PG_MAJORVERSION); - if (access(psqlrc, R_OK) == 0) - (void) process_file(psqlrc, false, false); + /* check for minor version first, then major, then no version */ + if (access(psqlrc_minor, R_OK) == 0) + (void) process_file(psqlrc_minor, false, false); + else if (access(psqlrc_major, R_OK) == 0) + (void) process_file(psqlrc_major, false, false); else if (access(filename, R_OK) == 0) (void) process_file(filename, false, false); - free(psqlrc); + + free(psqlrc_minor); + free(psqlrc_major); }