33 *
44 * Copyright (c) 2000-2005, PostgreSQL Global Development Group
55 *
6- * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.63 2005/07/10 15:53:42 momjian Exp $
6+ * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.64 2005/07/14 06:46:17 momjian Exp $
77 */
88#include "postgres_fe.h"
99#include "common.h"
2929
3030#include "mbprint.h"
3131
32+ static void *
33+ pg_local_malloc (size_t size )
34+ {
35+ void * tmp ;
36+
37+ tmp = malloc (size );
38+ if (!tmp )
39+ {
40+ psql_error ("out of memory\n" );
41+ exit (EXIT_FAILURE );
42+ }
43+ return tmp ;
44+ }
45+
3246static int
3347num_numericseps (const char * my_str )
3448{
@@ -46,6 +60,7 @@ num_numericseps(const char *my_str)
4660else
4761return int_len /3 - 1 ;/* no leading separator */
4862}
63+
4964static int
5065len_with_numericsep (const char * my_str )
5166{
@@ -77,12 +92,7 @@ format_numericsep(char *my_str, char *numericsep)
7792if (digits_before_sep == 0 )
7893new_len -- ;/* no leading separator */
7994
80- new_str = malloc (new_len );
81- if (!new_str )
82- {
83- fprintf (stderr ,_ ("out of memory\n" ));
84- exit (EXIT_FAILURE );
85- }
95+ new_str = pg_local_malloc (new_len + 1 );
8696
8797for (i = 0 ,j = 0 ; ;i ++ ,j ++ )
8898{
@@ -167,13 +177,8 @@ print_unaligned_text(const char *title, const char *const *headers,
167177if ((opt_align [i %col_count ]== 'r' )&& strlen (* ptr )> 0 &&
168178opt_numericsep != NULL && strlen (opt_numericsep )> 0 )
169179{
170- char * my_cell = malloc (len_with_numericsep (* ptr ));
180+ char * my_cell = pg_local_malloc (len_with_numericsep (* ptr )+ 1 );
171181
172- if (!my_cell )
173- {
174- fprintf (stderr ,_ ("out of memory\n" ));
175- exit (EXIT_FAILURE );
176- }
177182strcpy (my_cell ,* ptr );
178183format_numericsep (my_cell ,opt_numericsep );
179184fputs (my_cell ,fout );
@@ -249,13 +254,8 @@ print_unaligned_vertical(const char *title, const char *const *headers,
249254if ((opt_align [i %col_count ]== 'r' )&& strlen (* ptr )!= 0 &&
250255opt_numericsep != NULL && strlen (opt_numericsep )> 0 )
251256{
252- char * my_cell = malloc (len_with_numericsep (* ptr ));
257+ char * my_cell = pg_local_malloc (len_with_numericsep (* ptr )+ 1 );
253258
254- if (!my_cell )
255- {
256- fprintf (stderr ,_ ("out of memory\n" ));
257- exit (EXIT_FAILURE );
258- }
259259strcpy (my_cell ,* ptr );
260260format_numericsep (my_cell ,opt_numericsep );
261261fputs (my_cell ,fout );
@@ -482,13 +482,8 @@ print_aligned_text(const char *title, const char *const *headers,
482482{
483483if (strlen (* ptr )> 0 && opt_numericsep != NULL && strlen (opt_numericsep )> 0 )
484484 {
485- char * my_cell = malloc (cell_w [i ]);
485+ char * my_cell = pg_local_malloc (cell_w [i ]+ 1 );
486486
487- if (!my_cell )
488- {
489- fprintf (stderr ,_ ("out of memory\n" ));
490- exit (EXIT_FAILURE );
491- }
492487strcpy (my_cell ,* ptr );
493488format_numericsep (my_cell ,opt_numericsep );
494489fprintf (fout ,"%*s%s" ,widths [i %col_count ]- cell_w [i ],"" ,my_cell );
@@ -634,12 +629,7 @@ print_aligned_vertical(const char *title, const char *const *headers,
634629fprintf (fout ,"%s\n" ,title );
635630
636631/* make horizontal border */
637- divider = malloc (hwidth + dwidth + 10 );
638- if (!divider )
639- {
640- fprintf (stderr ,_ ("out of memory\n" ));
641- exit (EXIT_FAILURE );
642- }
632+ divider = pg_local_malloc (hwidth + dwidth + 10 );
643633divider [0 ]= '\0' ;
644634if (opt_border == 2 )
645635strcat (divider ,"+-" );
@@ -661,15 +651,9 @@ print_aligned_vertical(const char *title, const char *const *headers,
661651{
662652if (!opt_barebones )
663653{
664- char * record_str = malloc (32 );
654+ char * record_str = pg_local_malloc (32 );
665655size_t record_str_len ;
666656
667- if (!record_str )
668- {
669- fprintf (stderr ,_ ("out of memory\n" ));
670- exit (EXIT_FAILURE );
671- }
672-
673657if (opt_border == 0 )
674658snprintf (record_str ,32 ,"* Record %d" ,record ++ );
675659else
@@ -709,13 +693,8 @@ print_aligned_vertical(const char *title, const char *const *headers,
709693fputs (" " ,fout );
710694
711695{
712- char * my_cell = malloc (cell_w [i ]);
696+ char * my_cell = pg_local_malloc (cell_w [i ]+ 1 );
713697
714- if (!my_cell )
715- {
716- fprintf (stderr ,_ ("out of memory\n" ));
717- exit (EXIT_FAILURE );
718- }
719698strcpy (my_cell ,* ptr );
720699if ((opt_align [i %col_count ]== 'r' )&& strlen (* ptr )!= 0 &&
721700opt_numericsep != NULL && strlen (opt_numericsep )> 0 )
@@ -855,13 +834,8 @@ print_html_text(const char *title, const char *const *headers,
855834else if ((opt_align [i %col_count ]== 'r' )&& strlen (* ptr )!= 0 &&
856835opt_numericsep != NULL && strlen (opt_numericsep )> 0 )
857836{
858- char * my_cell = malloc (len_with_numericsep (* ptr ));
837+ char * my_cell = pg_local_malloc (len_with_numericsep (* ptr )+ 1 );
859838
860- if (!my_cell )
861- {
862- fprintf (stderr ,_ ("out of memory\n" ));
863- exit (EXIT_FAILURE );
864- }
865839strcpy (my_cell ,* ptr );
866840format_numericsep (my_cell ,opt_numericsep );
867841html_escaped_print (my_cell ,fout );
@@ -946,13 +920,8 @@ print_html_vertical(const char *title, const char *const *headers,
946920else if ((opt_align [i %col_count ]== 'r' )&& strlen (* ptr )!= 0 &&
947921opt_numericsep != NULL && strlen (opt_numericsep )> 0 )
948922{
949- char * my_cell = malloc (len_with_numericsep (* ptr ));
923+ char * my_cell = pg_local_malloc (len_with_numericsep (* ptr )+ 1 );
950924
951- if (!my_cell )
952- {
953- fprintf (stderr ,_ ("out of memory\n" ));
954- exit (EXIT_FAILURE );
955- }
956925strcpy (my_cell ,* ptr );
957926format_numericsep (my_cell ,opt_numericsep );
958927html_escaped_print (my_cell ,fout );
@@ -1646,12 +1615,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *f
16461615exit (EXIT_FAILURE );
16471616}
16481617
1649- footers [0 ]= malloc (100 );
1650- if (!footers [0 ])
1651- {
1652- fprintf (stderr ,_ ("out of memory\n" ));
1653- exit (EXIT_FAILURE );
1654- }
1618+ footers [0 ]= pg_local_malloc (100 );
16551619if (PQntuples (result )== 1 )
16561620snprintf (footers [0 ],100 ,_ ("(1 row)" ));
16571621else