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)
9696 destination [0 ] = '\0' ;
9797
9898 for (p = prompt_string ;
99- * p && strlen (destination ) < MAX_PROMPT_SIZE ;
99+ * p && strlen (destination ) < sizeof ( destination ) - 1 ;
100100 p ++ )
101101 {
102- memset (buf , 0 , MAX_PROMPT_SIZE + 1 );
102+ memset (buf , 0 , sizeof ( buf ) );
103103 if (esc )
104104 {
105105 switch (* p )
106106 {
107107 /* Current database */
108108 case '/' :
109109 if (pset .db )
110- strncpy (buf , PQdb (pset .db ), MAX_PROMPT_SIZE );
110+ strlcpy (buf , PQdb (pset .db ), sizeof ( buf ) );
111111 break ;
112112 case '~' :
113113 if (pset .db )
@@ -116,9 +116,9 @@ get_prompt(promptStatus_t status)
116116
117117 if (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 ) );
120120 else
121- strncpy (buf , PQdb (pset .db ), MAX_PROMPT_SIZE );
121+ strlcpy (buf , PQdb (pset .db ), sizeof ( buf ) );
122122 }
123123 break ;
124124
@@ -132,7 +132,7 @@ get_prompt(promptStatus_t status)
132132 /* INET socket */
133133 if (host && host [0 ] && !is_absolute_path (host ))
134134 {
135- strncpy (buf , host , MAX_PROMPT_SIZE );
135+ strlcpy (buf , host , sizeof ( buf ) );
136136 if (* p == 'm' )
137137 buf [strcspn (buf , "." )] = '\0' ;
138138 }
@@ -143,22 +143,22 @@ get_prompt(promptStatus_t status)
143143 if (!host
144144 || strcmp (host , DEFAULT_PGSOCKET_DIR ) == 0
145145 || * p == 'm' )
146- strncpy (buf , "[local]" , MAX_PROMPT_SIZE );
146+ strlcpy (buf , "[local]" , sizeof ( buf ) );
147147 else
148- snprintf (buf , MAX_PROMPT_SIZE , "[local:%s]" , host );
148+ snprintf (buf , sizeof ( buf ) , "[local:%s]" , host );
149149 }
150150#endif
151151 }
152152 break ;
153153 /* DB server port number */
154154 case '>' :
155155 if (pset .db && PQport (pset .db ))
156- strncpy (buf , PQport (pset .db ), MAX_PROMPT_SIZE );
156+ strlcpy (buf , PQport (pset .db ), sizeof ( buf ) );
157157 break ;
158158 /* DB server user name */
159159 case 'n' :
160160 if (pset .db )
161- strncpy (buf , session_username (), MAX_PROMPT_SIZE );
161+ strlcpy (buf , session_username (), sizeof ( buf ) );
162162 break ;
163163
164164 case '0' :
@@ -252,7 +252,7 @@ get_prompt(promptStatus_t status)
252252 fd = popen (file , "r" );
253253 if (fd )
254254 {
255- fgets (buf , MAX_PROMPT_SIZE - 1 , fd );
255+ fgets (buf , sizeof ( buf ) , fd );
256256 pclose (fd );
257257 }
258258 if (strlen (buf ) > 0 && buf [strlen (buf ) - 1 ] == '\n' )
@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status)
274274 name [nameend ] = '\0' ;
275275 val = GetVariable (pset .vars , name );
276276 if (val )
277- strncpy (buf , val , MAX_PROMPT_SIZE );
277+ strlcpy (buf , val , sizeof ( buf ) );
278278 free (name );
279279 p += nameend + 1 ;
280280 break ;
@@ -312,9 +312,8 @@ get_prompt(promptStatus_t status)
312312 }
313313
314314 if (!esc )
315- strncat (destination , buf , MAX_PROMPT_SIZE - strlen (destination ));
315+ strlcat (destination , buf , sizeof (destination ));
316316 }
317317
318- destination [MAX_PROMPT_SIZE ] = '\0' ;
319318 return destination ;
320319}
0 commit comments