3
3
*
4
4
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
5
5
*
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 $
7
7
*/
8
8
#include "postgres_fe.h"
9
9
#include "common.h"
@@ -1133,15 +1133,14 @@ void
1133
1133
printQuery (const PGresult * result , const printQueryOpt * opt , FILE * fout )
1134
1134
{
1135
1135
int nfields ;
1136
+ int ncells ;
1136
1137
const char * * headers ;
1137
1138
const char * * cells ;
1138
1139
char * * footers ;
1139
1140
char * align ;
1140
1141
int i ;
1141
1142
1142
-
1143
1143
/* extract headers */
1144
-
1145
1144
nfields = PQnfields (result );
1146
1145
1147
1146
headers = calloc (nfields + 1 , sizeof (* headers ));
@@ -1155,15 +1154,15 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
1155
1154
headers [i ] = mbvalidate (PQfname (result , i ), opt -> topt .encoding );
1156
1155
1157
1156
/* set cells */
1158
-
1159
- cells = calloc (nfields * PQntuples ( result ) + 1 , sizeof (* cells ));
1157
+ ncells = PQntuples ( result ) * nfields ;
1158
+ cells = calloc (ncells + 1 , sizeof (* cells ));
1160
1159
if (!cells )
1161
1160
{
1162
1161
perror ("calloc" );
1163
1162
exit (EXIT_FAILURE );
1164
1163
}
1165
1164
1166
- for (i = 0 ; i < nfields * PQntuples ( result ) ; i ++ )
1165
+ for (i = 0 ; i < ncells ; i ++ )
1167
1166
{
1168
1167
if (PQgetisnull (result , i / nfields , i % nfields ))
1169
1168
cells [i ] = opt -> nullPrint ? opt -> nullPrint : "" ;
@@ -1185,6 +1184,11 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
1185
1184
}
1186
1185
1187
1186
footers [0 ] = malloc (100 );
1187
+ if (!footers [0 ])
1188
+ {
1189
+ perror ("malloc" );
1190
+ exit (EXIT_FAILURE );
1191
+ }
1188
1192
if (PQntuples (result ) == 1 )
1189
1193
snprintf (footers [0 ], 100 , gettext ("(1 row)" ));
1190
1194
else
@@ -1194,7 +1198,6 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
1194
1198
footers = NULL ;
1195
1199
1196
1200
/* set alignment */
1197
-
1198
1201
align = calloc (nfields + 1 , sizeof (* align ));
1199
1202
if (!align )
1200
1203
{
@@ -1221,13 +1224,12 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
1221
1224
}
1222
1225
1223
1226
/* call table printer */
1224
-
1225
1227
printTable (opt -> title , headers , cells ,
1226
- footers ? (const char * const * ) footers : ( const char * const * ) ( opt -> footers ) ,
1228
+ (const char * const * ) footers ,
1227
1229
align , & opt -> topt , fout );
1228
1230
1229
- free (( void * ) headers );
1230
- free (( void * ) cells );
1231
+ free (headers );
1232
+ free (cells );
1231
1233
if (footers )
1232
1234
{
1233
1235
free (footers [0 ]);
0 commit comments