33 *
44 * Copyright (c) 2000-2007, PostgreSQL Global Development Group
55 *
6- * $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.49 2007/01/05 22:19:49 momjian Exp $
6+ * $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.50 2007/02/08 11:10:27 petere Exp $
77 */
88#include "postgres_fe.h"
99
@@ -96,18 +96,18 @@ get_prompt(promptStatus_t status)
9696destination [0 ]= '\0' ;
9797
9898for (p = prompt_string ;
99- * p && strlen (destination )< MAX_PROMPT_SIZE ;
99+ * p && strlen (destination )< sizeof ( destination ) - 1 ;
100100p ++ )
101101{
102- memset (buf ,0 ,MAX_PROMPT_SIZE + 1 );
102+ memset (buf ,0 ,sizeof ( buf ) );
103103if (esc )
104104{
105105switch (* p )
106106{
107107/* Current database */
108108case '/' :
109109if (pset .db )
110- strncpy (buf ,PQdb (pset .db ),MAX_PROMPT_SIZE );
110+ strlcpy (buf ,PQdb (pset .db ),sizeof ( buf ) );
111111break ;
112112case '~' :
113113if (pset .db )
@@ -116,9 +116,9 @@ get_prompt(promptStatus_t status)
116116
117117if (strcmp (PQdb (pset .db ),PQuser (pset .db ))== 0 ||
118118((var = getenv ("PGDATABASE" ))&& strcmp (var ,PQdb (pset .db ))== 0 ))
119- strcpy (buf ,"~" );
119+ strlcpy (buf ,"~" , sizeof ( buf ) );
120120else
121- strncpy (buf ,PQdb (pset .db ),MAX_PROMPT_SIZE );
121+ strlcpy (buf ,PQdb (pset .db ),sizeof ( buf ) );
122122}
123123break ;
124124
@@ -132,7 +132,7 @@ get_prompt(promptStatus_t status)
132132/* INET socket */
133133if (host && host [0 ]&& !is_absolute_path (host ))
134134{
135- strncpy (buf ,host ,MAX_PROMPT_SIZE );
135+ strlcpy (buf ,host ,sizeof ( buf ) );
136136if (* p == 'm' )
137137buf [strcspn (buf ,"." )]= '\0' ;
138138}
@@ -143,22 +143,22 @@ get_prompt(promptStatus_t status)
143143if (!host
144144|| strcmp (host ,DEFAULT_PGSOCKET_DIR )== 0
145145|| * p == 'm' )
146- strncpy (buf ,"[local]" ,MAX_PROMPT_SIZE );
146+ strlcpy (buf ,"[local]" ,sizeof ( buf ) );
147147else
148- snprintf (buf ,MAX_PROMPT_SIZE ,"[local:%s]" ,host );
148+ snprintf (buf ,sizeof ( buf ) ,"[local:%s]" ,host );
149149}
150150#endif
151151}
152152break ;
153153/* DB server port number */
154154case '>' :
155155if (pset .db && PQport (pset .db ))
156- strncpy (buf ,PQport (pset .db ),MAX_PROMPT_SIZE );
156+ strlcpy (buf ,PQport (pset .db ),sizeof ( buf ) );
157157break ;
158158/* DB server user name */
159159case 'n' :
160160if (pset .db )
161- strncpy (buf ,session_username (),MAX_PROMPT_SIZE );
161+ strlcpy (buf ,session_username (),sizeof ( buf ) );
162162break ;
163163
164164case '0' :
@@ -252,7 +252,7 @@ get_prompt(promptStatus_t status)
252252fd = popen (file ,"r" );
253253if (fd )
254254{
255- fgets (buf ,MAX_PROMPT_SIZE - 1 ,fd );
255+ fgets (buf ,sizeof ( buf ) ,fd );
256256pclose (fd );
257257}
258258if (strlen (buf )> 0 && buf [strlen (buf )- 1 ]== '\n' )
@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status)
274274name [nameend ]= '\0' ;
275275val = GetVariable (pset .vars ,name );
276276if (val )
277- strncpy (buf ,val ,MAX_PROMPT_SIZE );
277+ strlcpy (buf ,val ,sizeof ( buf ) );
278278free (name );
279279p += nameend + 1 ;
280280break ;
@@ -312,9 +312,8 @@ get_prompt(promptStatus_t status)
312312}
313313
314314if (!esc )
315- strncat (destination ,buf ,MAX_PROMPT_SIZE - strlen (destination ));
315+ strlcat (destination ,buf ,sizeof (destination ));
316316}
317317
318- destination [MAX_PROMPT_SIZE ]= '\0' ;
319318return destination ;
320319}