33 *
44 * Copyright 2000 by PostgreSQL Global Development Group
55 *
6- * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.34 2002/10/24 01:33:50 momjian Exp $
6+ * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.35 2002/11/01 15:12:19 tgl Exp $
77 */
88#include "postgres_fe.h"
99#include "common.h"
@@ -212,21 +212,29 @@ print_aligned_text(const char *title, const char *const * headers,
212212for (ptr = headers ;* ptr ;ptr ++ )
213213col_count ++ ;
214214
215- widths = calloc (col_count ,sizeof (* widths ));
216- if (!widths )
215+ if (col_count > 0 )
217216{
218- perror ("calloc" );
219- exit (EXIT_FAILURE );
220- }
217+ widths = calloc (col_count ,sizeof (* widths ));
218+ if (!widths )
219+ {
220+ perror ("calloc" );
221+ exit (EXIT_FAILURE );
222+ }
221223
222- head_w = calloc (col_count ,sizeof (* head_w ));
223- if (!head_w )
224+ head_w = calloc (col_count ,sizeof (* head_w ));
225+ if (!head_w )
226+ {
227+ perror ("calloc" );
228+ exit (EXIT_FAILURE );
229+ }
230+ }
231+ else
224232{
225- perror ( "calloc" ) ;
226- exit ( EXIT_FAILURE ) ;
233+ widths = NULL ;
234+ head_w = NULL ;
227235}
228236
229- /* count rows */
237+ /* countcells ( rows * cols) */
230238for (ptr = cells ;* ptr ;ptr ++ )
231239cell_count ++ ;
232240
@@ -240,23 +248,25 @@ print_aligned_text(const char *title, const char *const * headers,
240248}
241249}
242250else
243- cell_w = 0 ;
244-
251+ cell_w = NULL ;
245252
246253/* calc column widths */
247254for (i = 0 ;i < col_count ;i ++ )
248255{
249- if ((tmp = pg_wcswidth ((unsignedchar * )headers [i ],strlen (headers [i ])))> widths [i ])
256+ tmp = pg_wcswidth ((unsignedchar * )headers [i ],strlen (headers [i ]));
257+ if (tmp > widths [i ])
250258widths [i ]= tmp ;
251259head_w [i ]= tmp ;
252260}
253261
254262for (i = 0 ,ptr = cells ;* ptr ;ptr ++ ,i ++ )
255263{
256- if ((tmp = pg_wcswidth ((unsignedchar * )* ptr ,strlen (* ptr )))> widths [i %col_count ])
264+ tmp = pg_wcswidth ((unsignedchar * )* ptr ,strlen (* ptr ));
265+ if (tmp > widths [i %col_count ])
257266widths [i %col_count ]= tmp ;
258267cell_w [i ]= tmp ;
259268}
269+
260270if (opt_border == 0 )
261271total_w = col_count - 1 ;
262272else if (opt_border == 1 )
@@ -272,10 +282,11 @@ print_aligned_text(const char *title, const char *const * headers,
272282{
273283int tlen ;
274284
275- if ((unsignedint ) (tlen = pg_wcswidth ((unsignedchar * )title ,strlen (title ))) >=total_w )
285+ tlen = pg_wcswidth ((unsignedchar * )title ,strlen (title ));
286+ if (tlen >= (int )total_w )
276287fprintf (fout ,"%s\n" ,title );
277288else
278- fprintf (fout ,"%-*s%s\n" , (int )( total_w - tlen ) /2 ,"" ,title );
289+ fprintf (fout ,"%-*s%s\n" , (( int )total_w - tlen ) /2 ,"" ,title );
279290}
280291
281292/* print headers */
@@ -330,7 +341,7 @@ print_aligned_text(const char *title, const char *const * headers,
330341}
331342
332343/* content */
333- if (opt_align [( i ) %col_count ]== 'r' )
344+ if (opt_align [i %col_count ]== 'r' )
334345{
335346fprintf (fout ,"%*s%s" ,
336347widths [i %col_count ]- cell_w [i ],"" ,cells [i ]);