Commit 256d4639 authored by Neil Conway's avatar Neil Conway

A few cosmetic fixes and code cleanup.

parent 9d6570b8
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.86 2004/05/07 00:24:58 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.87 2004/05/23 22:20:10 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
...@@ -802,7 +802,8 @@ PrintQueryResults(PGresult *results) ...@@ -802,7 +802,8 @@ PrintQueryResults(PGresult *results)
char buf[10]; char buf[10];
success = true; success = true;
sprintf(buf, "%u", (unsigned int) PQoidValue(results)); snprintf(buf, sizeof(buf),
"%u", (unsigned int) PQoidValue(results));
if (!QUIET()) if (!QUIET())
{ {
if (pset.popt.topt.format == PRINT_HTML) if (pset.popt.topt.format == PRINT_HTML)
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2003, PostgreSQL Global Development Group * Copyright (c) 2000-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.47 2004/05/18 20:18:58 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.48 2004/05/23 22:20:10 neilc Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "common.h" #include "common.h"
...@@ -1133,15 +1133,14 @@ void ...@@ -1133,15 +1133,14 @@ void
printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
{ {
int nfields; int nfields;
int ncells;
const char **headers; const char **headers;
const char **cells; const char **cells;
char **footers; char **footers;
char *align; char *align;
int i; int i;
/* extract headers */ /* extract headers */
nfields = PQnfields(result); nfields = PQnfields(result);
headers = calloc(nfields + 1, sizeof(*headers)); headers = calloc(nfields + 1, sizeof(*headers));
...@@ -1155,15 +1154,15 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) ...@@ -1155,15 +1154,15 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding); headers[i] = mbvalidate(PQfname(result, i), opt->topt.encoding);
/* set cells */ /* set cells */
ncells = PQntuples(result) * nfields;
cells = calloc(nfields * PQntuples(result) + 1, sizeof(*cells)); cells = calloc(ncells + 1, sizeof(*cells));
if (!cells) if (!cells)
{ {
perror("calloc"); perror("calloc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (i = 0; i < nfields * PQntuples(result); i++) for (i = 0; i < ncells; i++)
{ {
if (PQgetisnull(result, i / nfields, i % nfields)) if (PQgetisnull(result, i / nfields, i % nfields))
cells[i] = opt->nullPrint ? opt->nullPrint : ""; cells[i] = opt->nullPrint ? opt->nullPrint : "";
...@@ -1185,6 +1184,11 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) ...@@ -1185,6 +1184,11 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
} }
footers[0] = malloc(100); footers[0] = malloc(100);
if (!footers[0])
{
perror("malloc");
exit(EXIT_FAILURE);
}
if (PQntuples(result) == 1) if (PQntuples(result) == 1)
snprintf(footers[0], 100, gettext("(1 row)")); snprintf(footers[0], 100, gettext("(1 row)"));
else else
...@@ -1194,7 +1198,6 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) ...@@ -1194,7 +1198,6 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
footers = NULL; footers = NULL;
/* set alignment */ /* set alignment */
align = calloc(nfields + 1, sizeof(*align)); align = calloc(nfields + 1, sizeof(*align));
if (!align) if (!align)
{ {
...@@ -1221,13 +1224,12 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) ...@@ -1221,13 +1224,12 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
} }
/* call table printer */ /* call table printer */
printTable(opt->title, headers, cells, printTable(opt->title, headers, cells,
footers ? (const char *const *) footers : (const char *const *) (opt->footers), (const char *const *) footers,
align, &opt->topt, fout); align, &opt->topt, fout);
free((void *) headers); free(headers);
free((void *) cells); free(cells);
if (footers) if (footers)
{ {
free(footers[0]); free(footers[0]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment