33 *
44 * Copyright (c) 2000-2003, PostgreSQL Global Development Group
55 *
6- * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.47 2004/05/18 20:18:58 momjian Exp $
6+ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.48 2004/05/23 22: 20:10 neilc Exp $
77 */
88#include "postgres_fe.h"
99#include "common.h"
@@ -1133,15 +1133,14 @@ void
11331133printQuery (const PGresult * result ,const printQueryOpt * opt ,FILE * fout )
11341134{
11351135int nfields ;
1136+ int ncells ;
11361137const char * * headers ;
11371138const char * * cells ;
11381139char * * footers ;
11391140char * align ;
11401141int i ;
11411142
1142-
11431143/* extract headers */
1144-
11451144nfields = PQnfields (result );
11461145
11471146headers = calloc (nfields + 1 ,sizeof (* headers ));
@@ -1155,15 +1154,15 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
11551154headers [i ]= mbvalidate (PQfname (result ,i ),opt -> topt .encoding );
11561155
11571156/* set cells */
1158-
1159- cells = calloc (nfields * PQntuples ( result ) + 1 ,sizeof (* cells ));
1157+ ncells = PQntuples ( result ) * nfields ;
1158+ cells = calloc (ncells + 1 ,sizeof (* cells ));
11601159if (!cells )
11611160{
11621161perror ("calloc" );
11631162exit (EXIT_FAILURE );
11641163}
11651164
1166- for (i = 0 ;i < nfields * PQntuples ( result ) ;i ++ )
1165+ for (i = 0 ;i < ncells ;i ++ )
11671166{
11681167if (PQgetisnull (result ,i /nfields ,i %nfields ))
11691168cells [i ]= opt -> nullPrint ?opt -> nullPrint :"" ;
@@ -1185,6 +1184,11 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
11851184}
11861185
11871186footers [0 ]= malloc (100 );
1187+ if (!footers [0 ])
1188+ {
1189+ perror ("malloc" );
1190+ exit (EXIT_FAILURE );
1191+ }
11881192if (PQntuples (result )== 1 )
11891193snprintf (footers [0 ],100 ,gettext ("(1 row)" ));
11901194else
@@ -1194,7 +1198,6 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
11941198footers = NULL ;
11951199
11961200/* set alignment */
1197-
11981201align = calloc (nfields + 1 ,sizeof (* align ));
11991202if (!align )
12001203{
@@ -1221,13 +1224,12 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
12211224}
12221225
12231226/* call table printer */
1224-
12251227printTable (opt -> title ,headers ,cells ,
1226- footers ? (const char * const * )footers : ( const char * const * ) ( opt -> footers ) ,
1228+ (const char * const * )footers ,
12271229align ,& opt -> topt ,fout );
12281230
1229- free (( void * ) headers );
1230- free (( void * ) cells );
1231+ free (headers );
1232+ free (cells );
12311233if (footers )
12321234{
12331235free (footers [0 ]);