Skip to content

Commit 2da2d34

Browse files
committed
Clean up version comparison/display code, per gripe from Michael Fuhr.
1 parent 0372894 commit 2da2d34

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

src/bin/psql/startup.c

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.121 2005/09/05 13:59:08 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.122 2005/09/05 18:05:13 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -78,6 +78,7 @@ struct adhoc_opts
7878
bool no_psqlrc;
7979
};
8080

81+
static int parse_version(const char *versionString);
8182
static void parse_psql_options(int argc, char *argv[],
8283
struct adhoc_opts * options);
8384
static void process_psqlrc(char *argv0);
@@ -312,51 +313,46 @@ main(int argc, char *argv[])
312313

313314
if (!QUIET() && !pset.notty)
314315
{
315-
/*
316-
* Server value for 8.12 is 80102.
317-
* This code does not handle release numbers like
318-
* 8.112. (Is that 8.1, version 12, or 8.11, version 2?
319-
*/
320-
int client_ver_major_int = atoi(PG_VERSION) * 100 +
321-
strchr(PG_VERSION, '.')[1] - '0';
322-
int client_ver_int = atoi(PG_VERSION) * 10000 +
323-
(strchr(PG_VERSION, '.')[1] - '0') * 100 +
324-
(isdigit(strchr(PG_VERSION, '.')[2]) ?
325-
strchr(PG_VERSION, '.')[2] - '0' : '\0');
326-
327-
if (pset.sversion / 100 != client_ver_major_int)
328-
{
329-
printf(_("WARNING: You are connected to a server with major version %d.%d,\n"
330-
"but your %s client is major version %d.%d. Informational backslash\n"
331-
"commands, like \\d, might not work properly.\n\n"),
332-
pset.sversion / 10000, (pset.sversion / 100) % 10,
333-
pset.progname, atoi(PG_VERSION), strchr(PG_VERSION, '.')[1] - '0');
334-
}
316+
int client_ver = parse_version(PG_VERSION);
335317

336-
if (pset.sversion != client_ver_int)
318+
if (pset.sversion != client_ver)
337319
{
320+
const char *server_version;
338321
char server_ver_str[16];
339322

340-
snprintf(server_ver_str, 16, "%d.%c%c", pset.sversion / 10000,
341-
(pset.sversion / 100) % 10 + '0',
342-
/* print last digit? */
343-
(pset.sversion % 10 != 0) ?
344-
pset.sversion % 10 + '0' : '\0');
345-
346-
printf(_("Welcome to %s, the PostgreSQL interactive terminal.\n"),
347-
pset.progname);
348-
printf(_("%s version %s, server version %s\n\n"),
349-
pset.progname, PG_VERSION, server_ver_str);
323+
/* Try to get full text form, might include "devel" etc */
324+
server_version = PQparameterStatus(pset.db, "server_version");
325+
if (!server_version)
326+
{
327+
snprintf(server_ver_str, sizeof(server_ver_str),
328+
"%d.%d.%d",
329+
pset.sversion / 10000,
330+
(pset.sversion / 100) % 100,
331+
pset.sversion % 100);
332+
server_version = server_ver_str;
333+
}
334+
335+
printf(_("Welcome to %s %s (server %s), the PostgreSQL interactive terminal.\n\n"),
336+
pset.progname, PG_VERSION, server_version);
350337
}
351338
else
352339
printf(_("Welcome to %s %s, the PostgreSQL interactive terminal.\n\n"),
353-
pset.progname, PG_VERSION);
340+
pset.progname, PG_VERSION);
354341

355342
printf(_("Type: \\copyright for distribution terms\n"
356343
" \\h for help with SQL commands\n"
357344
" \\? for help with psql commands\n"
358345
" \\g or terminate with semicolon to execute query\n"
359346
" \\q to quit\n\n"));
347+
348+
if (pset.sversion / 100 != client_ver / 100)
349+
printf(_("WARNING: You are connected to a server with major version %d.%d,\n"
350+
"but your %s client is major version %d.%d. Some backslash commands,\n"
351+
"such as \\d, might not work properly.\n\n"),
352+
pset.sversion / 10000, (pset.sversion / 100) % 100,
353+
pset.progname,
354+
client_ver / 10000, (client_ver / 100) % 100);
355+
360356
#ifdef USE_SSL
361357
printSSLInfo();
362358
#endif
@@ -383,6 +379,28 @@ main(int argc, char *argv[])
383379
}
384380

385381

382+
/*
383+
* Convert a version string into a number.
384+
*/
385+
static int
386+
parse_version(const char *versionString)
387+
{
388+
int cnt;
389+
int vmaj,
390+
vmin,
391+
vrev;
392+
393+
cnt = sscanf(versionString, "%d.%d.%d", &vmaj, &vmin, &vrev);
394+
395+
if (cnt < 2)
396+
return -1;
397+
398+
if (cnt == 2)
399+
vrev = 0;
400+
401+
return (100 * vmaj + vmin) * 100 + vrev;
402+
}
403+
386404

387405
/*
388406
* Parse command line options

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy